2015-10-05 48 views
-1

在这里,我已经做出了结构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. 接受字符串:
  2. 显示的字符串:
  3. 反转显示:
  4. 插入字符:
  5. 删除一个字符:
  6. 修改人物:
  7. 还原字符串:
  8. 退出 :1

    接受一个字符串... 输入字母阿米特upadhyay

    输入您的选择:

  9. 接受字符串:

  10. 显示字符串:
  11. 反转显示:
  12. 插入一个字符:
  13. 删除字符:
  14. 修改字符:
  15. 还原字符串:
  16. 退出 :2

    显示字符串.. 一个

    输入您的选择:

  17. 接受字符串:

  18. 显示的字符串:
  19. 反转显示:
  20. 插入字符:
  21. 删除字符:
  22. 修改字符:
  23. 恢复字符串:
  24. 退出 :
+1

1)**总是**检查可能遇到错误的函数的结果。 2)不要将'malloc'和朋友的结果放在C中! 3)使用调试器。 – Olaf

+0

@Olaf用户调试器,如果你能帮助我理解错误的地方以及我应该怎样改进它。那么请告诉我 –

+0

嗯,你在哪里设置一个 - >除了NULL之外的任何东西。看起来像你设置了prev链接,但不是下一个链接 –

回答

1

你缺少做一个链接。

while(ch!='\n') 
{ 
    ch = getchar(); 
    memory1 = (struct node*)malloc(sizeof(struct node)); 
    memory1->letter = ch; 
    memory1->prev = memory; 
    memory1->next = NULL; 

    // The missing link 
    memory->next = memory1; 

    memory = memory1; 
    i++; 
} 
+0

@RSahu好赶上! – 2015-10-06 00:12:40

1

在你malloc是你没有为这两个指针ALLOC足够的空间,即使用

(struct node*)malloc(sizeof(struct node)); 

,而不是

(struct node*)malloc(sizeof(char)); 

你也忘了初始化memory->next指针,即添加

memory->next = memory1; 

struct node* accept(struct node* head)的while循环

+0

问题仍然存在。显示时只显示第一个字母 –

+0

,这是一个打字错误。即使纠正问题仍然是一样的。 –

+0

并且应该是(struct node *)malloc(sizeof(struct node)),而不是你所说的。现在回复,如果你可以 –

1

你是不是问scanf函数的字符串,但对于char

scanf("%c",&dummy); 

应该是:

scanf("%s", &dummy); 

%c = character
%d = decimal (intiger)
%f = flaot
%s = string (character array)

的scanf()作为输入读取和printf()作为打印输出函数查找上面列出的特殊格式化字符串,然后查找字符串后面的输入/输出值。因此,当你有scanf("%d", &value)时,scanf将接受所有输入作为十进制值,如果你这样做scanf("%c", &value) scanf会查找一个字符并在接受一个字符后停止,但如果你把scanf("%s", &value)取回所有字符直到输入中断字符为止通常是\n或者\r\n\r取决于系统

+0

好的,我也是这样做的,但先生我不知道我们为什么这样做。你能告诉我吗 ?为什么我们需要写这个语句scanf(“%s”,&dummy)?请回答 –

+0

编辑该问题以帮助您理解。 – 2015-10-06 00:12:11