當前位置:編程學習大全網 - 編程語言 - c語言編程題,程序功能: 建立壹個鏈表,每個結點包括:學號、姓名、性別、年齡,輸入壹個學號,如果鏈表中

c語言編程題,程序功能: 建立壹個鏈表,每個結點包括:學號、姓名、性別、年齡,輸入壹個學號,如果鏈表中

struct student *creat(int a)

{

struct student *head;

struct student *p1,*p2;

head=NULL;

for(n=0;n<a;n++) //這裏改成for

{

p1=(struct student*)malloc(LEN);

scanf("%ld %s %s %d",&p1->num,p1->name,p1->sex,&p1->age); //這裏改成空格才可以保證name sex正確輸入!!

if(n==0) head=p1;

else p2->next=p1;

p2=p1;

}

p1->next=NULL;

return(head);

}

int main()

{

struct student *p1,*head,*p2;

int n;

long t;

printf("please input the total number");

scanf("%d",&n);

head=p1=p2=creat(n);

printf("please input the num you want to delete");

scanf("%ld",&t);

//while(t!=p1->num&&p1!=NULL){ p2=p1;p1=p1->next;}

while( p1!=NULL && t!=p1->num){ p2=p1;p1=p1->next;} //要先判斷p1是否為空,再判斷數據是否相同,否則找不到數據時會出錯

if(p1==NULL) printf("the num you want to delete is not exist");

else

{

printf("%ld,\n%s,\n%s,\n%d\n",p1->num,p1->name,p1->sex,p1->age);

if(p1==head) head=p1->next;

/**以下兩句是廢話**

else if(p1->next==NULL) p2->next=NULL;

else p2->next=p1->next;

****/

p2->next=p1->next;

free(p1); //刪除了,就釋放掉這個結點

}

//顯示壹下 剩余的數據

printf("\n" );

p1=head;

while ( p1 )

{

printf("%ld, %s, %s, %d\n",p1->num,p1->name,p1->sex,p1->age);

p1=p1->next;

}

return 0;

}

  • 上一篇:cloud是什麽意思翻譯
  • 下一篇:網絡圍棋有哪些
  • copyright 2024編程學習大全網