?? 課題參考4.txt
字號:
#define NULL 0
#define TYPE struct stu
#define LEN sizeof(struct stu)
struct stu /* 定義學生信息對應的結構體類型*/
{
int num;
char name[12];
struct stu*next;
};
TYPE*creat(int n) /*建立鏈表的函數*/
{
struct stu*head,*pf,*pb;
int i;
for(i=0;i<n;i++)
{
pb=(TYPE*)malloc(LEN);
printf("請輸入第%d個學生的學號和姓名:",i+1);
scanf("%d%s",&pb->num,pb->name);
if(i==0)
pf=head=pb;
else
pf->next=pb;
pb->next=NULL;
pf=pb;
}
return(head);
}
TYPE*delete(TYPE*head,int num) /*刪除指定結點的函數*/
{
TYPE*pf,*pb;
if(head==NULL)
{
printf("\n沒有記錄!\n");
goto end;
}
pb=head;
while(pb->num!=num&&pb->next!=NULL)
{
pf=pb;
pb=pb->next;
}
if(pb->num==num)
{
if(pb==head)
head=pb->next;
else
pf->next=pb->next;
printf("記錄已經被刪除!\n");
}
else
{
free(pb);
printf("該記錄不存在!\n");
}
end:
return head;
}
TYPE*insert(TYPE*head,TYPE*pi) /*插入學生信息的函數*/
{
TYPE*pb,*pf;
pb=head;
if(head==NULL)
{
head=pi;
pi->next=NULL;
}
else
{
while((pi->num>pb->num)&&(pb->next!=NULL))
{
pf=pb;
pb=pb->next;
}
if(pi->num<=pb->num)
{
if(head==pb)
head=pi;
else
pf->next=pi;
pi->next=pb;
}
else
{
pb->next=pi;
pi->next=NULL;
}
}
return head;
}
void search(TYPE*head,int n)
{
TYPE*p;
int i;
p=head;
while(p->num!=n&&p->next!=NULL)
p=p->next;
if(p->num==n)
printf("該學生姓名為:%s",p->name);
if(p->num!=n&&p->next==NULL)
printf("該學生信息不存在!\n");
}
void print(TYPE*head) /*輸出所有學生信息*/
{
printf("\n學生信息表內容如下:\n");
printf("學號\t\t姓名\n");
while(head!=NULL)
{
printf("%d\t\t%s\n",head->num,head->name);
head=head->next;
}
}
main()
{
TYPE*head,*pnum;
int n,num,t;
printf(" \n_******************************\n");
printf(" \n 歡迎進入學生成績管理系統\n");
printf(" \n******************************\n");
printf("您要輸入多少條記錄?請輸入:");
scanf("%d",&n);
head=creat(n);
print(head);
printf("1.刪除 2.插入 3.查找\n請輸入您想進行的操作:");
scanf("%d",&t);
if(t==1)
{ printf("請輸入要刪除的學生信息所對應的學號:");
scanf("%d",&num);
head=delete(head,num);
print(head);
}
else if(t==2)
{
printf("請輸入要插入的學生信息的學號和姓名:");
pnum=(TYPE*)malloc(LEN);
scanf("%d%s",&pnum->num,pnum->name);
head=insert(head,pnum);
print(head);
}
else if(t==3)
{
printf("請輸入要查找的學生的學號:");
scanf("%d",&n);
search(head,n);
getch();
}
else
printf("輸入錯誤!");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -