⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch23-16.c

📁 C语言程序设计上机指导与练习 冶金工业出版社 刘怀亮
💻 C
字号:
#define LEN sizeof(struct stud_node) 
#include<conio.h> 
#include<stdio.h> 
#include<string.h>
#include "stdlib.h" 
struct stud_record 
{ 
   char StudNo[6]; 
   char StudName[10]; 
   char StudSex; /*M---Male F---Female*/ 
   int StudAge; 
}; 
struct stud_node 
{ 
   struct stud_record stud_mem; 
   struct stud_node *next; 
}; 

/*Create Student Linear table*/ 
struct stud_node *create() 
{ 
   struct stud_node *head,*p,*q; 
   char vno[6],vname[10],vsex; 
   int vage; 
   head=NULL; 
   while(1) 
   { 
      printf("\nInput a record\n\nNo\tName\tSex\tAge\n\n");
      scanf("%s",vno); 
      getchar(); 
      if(strcmp(vno,"0")==0) /*when vno=="0" to exit*/
         break; 
      scanf("%s",vname); 
      getchar(); 
      scanf("%c%d",&vsex,&vage); 
      getchar();
      p=(struct stud_node *)malloc(LEN); /*allocate space to node p*/
      strcpy(p->stud_mem.StudNo,vno); 
      strcpy(p->stud_mem.StudName,vname); 
      p->stud_mem.StudSex=vsex; 
      p->stud_mem.StudAge=vage; 
      if(head==NULL) 
         head=p; 
      else 
         q->next=p; /*q is the previous node of p*/ 
      q=p; 
   } 
   if(head!=NULL) 
      q->next=NULL; /*the last node has no child*/
   return head; 
} 

/*Find a student and If Found then Delete the node*/ 
struct stud_node *delete(struct stud_node *head,char no[6]) 
{ 
   struct stud_node *p,*q; 
   p=head; 
   q=p; 
   while(p) 
   { 
      if(strcmp(p->stud_mem.StudNo,no)==0) /*delete the node*/ 
      { 
         if(p==head) /*delete the first node*/ 
            head=p->next; 
         else 
         { 
            if(p->next!=NULL) /*delete the middle node*/ 
               q->next=p->next; 
            else /*delete the last node*/
               q->next=NULL; 
         } 
         printf("\n\t\t%s\t%s\t%c\t%d\n",p->stud_mem.StudNo,p->stud_mem.StudName,
              p->stud_mem.StudSex,p->stud_mem.StudAge);
         free(p); 
         break; 
      } 
      q=p; 
      p=p->next; 
   } 
   return head; 
} 

/*Disp linear table content*/ 
void prn(struct stud_node *head) 
{ 
   struct stud_node *p; 
   int i=1; 
   p=head; 
   printf("\nRecord\tNo\tName\tSex\tAge\n"); 
   while(p) 
   { 
      printf("%3d\t%s\t%s\t%c\t%d\n",i,p->stud_mem.StudNo,p->stud_mem.StudName,
         p->stud_mem.StudSex,p->stud_mem.StudAge); 
      p=p->next; 
      i++; 
   } 
} 

/*main program here*/ 
void main() 
{ 
   struct stud_node *head; 
   char no[6]; 
   head=create(); 
   prn(head); 
   getch(); 
   printf("\nPlease input a studno to find:"); 
   gets(no); 
   head=delete(head,no); 
   prn(head); 
   getch();
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -