您的位置:首页 > 编程语言 > C语言/C++

C语言单链表的建立,查找,添加,删除,修改功能实现

2010-03-29 16:40 986 查看
这个程序是我在初学单链表的时候编写出来的代码,所以还不完整,希望对各位在理解单链表的时候起到一点儿的帮助,而且欢迎各位同学讨论技术,以技术广交天下好友。本人QQ号:648422746

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#define len (struct staff *)malloc(sizeof(struct staff))
struct staff
{
char name[10];
int salary;
struct staff *next;
};
struct staff *creat();
void print(struct staff *);
struct staff *find(struct staff *);
struct staff *addmessage(struct staff *);
struct staff *deletemessage(struct staff *);
struct staff *changemessage(struct staff *);
main()
{
struct staff *head,*p;
head=creat();
print(head);
p=find(head);
head=addmessage(head);
head=deletemessage(head);
head=changemessage(head);
}

struct staff *creat()
{
struct staff *head,*p1,*p2;
int n;
n=0;
head=NULL;
p1=len;
printf("/n请键入学生的信息 (包括名字,分数):/n");
scanf("%s%d",p1->name,&p1->salary);
while(p1->salary>0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=len;
scanf("/n%s%d",p1->name,&p1->salary);

}
p2->next=NULL;
return head;
}
void print(struct staff *head)
{
struct staff *p;
p=head;

while(p!=NULL)
{
printf("/n%s%d",p->name,p->salary);
p=p->next;
}
}
struct staff *find(struct staff *head)
{
struct staff *p1;
char findname[10];
p1=head;
printf("/nplease input you find name:/n");
scanf("%s",findname);
while(p1!=NULL)
{
if(strcmp(findname,p1->name)==0)
{
printf("/n%s%d",p1->name,p1->salary);
return p1;
}
else
p1=p1->next;
}
if(p1=NULL)
printf("/n没有你要查找的学生的信息/n");
}
struct staff *addmessage(struct staff *head)
{
struct staff *p1,*p2;
p1=head;
p2=head;
p1=len;
printf("/n请输入你要添加的信息:/n");
scanf("/n%s%d",p1->name,&p1->salary);
if(head!=NULL)
{
p2=head;
p1->next=p2;
head=p1;
}
else
head=p1;
print(head);
return head;
}
struct staff *deletemessage(struct staff *head)
{
struct staff *p1,*p2;
char deletename[10];
p1=head;
p2=head;
printf("/n删除学生的信息,请键入该学生的姓名:/n");
scanf("%s",deletename);
while(strcmp(deletename,p1->name)!=0)
{
p2=p1;
p1=p1->next;
}
if(strcmp(head->name,p1->name)==0)
{
head=p1->next;
}
else if(strcmp(deletename,p1->name)==0)
{
p2->next=p1->next;
}
free(p1);
print(head);
return head;
}
struct staff *changemessage(struct staff *head)
{
struct staff *p;
char changename[10];
p=head;
printf("/n修改学生的信息,请键入该学生的名字:");
scanf("%s",changename);
while(strcmp(changename,p->name)!=0)
{
p=p->next;
}
printf("/n该学生的信息为:%s%d",p->name,p->salary);
printf("/n请重新键入该学生的信息包括名字,分数:");
scanf("%s%d",p->name,&p->salary);
printf("/n全部学生的信息如下:/n");
print(head);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐