2010-09-02 114 views
1

我做了下列不打印清单的链表。打印记录功能不起作用。链接列表不打印清单

#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h> 
#include<string.h> 
struct student 
    { 
    char *name; 
    int roll_no; 
    struct student *ptr_next; 
    }*ptr_this,*ptr_first; 
void PrintMenu(void); 
void AddRecord(void); 
void DeleteRecord(void); 
void SearchRecord(void); 
void SortRecord(void); 
void PrintRecord(void); 
int main() 
{ 
    char choice; 
    ptr_this=ptr_first=(struct student*)NULL; 
    while(1) 
    { 
     PrintMenu(); 
     choice=getche(); 
     switch(choice) 
     { 
      case '1': 
      AddRecord(); 
      break; 
      case '2': 
      DeleteRecord(); 
      break; 
      case '3': 
      SearchRecord(); 
      break; 
      case '4': 
      SortRecord(); 
      break; 
      case '5': 
      PrintRecord(); 
      break; 
      case '6': 
      exit(0); 
      default: 
      printf("Enter a valid choice.\n"); 
      getch(); 
     } 
    } 

} 
void PrintMenu(void) 
{ 
    clrscr(); 
    printf("Database System.\n1.Add Record\n2.Delete Record\n3.Search Record \n4.Sort Records\n5.Display Records\n6.exit"); 

} 
void AddRecord(void) 
{ 
    char temp[100]; 
    int i; 
    struct student *ptr_new; 
    ptr_new=(struct student*)malloc(sizeof(struct student)); 
    if(ptr_new==(struct student*)NULL) 
    { 
     printf("Sorry but you can not add any more data becasue the computer memory is full.\n"); 
     return; 
    } 
    printf("Enter Name:\n"); 
    gets(temp); 
    ptr_new->name=(char*)malloc(sizeof(char)*(strlen(temp)+1)); 
    strncpy(ptr_new->name,temp,strlen(temp)+1); 
    printf("Enter Roll no:"); 
    gets(temp); 
    ptr_new->roll_no=atoi(temp); 
    ptr_new->ptr_next=(struct student*)NULL; 
    if(ptr_first->ptr_next==(struct student*)NULL) 
    { 
     ptr_first=ptr_this=ptr_new; 
    } 
    else 
    { 
     ptr_this->ptr_next=ptr_new; 
     ptr_this=ptr_new; 
    } 
    printf("Record Successfully added.\n"); 
    printf("%s",ptr_this->name); 
    getch(); 
} 

void DeleteRecord(void) 
{ 
    char temp[100]; 
    int rec_no,i; 
    struct student* ptr_del,*ptr_prev; 
    printf("Enter Record Number to be Deleted:\n"); 
    gets(temp); 
    rec_no=atoi(temp); 
    ptr_del=ptr_prev=ptr_first; 
    for(i=0;ptr_del!=(struct student*)NULL;i++,ptr_del=ptr_del->ptr_next) 
    { 
     if(i==rec_no) 
     { 
      if(ptr_del==ptr_first) 
       ptr_first=ptr_first->ptr_next; 
      else 
       ptr_prev->ptr_next=ptr_del->ptr_next; 
      free(ptr_del); 
      printf("Record #%d has been deleted.",rec_no); 
      getch(); 
      return ; 
     } 
     ptr_prev=ptr_del; 
    } 


} 
void SearchRecord(void) 
{ 
    struct student *ptr_search; 
    char temp[100]; 
    int roll,flag=0; 
    clrscr(); 
    printf("Enter roll no to search:\n"); 
    gets(temp); 
    roll=atoi(temp); 
    ptr_search=ptr_first; 
    for(;ptr_search!=(struct student*)NULL;ptr_search=ptr_search->ptr_next) 
    { 
     if(ptr_search->roll_no==roll) 
     { 
     flag=1; 
     printf("Result Found!\nName:\t%s\nRoll #:\t%d\n",ptr_search->name,ptr_search->roll_no); 
     getch(); 
     return; 
     } 
    } 
    if(flag==0) 
    printf("Record not found!\n"); 
    getch(); 

} 
void SortRecord(void) 
{ 
    struct student *out,*in,*dummy; 
    for(out=ptr_first;out!=(struct student*)NULL;out=out->ptr_next) 
    { 
     for(in=out->ptr_next;out->ptr_next!=(struct student*)NULL;in=in->ptr_next) 
     { 
      if(!(strcmpi(out->name,in->name))) 
      *dummy=*in; 
      *in=*out; 
      *out=*dummy; 
      dummy=in; 
      in=out; 
      out=dummy; 
     } 
    } 
    printf("Records have been successfully sorted."); 
} 
void PrintRecord(void) 
{ 
    printf("HEllo"); 
    getch(); 
    struct student *ptr_print; 
    ptr_print=ptr_first; 
    for(;ptr_print!=(struct student*)NULL;ptr_print=ptr_print->ptr_next) 
    { 
     printf("NAME:\t%s\nROLL#:\t%d\n",ptr_print->name,ptr_print->roll_no); 
     printf("Press Enter to display next record or space to exit"); 
     getch(); 
    } 


} 
+0

定义“不工作”。 – 2010-09-02 21:49:12

+0

它没有打印list.I添加了一个hello语句来检查函数调用是否有问题,但是它打印了hello消息。问题出在打印函数中的代码 – 2010-09-02 21:52:33

+0

愚蠢的问题:你知道你把一个'getch()'在打印功能中? – rano 2010-09-02 21:54:57

回答

2

打印功能是好的,但这种可疑:

if(ptr_first->ptr_next==(struct student*)NULL) 
{ 
    ptr_first=ptr_this=ptr_new; 
} 
else 
{ 
    ptr_this->ptr_next=ptr_new; 
    ptr_this=ptr_new; 
} 

我想说的条件应该读

if(ptr_first==NULL) 

不知道为什么你没有得到段错误在这个地方......

+0

我将NULL转换为结构的类型。 – 2010-09-02 22:26:26

+0

是的,这是不必要的(除了破碎的编译器/库)。你也可以用'0'来测试。然而,这个优点不在'=='的RHS中,而是在LHS中。 – jpalecek 2010-09-02 22:30:44

+0

工作:D谢谢:D – 2010-09-04 20:36:17

1

当您尝试添加消息时(ptr_first始终为空),您的代码应该会崩溃。如果你不打电话AddRecordPrintRecord什么也不输出 - ptr_first仍然为空并不奇怪。

+0

再次看到代码,它指向新添加的记录 – 2010-09-04 20:37:16