2016-08-24 2 views
-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; 
} 
+0

들여 쓰기가 잘 보이도록 수정하십시오. –

+0

오타가 있습니다 :'rev_dispaly'는'rev_display'가 아닙니다. – aschepler

+0

@aschepler 감사합니다 실수를 발견했습니다 내 다른 하나를 바로 잡으십시오 – user6547375

답변

1

를 부착하고있다. 이전 입력이없는 설정에 도달하면 중지합니다. 현재 항목이 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; 
     } 
} 

코드는 정말 당신은 insertattail()에서 빈리스트 사례를 확인하지 않는

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

해야한다.

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; 
     } 
    } 
관련 문제