#include <stdio.h>
#include "string.h"
#include<stdlib.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
int tel;
struct student *next;
};
struct student *create()
{
struct student *p1,*p2,*head;
int num;
char name[20];
int tel;
int n=0;
head=NULL;
p1=p2=(struct student *)malloc(LEN);
printf("輸入學號,姓名和電話");
scanf("%d %s %d",&p1->num ,&p1->name ,&p1->tel);
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
printf("繼續輸入\n");
scanf("%d %s %d",&p1->num ,&p1->name ,&p1->tel);
}
p2->next=NULL;
return head;
}
void menu()
{
printf("通訊錄\n");
printf("1 查找\n");
printf("2 刪除\n");
printf("3 修改\n");
printf("4 增加\n");
printf("5 退出\n");
}
void printlist(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
{
do
{
printf("num:%d\tname:%s\ttel:%d\n",p->num,p->name,p->tel);
p=p->next;
}while(p!=NULL);
}
}
//void search(struct student *head)
void *delNODE(struct student *head)
{
int num;
printf("輸入你想刪除的序號");
scanf("%d",&num);
printf("delNODE\n");
struct student *p1,*p2;
if(head==NULL)
{
printf("the list is null\n");
}
else
{
p1=head;
while(p1->next!=NULL&&p1->num!=num)
{
p2=p1;
p1=p1->next;
}
if(p1->num==num)
{
if(p1==head&&p1->next!=NULL )
head=p1->next;
else
p2->next=p1->next;
}
else
printf("can not find list num\n");
}
return head;
}
void *update(struct student *head)
{
int num;
char name[20];
int tel;
printf("update\n");
printf("輸入想要改的序號");
scanf("%d",&num);
struct student *p;
if(head==NULL)
{
printf("the list is null\n");
}
else
p=head;
while(p->next!=NULL&&p->num!=num)
{
p=p->next;
}
if(p->num==num)
{
/* printf("學號");
scanf("%d",&p->num);
printf("姓名");
scanf("%s",&p->name);
printf("電話");
scanf("%d",&p->tel);*/
printf("學號");
scanf("%d",&p->num);
printf("姓名");
scanf("%s",&name);
strcpy(p->name,name);
printf("電話");
scanf("%d",&p->tel);
}
else
printf("can not find list index\n");
return head;
}
void *add(struct student *head)
{
int index,num,tel;
char name[20];
printf("add\n");
printf("在幾號後加入");
scanf("%d",&index);
struct student *p1,*p2,*p3,*p4;
if(head==NULL)
{
printf("the list is null\n");
}
else
{
p1=p2=head;
while(p1->next!=NULL&&p1->num!=index)
{
p1=p2->next;
p2=p1;
}
if(p1->num==index)
{
p3=(struct student *)malloc(LEN);
printf("學號");
scanf("%d",&p3->num);
printf("名字");
scanf("%s",&p3->name);
// strcpy(p3->name,name);
printf("電話");
scanf("%d",&p3->tel);
if(p2->next==NULL)
{
p2->next=p3;
p3->next=NULL;
}
else
{
p3->next=p2->next;
p2->next=p3;
}
}
else
printf("can not find list index\n");
}
return head;
}
struct student *head_insert(struct student *head)
{
int num,tel;
char name[20];
struct student *p1,*p2;
if(head=NULL)
{
printf("no");
}
else
{
p2=head;
p1=(struct student *)malloc(LEN);
printf("xuehao");
scanf("%d",&p1->num);
printf("name");
scanf("%s",&name);
strcpy(p1->name,name);
printf("tel");
scanf("%d",&p1->tel);
head=p1;
p1->next=p2;
}
return head;
}
int main()
{
menu();
struct student *head;
head=create();
while(1)
{
printf("選擇");
int b;
scanf("%d",&b);
switch(b)
{
case 1:
printlist(head);
break;
case 2:
head= delNODE(head);
break;
case 3:
update(head);
break;
case 4:
add(head);
break;
case 5:
return 0;
}
}
return 0;
}
這是學c語言之後第一次做的項目,開始的時候可以說有點手足無措,感覺無從下手。還好有同學的幫助,讓我理清了思路,,但也只能做出這種比較低級的,功能上有所欠缺的程序。前路漫漫......