在這裏,我已經做出了結構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鏈接,但不是下一個鏈接 –