-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;
}
請修復您的縮進以便清晰。 –
你有一個錯字:'rev_dispaly'不是'rev_display'。 – aschepler
@aschepler謝謝我發現這個錯誤請糾正我的另一個 – user6547375