-3
방금 삽입 작업을 한 후에 값을 인쇄하려고하는 이중 링크 목록의 프로그램을 만들었습니다.링크 목록이 첫 번째 반복 이후에 값을 인쇄하지 않습니다.
첫 번째 삽입 후 아무런 값도 인쇄되지 않지만 두 번째 삽입에서 첫 번째 경우를 제외하고 값이 잘 인쇄됩니다.
는 본인 오류가 while 루프의 정의에 전체 코드
// 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