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);
}
#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);
}
相关文章推荐
- 用c语言实现一个通讯录; 通讯录可以存储1000个人的信息,信息包括姓名、性别、年龄、电话、住址 提供方法: 1.添加联系人信息 2.删除指定联系人信息 3.查找指定联系人信息 4.修改指定联系&
- 同学录--利用二叉树存储结构实现建立、查找、新增、修改、删除等功能
- 用c语言实现 一个通讯录(实现 增加、删除、查找、修改、显示、清空功能)
- 死亡历险,翻开新的一篇之Delphi 连接 JAVA WebService,并实现查找,添加,修改,删除等功能
- C语言:动态链表的建立,查找,删除,插入功能的实现
- 用C语言模拟实现一个通讯录,要求实现其添加、删除、修改、查找、显示和排序联系人信息的功能
- JavaScript数组,JSON对象实现动态添加、修改、删除功能示例
- 【通讯录系统】:实现一个通讯录(可添加、删除、修改、查找、显示、清空、排序)
- mybatis学习笔记-实现添加修改删除用户功能
- 平衡二叉树的 插入 删除 查找 等功能c语言实现 数据结构
- lucene功能四;索引库的维护;索引库的添加;修改;删除;及代码实现;代码示例
- 创建一个学生系统,实现一下功能; * 1:增加学生 * 2:显示学生 * 3:修改学生 * 4:删除学生 * 5:查找学生 * 6:退出程序 * 请输入选择(
- c语言实现对传统单链表的创建、添加 遍历 删除 反转元素操作
- [Extjs6]随记 创建store 实现添加、修改、删除功能
- javascript实现二叉树的创建,遍历,添加,查找最大值最小值和指定值的寻找及删除功能——递归的多次运用
- 单链表的功能使用:初始化,赋值,输出,删除,按值查找,按地址查找,数据修改
- java实现单链表的初始化,创建,删除,插入,查找,排序,同项删除,退出等功能
- 利用DhtmlXtree实现展现,修改,添加,删除,移动功能一棵树上实现,iframe的单个滑动条显示,包含在iframe中树节点中文内容过长问题解决
- c语言实现线性表的建立,初始化,插入,删除,查找,遍历以及时间复杂度分析
- 单链表的初步实现---(转置,删除,替换,查找,添加)