第一次迭代后链接列表不打印该值

时间:2016-08-24 18:29:25

标签: c data-structures linked-list

我刚刚创建了一个双链表的程序,我在每次插入操作完成后都试图打印这些值。

首次插入后,没有打印任何值,但从第二次插入开始,该值打印正常(第一次除外)。

我特此附上完整的代码

// 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;
} 

1 个答案:

答案 0 :(得分: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;
        }
  }