2015-10-05 40 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