链表的创建、测长、排序、插入、逆序的实现(C语言)
2013-11-21 13:46
519 查看
#include <stdio.h>
#define END_elem 0
struct node
{
int date;
struct node * next;
};
//链表创建
node * creat()
{
int x;
node *head,*last,*p;
head=new node();
last=head;
scanf("%d",&x);
while(x!=END_elem)
{
p=new node();
p->date=x;
last->next=p;
last=p;
scanf("%d",&x);
}
head=head->next;
last->next=NULL;
return head;
}
//单链表测长
int lenght(node *head)
{
int n=0;
node *p=head;
while(p!=NULL)
{
p=p->next;
n++;
}
return n;
}
//链表排序
node *sort(node *head,int n)
{
int temp;
node *p;
if(head==NULL||head->next==NULL)
return head;
for(int i=0;i<n-1;i++)
{
p=head;
for(int j=0;j<n-1-i;j++)
{
if(p->date>p->next->date)
{
temp=p->date;
p->date=p->next->date;
p->next->date=temp;
}
p=p->next;
}
}
return head;
}
//链表插入
node * insert(node *head,int num)
{
node *p,*q,*t;
p=head;
t=new node();
t->date=num;
while(num>p->date&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(num<=p->date)
{
if(p==head)
{
t->next=p;
head=t;
}
else
{
t->next=p;
q->next=t;
}
}
else//把插入点定在表尾
{
p->next=t;
t->next=NULL;
}
return head;
}
//链表输出
void print(node *head)
{
struct node *p=head;
while(p!=NULL)
{
printf("%d\n",p->date);
p=p->next;
}
}
//链表删除
node *del(node *head,int num)
{
node *p,*q;
p=head;
while(num!=p->date&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->date==num)
{
if(p==head)
{
head=p->next;
delete p;
}
else
{
q->next=p->next;
delete p;
}
}
else
printf("Didn't Fond The Num!");
return head;
}
//链表逆序
node *reverse(node *head)
{
node *p1,*p2,*p3;
p1=head;
p2=head->next;
while(p2)
{
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
head->next=NULL;
head=p1;
return head;
}
void main()
{
struct node *head;
int n,m;
printf("链表创建,输入数字:\n");
head=creat();
print(head);
n=lenght(head);
printf("链表的长度为 %d\n",n);
printf("链表排序:\n");
head=sort(head,n);
print(head);
printf("输入要插入链表的数:\n");
scanf("%d",&m);
printf("插入后的链表:\n");
head=insert(head,m);
print(head);
printf("链表逆序:\n");
head=reverse(head);
print(head);
}
#define END_elem 0
struct node
{
int date;
struct node * next;
};
//链表创建
node * creat()
{
int x;
node *head,*last,*p;
head=new node();
last=head;
scanf("%d",&x);
while(x!=END_elem)
{
p=new node();
p->date=x;
last->next=p;
last=p;
scanf("%d",&x);
}
head=head->next;
last->next=NULL;
return head;
}
//单链表测长
int lenght(node *head)
{
int n=0;
node *p=head;
while(p!=NULL)
{
p=p->next;
n++;
}
return n;
}
//链表排序
node *sort(node *head,int n)
{
int temp;
node *p;
if(head==NULL||head->next==NULL)
return head;
for(int i=0;i<n-1;i++)
{
p=head;
for(int j=0;j<n-1-i;j++)
{
if(p->date>p->next->date)
{
temp=p->date;
p->date=p->next->date;
p->next->date=temp;
}
p=p->next;
}
}
return head;
}
//链表插入
node * insert(node *head,int num)
{
node *p,*q,*t;
p=head;
t=new node();
t->date=num;
while(num>p->date&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(num<=p->date)
{
if(p==head)
{
t->next=p;
head=t;
}
else
{
t->next=p;
q->next=t;
}
}
else//把插入点定在表尾
{
p->next=t;
t->next=NULL;
}
return head;
}
//链表输出
void print(node *head)
{
struct node *p=head;
while(p!=NULL)
{
printf("%d\n",p->date);
p=p->next;
}
}
//链表删除
node *del(node *head,int num)
{
node *p,*q;
p=head;
while(num!=p->date&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->date==num)
{
if(p==head)
{
head=p->next;
delete p;
}
else
{
q->next=p->next;
delete p;
}
}
else
printf("Didn't Fond The Num!");
return head;
}
//链表逆序
node *reverse(node *head)
{
node *p1,*p2,*p3;
p1=head;
p2=head->next;
while(p2)
{
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
head->next=NULL;
head=p1;
return head;
}
void main()
{
struct node *head;
int n,m;
printf("链表创建,输入数字:\n");
head=creat();
print(head);
n=lenght(head);
printf("链表的长度为 %d\n",n);
printf("链表排序:\n");
head=sort(head,n);
print(head);
printf("输入要插入链表的数:\n");
scanf("%d",&m);
printf("插入后的链表:\n");
head=insert(head,m);
print(head);
printf("链表逆序:\n");
head=reverse(head);
print(head);
}
相关文章推荐
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现
- C语言实现链表的创建,初始化,插入,删除,查找
- c语言:单链表的实现(一) 创建,插入,删除,查找
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- 【回忆c语言】从指针的定义初始化到链表的插入删除排序实现code
- C语言单向动态链表程序,实现链表的建立,合并,重新排序,链表元素的插入与删除,以及根据元素成员的值进行元素删除。
- C++链表的创建、插入、删除、查找、合并、排序、修改等操作的实现
- c语言实现单链表的操作:创建,删除,插入,反转, 排序等
- 链表的创建 查找 排序 插入 删除 逆序 长度 显示
- C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)
- c语言链表的创建、插入、删除、排序
- C语言实现链表的创建、增加、删除、查询、逆序、清空等基本操作
- C语言中,链表的创建,插入,删除,遍历,求链表长度,排序等
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)
- 链表基础2(实现链表的逆序、将新结点插入到特定位置、边插结点边排序)
- c语言实现直接插入排序(正序和逆序)
- 【数据结构】用C语言实现顺序表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)
- 循环链表的创建、插入、删除、逆序、显示(C++实现)