2016-08-24 95 views
-3

我剛做了一個雙鏈表的程序,其中我試圖在每次插入操作完成後打印值。鏈接列表在第一次迭代後沒有打印值

第一次插入後沒有值正在打印,但從第二次插入後,打印值正常(第一次除外)。

我特此附接全碼

// Double Linked List 
#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h> 

struct node 
{ 
    int data; 
    struct node *next,*prev; 
}; 

struct node *head; 

struct node *getnewnode(int); 
void insertathead(int); 
void insertattail(int); 
void display(); 
void rev_display(); 

void main() 
{ 
    char c; 
    int n,n1; 

    clrscr(); 
    head = NULL; 
    do 
    { 
     printf("\n Enter Data Element"); 
     scanf("%d", &n); 
     printf("Press 1 to insert at beginning \n Press 2 to insert at the end"); 
     scanf("%d", &n1); 

     if(n1 == 1) 
     { 
      insertathead(n); 
      display(); 
      rev_display(); 
     } 
     if(n1 == 2) 
     { 
      insertattail(n); 
      display(); 
      rev_display(); 
     } 
     printf("Do you wish to enter more (Y/N)"); 
     c = getch(); 
    } while(c == 'Y' || c == 'y'); 
    getch(); 
} 

struct node *getnewnode(int x) 
{ 
    struct node *newnode = (struct node*)malloc(sizeof(struct node)); 
    newnode->data = x; 
    newnode->next = NULL; 
    newnode->prev = NULL; 
    return(newnode); 
} 

void insertathead(int x) 
{ 
    struct node *temp = getnewnode(x); 
    if(head == NULL) 
    { 
     head = temp; 
    } 
    else 
    { 
     head->prev = temp; 
     temp->next = head; 
     head = temp; 
    } 
} 

void display() 
{ 
    struct node *temp; 
    temp = head; 
    printf("Forward:\n"); 

    while(temp->next != NULL) 
    { 
     printf("%d ", temp->data); 
     temp = temp->next; 
    } 
    printf("\n"); 
} 

void rev_display() 
{ 
    struct node *temp; 
    temp = head; 

    while(temp->next != NULL) 
    { 
     temp = temp->next; 
    } 

    while(temp->prev != NULL) 
    { 
     printf("%d ", temp->data); 
     temp = temp->prev; 
    } 
} 

void insertattail(int x) 
{ 
    struct node *temp = getnewnode(x); 
    struct node *t; 

    t = head; 

    while(t->next != NULL) 
    { 
     t = t->next; 
    } 
    t->next = temp; 
    temp->prev = t; 
} 
+0

請修復您的縮進以便清晰。 –

+0

你有一個錯字:'rev_dispaly'不是'rev_display'。 – aschepler

+0

@aschepler謝謝我發現這個錯誤請糾正我的另一個 – user6547375

回答

1

的錯誤是在while循環的定義。當你達到一個沒有預先錄入的設置時,你停下來。噹噹前條目爲NULL時應停止

另請注意,在您原來的電話號碼rev_display()中,您定義的功能爲rev_dispaly()。該錯字應該修復。

您還假定insertattail()永遠不會有列表爲空的情況(head == NULL)我將在rev_display修復程序後顯示這種情況。

void rev_display() 
    { 
    struct node *temp; 
    temp=head; 
    // This correctly finds the last entry 
    while(temp->next!=NULL) 
     { 
     temp=temp->next; 
     } 
    /* This will stop when you reach the entry with no previous entry */ 
    while(temp->prev!=NULL) 
     { 
     printf("%d ",temp->data); 
     temp=temp->prev; 
     } 
} 

的代碼確實應該

void rev_display() 
    { 
    struct node *temp; 
    temp=head; 
    // This correctly finds the last entry 
    while(temp->next!=NULL) 
     { 
     temp=temp->next; 
     } 
    /* This will correctly include the head as well in the print */ 
    while(temp != NULL) 
     { 
     printf("%d ",temp->data); 
     temp=temp->prev; 
     } 
} 

你不檢查在insertattail()空列表情況。

void insertattail(int x) 
    { 
    struct node *temp=getnewnode(x); 
    struct node *t; 
    t=head; 
    // Note that this assumes that the list is not empty 
    while(t->next!=NULL) 
     { 
     t=t->next; 
     } 
    t->next=temp; 
    temp->prev=t; 
    } 

這需要檢查空的列表。

void insertattail(int x) 
    { 
    struct node *temp=getnewnode(x); 
    struct node *t; 
    // First check if the list is empty 
    if(head==NULL) 
     { 
     head=temp; 
     head->next = NULL; 
     head->prev = NULL;  
     } 
    else 
     { 
     t=head; 
     // This list is not empty so find the end 
     while(t->next!=NULL) 
      { 
      t=t->next; 
      } 
      t->next=temp; 
      temp->prev=t; 
     } 
    } 
相關問題