在这里,我已经做出了结构DLL: -如何在C编程中使用双向链接列表接受字符串?
struct node
{
char letter;
struct node* prev;
struct node* next;
};
,这是接受字符串的函数: -
struct node* accept(struct node* head)
{
int i=0;
char dummy, ch;
struct node* memory, *memory1;
memory = (struct node*)malloc(sizeof(struct node));
printf("\n Enter the letters ");
scanf("%c",&dummy);
ch = getchar();
if(ch == '\n')
{
return head;
}
memory->letter = ch;
memory->prev = NULL;
memory->next = NULL;
head = memory;
while(ch!='\n')
{
ch = getchar();
memory1 = (struct node*)malloc(sizeof(struct node));
memory1->letter = ch;
memory1->prev = memory;
memory1->next = NULL;
memory = memory1;
i++;
}
n = i;
return head;
}
及以下的几乎一半完成的程序: -
#include<stdio.h>
#include<stdlib.h>
struct node
{
char letter;
struct node* prev;
struct node* next;
};
int n= 0;
struct node* accept(struct node* head)
{
int i=0;
char dummy, ch;
struct node* memory, *memory1;
memory = (struct node*)malloc(sizeof(struct node));
printf("\n Enter the letters ");
scanf("%c",&dummy);
ch = getchar();
if(ch == '\n')
{
return head;
}
memory->letter = ch;
memory->prev = NULL;
memory->next = NULL;
head = memory;
while(ch!='\n')
{
ch = getchar();
memory1 = (struct node*)malloc(sizeof(char));
memory1->letter = ch;
memory1->prev = memory;
memory1->next = NULL;
memory = memory1;
i++;
}
n = i;
return head;
}
void display(struct node* head)
{
if(head == NULL)
{
printf("\n Nothing to display .. ");
return ;
}
struct node* temp;
temp = head;
printf("\n");
while(temp!=NULL)
{
printf("%c",temp->letter);
temp = temp->next;
}
printf("\n");
}
void reverseDisplay(struct node* head)
{
struct node* temp;
temp = head;
while(temp!= NULL)
{
reverseDisplay(temp->next);
}
printf("%c",temp->letter);
}
struct node* insertChar(struct node* head)
{
int pos, i;
char ch;
struct node* temp, *temp1;
temp1 = head;
printf("\n Enter the position where you want to insert a letter : ");
scanf("%d",&pos);
if(pos <1 && pos >(n+1))
{
printf("\n INVALID POSITION .. ");
}
else
{
temp = (struct node*)malloc(sizeof(char));
ch = getchar();
temp->letter = ch;
if(ch == '\n')
{
return head;
}
if(head == NULL)
{
temp->next = NULL;
temp->prev = NULL;
head = temp;
return head;
}
for(i=1; i<pos-1; i++)
{
temp1 = temp1->next;
}
temp->next = temp1->next;
temp1->next = temp;
temp->prev = temp1;
temp1->next->prev = temp;
n++;
}
return head;
}
int main()
{
struct node* head;
head = NULL;
int i,ch;
char c;
do
{
printf("\n Enter your choice :\n");
printf("\n 1. Accept the string : ");
printf("\n 2. Display the string : ");
printf("\n 3. Reverse Display : ");
printf("\n 4. Insert a character : ");
printf("\n 5. Delete a character : ");
printf("\n 6. Modify a character : ");
printf("\n 7. Revert the string : ");
printf("\n 8. Exit \n : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Accepting a string ...");
head = accept(head);
break;
case 2:
printf("\n Displaying the string .. ");
display(head);
break;
case 3:
printf("\n Reverse Displaying ..");
if(head == NULL)
{
printf("\n There is nothing to be displayed ..");
break;
}
printf("\n");
reverseDisplay(head);
break;
case 4:
head = insertChar(head);
break;
}
}while(ch!=8);
return 0;
}
问题是,当我选择显示文本时,这只显示我输入到字符串中的第一个字母。无法弄清楚。请帮帮我。
几个输出实例: -
输入您的选择:
- 接受字符串:
- 显示的字符串:
- 反转显示:
- 插入字符:
- 删除一个字符:
- 修改人物:
- 还原字符串:
退出 :1
接受一个字符串... 输入字母阿米特upadhyay
输入您的选择:
接受字符串:
- 显示字符串:
- 反转显示:
- 插入一个字符:
- 删除字符:
- 修改字符:
- 还原字符串:
退出 :2
显示字符串.. 一个
输入您的选择:
接受字符串:
- 显示的字符串:
- 反转显示:
- 插入字符:
- 删除字符:
- 修改字符:
- 恢复字符串:
- 退出 :
1)**总是**检查可能遇到错误的函数的结果。 2)不要将'malloc'和朋友的结果放在C中! 3)使用调试器。 – Olaf
@Olaf用户调试器,如果你能帮助我理解错误的地方以及我应该怎样改进它。那么请告诉我 –
嗯,你在哪里设置一个 - >除了NULL之外的任何东西。看起来像你设置了prev链接,但不是下一个链接 –