2013-08-21 44 views
-3

需要幫助寫出正確的代碼plz q:編寫一個程序,它組織一個簡單的學生信息數據庫。鏈接列表:學生數據庫>>需要幫助

程序應該允許用戶輸入一系列學生數據記錄。每個學生數據記錄都有三個字段:名字,姓氏和年齡。用戶應能夠執行以下4個操作:1)輸入記錄,2)刪除記錄,3)打印所有記錄,4)對所有記錄進行分類。所有4個選項應在開始時打印。用戶應該收到提示,他/她可以輸入與他想要執行的操作相對應的號碼。輸入記錄操作將提示用戶在同一行中鍵入三個字符串,每個字段一個字符串。打印所有記錄操作應在不同的行上打印每條記錄。排序所有記錄操作應根據姓氏進行排序。

我寫的代碼是:(DISPLY並添加在工作,但刪除和排序都沒有!)

 #include<stdio.h> 
    #include<stdlib.h> 
    #include<malloc.h> 


    struct student 
     { 
     char fname[29]; 
     char lname[29]; 
     int age; 
     struct student *next; 
     }; 

     int main() 
     { 
     int i,n,ch,ps,x,k; 
     k=0; 
     struct student *h,*t,*t1,*w,*q; 
     h=NULL; 

     printf("\n/* student data records*/"); 


    while(1) 
    { 
      printf("\n1.display\n2:add\n3.delete\n4.exit\n5.sort by first name\n"); 
      printf("\nenter your choice="); 
      scanf("%d",&ch); 
      switch(ch) 
    { 
    case 1: 
     if(h==NULL) 
     { 
     printf("no records are available"); 
     } 
     w=h; 
     while(w!=NULL) 
    { 
     printf("\nfirst name of a student:%s\nlast name of a student:%s\nage of a student:%d\n", 
     w->fname,w->lname,w->age); 
     w=w->next; 
    } 
    break; 

    case 2: 
     printf("\nenter the new record=\t"); 
     if(h==NULL) 
    { 
     h=t=(struct student *)malloc(sizeof(struct student)); 
     printf("\nfirst Name of a student:\t"); 
     scanf("%s",&t->fname); 
     printf("\nlast Name of a student:\t"); 
     scanf("%s",&t->lname); 
     printf("\nage of a student:\t"); 
     scanf("%d",&t->age); 
     t->next=NULL; 
    break; 
    } 
    else 
     { 
     t1=(struct student *)malloc(sizeof(struct student)); 
     printf("\nFirst Name of a student:\t"); 
     scanf("%s",&t1->fname); 
     printf("\nlast Name of a student:\t"); 
     scanf("%s",&t1->lname); 
     printf("\nage of a student:\t"); 
     scanf("%d",&t1->age); 
     t1->next=t->next; 
     t->next=t1; 
     t=t1; 
    } 
    break; 

    case 3: 
     printf("enter name of student whos record is to be deleted=\n"); 
     scanf("%d",&ps); 
     t=h; 
    while(t->fname!=ps-1) 
     { 
     t=t->next; 
     } 
     t1=t->next; 
     t->next=t1->next; 
     free(t1); 
    break; 

    case 4: 
    exit(0); 
    break; 

    case 5: 
    printf("not working yet"); 
    { 
    void sort(student[],int n) 
    { int i,j,comp=0,passes=0; 
     student temp; 
     for(i=1;i<n;i++) 
     { 
      passes++; 
      for(j=0;j<n-i;j++) 
     {  comp++; 
       if(st[j].lname < st[j+1].lname) 
       { temp=st[j]; 
        st[j]=st[j+1]; 
        st[j+1]=temp; 
       } 
     } 
     } 
    } 
    } 
    break; 
    } 
    } 
    } 

回答

0

好了,萬一5,你使用學生作爲一個數組的名字(或者可能是沒有名字的數組的類型?)AND作爲temp的類型;那麼你從來不使用學生,但使用st,這似乎並沒有在任何地方定義。我很驚訝,這編譯...

首先猜測解決方案:也許你的意思是排序()的參數是student st[]

第二個猜測:它不會編譯,而且您實際上還沒有嘗試寫入鏈接列表的排序代碼。

嘗試這種方法:首先讓刪除工作,但寫它,以便它可選地返回「已刪除」的記錄,而不是釋放它。然後,您可以通過建立一個新列表來替換舊列表,方法是重複查找並不完全刪除舊列表中剩下的最大元素,並將其添加到新列表中,直到舊列表耗盡。

至於刪除不起作用,您沒有提供任何詳細信息,但我注意到h永不改變,並且您認爲搜索到的名稱將被找到。

+0

我試着按字母順序排序鏈表,但我不能,所以我想也許排序數組fname []可以做這項工作和排序的名稱,但沒有幫助:(如果你知道如何排序名稱使用鏈表,你可以編寫代碼或正確的我的?非常感謝 –