对链表的综合操作
2011-03-26 10:25
417 查看
/*
*对链表的综合操作
*功能有建立,排序,插入,删除,输出
*/
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct NodeType
{
ElemType data;
struct NodeType *next;
} NodeType,*LinkType;
LinkType create()
{//建立链表,返回链表的首地址,头结点没有数据
LinkType head,p1,p2;
head=(LinkType)malloc(sizeof(NodeType));
p1=head;
while(p1->data!=0)//当data=0时链表结束
{
p2=p1;
p1=(LinkType)malloc(sizeof(NodeType));
printf("Enter student's information:/ndata=");
scanf("%d",&p1->data);
p2->next=p1;
}
p2->next=NULL;
free(p1);
return(head);
}
void output(LinkType head)
{//链表的输出,接收链表的首地址
head=head->next;
while(head!=NULL)
{
printf("data=%d/n",head->data);
head=head->next;
}
}
LinkType sort(LinkType head)
{//链表排序,接收链表首地址,返回链表首地址
LinkType ph,p1;
ElemType temp;
ph=head->next;
p1=head->next;
while(p1->next!=NULL)//冒泡法
{
ph=head;
while(ph->next!=NULL)
{
if(ph->data>ph->next->data)//按data由小到大排序
{
temp=ph->data;
ph->data=ph->next->data;
ph->next->data=temp;
}
ph=ph->next;
}
p1=p1->next;
}
return(head);
}
LinkType del(LinkType head)
{//删除结点,接收链表的首地址,返回链表的首地址
ElemType DelData;
LinkType ph,p;
ph=head->next;
printf("Enter the data you want to del:/nDelData=");
scanf("%d",&DelData);
while(ph!=NULL && ph->data!=DelData)//寻找要删除的结点
{
p=ph;
ph=ph->next;
}
if(ph==NULL)//没有找到要删除的结点
{
printf("Enter error!/n");
return(head);
}
else
{
if(ph==head->next)//删除头结点
{
head->next=ph->next;
}
else//删除其它结点
{
p->next=ph->next;
}
}
free(ph);
return(head);
}
LinkType insert(LinkType head)
{//插入结点,接收链表首地址,返回链表首地址
LinkType ph,p,insert,temp;
insert=(LinkType)malloc(sizeof(NodeType));
printf("Enter the data you want to insert:/ndata=");
scanf("%d",&insert->data);
ph=head->next;
while(ph!=NULL && ph->data < insert->data)//寻找插入的位置
{
p=ph;
ph=ph->next;
}
if(head->next->data > insert->data)//插入头部
{
temp=head->next;
head->next=insert;
insert->next=temp;
}
else//插入到其它地方
{
p->next=insert;
insert->next=ph;
}
return(head);
}
void main()
{
LinkType head;
head=create();
output(head);
printf("/n/n");
head=sort(head);
output(head);
printf("/n/n");
head=del(head);
output(head);
printf("/n/n");
head=insert(head);
output(head);
}
*对链表的综合操作
*功能有建立,排序,插入,删除,输出
*/
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct NodeType
{
ElemType data;
struct NodeType *next;
} NodeType,*LinkType;
LinkType create()
{//建立链表,返回链表的首地址,头结点没有数据
LinkType head,p1,p2;
head=(LinkType)malloc(sizeof(NodeType));
p1=head;
while(p1->data!=0)//当data=0时链表结束
{
p2=p1;
p1=(LinkType)malloc(sizeof(NodeType));
printf("Enter student's information:/ndata=");
scanf("%d",&p1->data);
p2->next=p1;
}
p2->next=NULL;
free(p1);
return(head);
}
void output(LinkType head)
{//链表的输出,接收链表的首地址
head=head->next;
while(head!=NULL)
{
printf("data=%d/n",head->data);
head=head->next;
}
}
LinkType sort(LinkType head)
{//链表排序,接收链表首地址,返回链表首地址
LinkType ph,p1;
ElemType temp;
ph=head->next;
p1=head->next;
while(p1->next!=NULL)//冒泡法
{
ph=head;
while(ph->next!=NULL)
{
if(ph->data>ph->next->data)//按data由小到大排序
{
temp=ph->data;
ph->data=ph->next->data;
ph->next->data=temp;
}
ph=ph->next;
}
p1=p1->next;
}
return(head);
}
LinkType del(LinkType head)
{//删除结点,接收链表的首地址,返回链表的首地址
ElemType DelData;
LinkType ph,p;
ph=head->next;
printf("Enter the data you want to del:/nDelData=");
scanf("%d",&DelData);
while(ph!=NULL && ph->data!=DelData)//寻找要删除的结点
{
p=ph;
ph=ph->next;
}
if(ph==NULL)//没有找到要删除的结点
{
printf("Enter error!/n");
return(head);
}
else
{
if(ph==head->next)//删除头结点
{
head->next=ph->next;
}
else//删除其它结点
{
p->next=ph->next;
}
}
free(ph);
return(head);
}
LinkType insert(LinkType head)
{//插入结点,接收链表首地址,返回链表首地址
LinkType ph,p,insert,temp;
insert=(LinkType)malloc(sizeof(NodeType));
printf("Enter the data you want to insert:/ndata=");
scanf("%d",&insert->data);
ph=head->next;
while(ph!=NULL && ph->data < insert->data)//寻找插入的位置
{
p=ph;
ph=ph->next;
}
if(head->next->data > insert->data)//插入头部
{
temp=head->next;
head->next=insert;
insert->next=temp;
}
else//插入到其它地方
{
p->next=insert;
insert->next=ph;
}
return(head);
}
void main()
{
LinkType head;
head=create();
output(head);
printf("/n/n");
head=sort(head);
output(head);
printf("/n/n");
head=del(head);
output(head);
printf("/n/n");
head=insert(head);
output(head);
}
相关文章推荐
- 单链表综合操作
- l链表的综合操作
- 链表的综合操作(重新整理,模块一点)
- P305_1111链表的综合操作,建表、…
- 链表操作综合练习(创建、拆分、插入、删除、合并、打印、逆置)
- 单链表综合操作
- 对单向链表的综合操作
- 关于链表的综合操作
- tornado开发学习之2.输入输出,数据库操作,内置模板,综合示例
- 单链表的操作(cpp)
- 重学数据结构001——链表基本操作与一元多项式相加
- SPLAY树各种操作--链表
- c++实现链表的基本操作
- 链表的操作
- 带尾节点的循环链表基本操作
- C语言单链表常见操作汇总
- 实现链表基本操作(简单操作)
- 数据结构与算法:单链表(利用万能指针实现对任意类型数据进行操作)
- 2.简单链表操作_C语言
- 链表的循环带删除操作