2016-08-24 99 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; 
     } 
    } 
相关问题