单链表的常用操作,包括单链表的创建、插入、删除、排序、逆置以及打印输出等
来源:互联网 发布:局域网摄像头监控软件 编辑:程序博客网 时间:2024/06/06 14:21
#include<IOSTREAM>
using namespace std;
typedef struct Single_link
{
int data;
struct Single_link *next;
}node;
//单链表的创建
node *Create()
{
node *head,*p,*s;
int x,cycle=1;
head=(node *)malloc(sizeof(node));
p=head;
cout<<"Input the elements of the link:"<<endl;
while (cycle)
{
cin>>x;
if (x!=0)
{
s=(node *)malloc(sizeof(node));
s->data=x;
p->next=s;
p=s;
}
else
cycle=0;
}
head=head->next;
p->next=NULL;
return head;
}
//单链表测长
int length(node *head)
{
node *p;
p=head;
int count=0;
while (p!=NULL)
{
p=p->next;
count++;
}
return count;
}
//////////////////////////////////单链表插入///////////////////////////////////////
node *Insert(node *head,int element)
{
//p是遍历指针,q用于保存插入节点的位置节点,s是将要插入的节点
node *p,*q,*s;
p=head;
//创建插入的新节点
s=(node *)malloc(sizeof(node));
s->data=element;
while (s->data>p->data && p->next!=NULL)
{
q=p;
p=p->next;
}
if (s->data<=p->data)
{
if (head==p)
{
s->next=p;
head=s;
}
else
{
q->next=s;
s->next=p;
}
}
else
{
p->next=s;
s->next=NULL;
}
return head;
}
/////////////////////////////单链表删除/////////////////////////////////////////////
node *del(node *head,int element)
{
//p是遍历指针,q用于保存删除的位置节点
node *p,*q;
p=head;
while (p->data!=element && p->next!=NULL)
{
q=p;
p=p->next;
}
if (p->data==element)
{
if (head==p)
{
head=p->next;
free(p);
}
else
{
q->next=p->next;
}
}
else
{
cout<<" Not find the delete element."<<endl;
}
return head;
}
///////////////////////////////单链表逆序///////////////////////////////////////////
node *Reverse(node *head)
{
node *p,*q,*s;
if (head==NULL && head->next==NULL)
{
return head;
}
p=head;
q=p->next;
while (q)
{
s=q->next;
q->next=p;
p=q;
q=s;
}
head->next=NULL;
head=p;
return head;
}
///////////////////////////////单链表排序///////////////////////////////////////////
node *Sort(node *head)
{
node *p;
int n;
int temp;
n=length(head);
if(head==NULL && head->next==NULL)
{
return head;
}
p=head;
for (int j=1;j<n;j++)
{
p=head;
for (int i=0;i<n-j;i++)
{
if (p->data > p->next->data)
{
temp=p->data;
p->data=p->next->data;
p->next->data=temp;
}
p=p->next;
}
}
return head;
}
////////////////////////打印单链表///////////////////////////////////////////////////////////
void print(node *head)
{
node *p;
p=head;
int n=length(head);
cout<<"链表中共有"<<n<<"记录."<<endl;
if (head!=NULL)
{
cout<<"链表中元素为:"<<endl;
while (p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
}
int main()
{
node *head;
int n;
head=Create();
print(head);
head=Sort(head);
print(head);
cout<<"Input the insert number:"<<endl;
cin>>n;
Insert(head,n);
print(head);
cout<<"Input the delete number:"<<endl;
cin>>n;
del(head,n);
print(head);
head=Reverse(head);
print(head);
return 0;
}
using namespace std;
typedef struct Single_link
{
int data;
struct Single_link *next;
}node;
//单链表的创建
node *Create()
{
node *head,*p,*s;
int x,cycle=1;
head=(node *)malloc(sizeof(node));
p=head;
cout<<"Input the elements of the link:"<<endl;
while (cycle)
{
cin>>x;
if (x!=0)
{
s=(node *)malloc(sizeof(node));
s->data=x;
p->next=s;
p=s;
}
else
cycle=0;
}
head=head->next;
p->next=NULL;
return head;
}
//单链表测长
int length(node *head)
{
node *p;
p=head;
int count=0;
while (p!=NULL)
{
p=p->next;
count++;
}
return count;
}
//////////////////////////////////单链表插入///////////////////////////////////////
node *Insert(node *head,int element)
{
//p是遍历指针,q用于保存插入节点的位置节点,s是将要插入的节点
node *p,*q,*s;
p=head;
//创建插入的新节点
s=(node *)malloc(sizeof(node));
s->data=element;
while (s->data>p->data && p->next!=NULL)
{
q=p;
p=p->next;
}
if (s->data<=p->data)
{
if (head==p)
{
s->next=p;
head=s;
}
else
{
q->next=s;
s->next=p;
}
}
else
{
p->next=s;
s->next=NULL;
}
return head;
}
/////////////////////////////单链表删除/////////////////////////////////////////////
node *del(node *head,int element)
{
//p是遍历指针,q用于保存删除的位置节点
node *p,*q;
p=head;
while (p->data!=element && p->next!=NULL)
{
q=p;
p=p->next;
}
if (p->data==element)
{
if (head==p)
{
head=p->next;
free(p);
}
else
{
q->next=p->next;
}
}
else
{
cout<<" Not find the delete element."<<endl;
}
return head;
}
///////////////////////////////单链表逆序///////////////////////////////////////////
node *Reverse(node *head)
{
node *p,*q,*s;
if (head==NULL && head->next==NULL)
{
return head;
}
p=head;
q=p->next;
while (q)
{
s=q->next;
q->next=p;
p=q;
q=s;
}
head->next=NULL;
head=p;
return head;
}
///////////////////////////////单链表排序///////////////////////////////////////////
node *Sort(node *head)
{
node *p;
int n;
int temp;
n=length(head);
if(head==NULL && head->next==NULL)
{
return head;
}
p=head;
for (int j=1;j<n;j++)
{
p=head;
for (int i=0;i<n-j;i++)
{
if (p->data > p->next->data)
{
temp=p->data;
p->data=p->next->data;
p->next->data=temp;
}
p=p->next;
}
}
return head;
}
////////////////////////打印单链表///////////////////////////////////////////////////////////
void print(node *head)
{
node *p;
p=head;
int n=length(head);
cout<<"链表中共有"<<n<<"记录."<<endl;
if (head!=NULL)
{
cout<<"链表中元素为:"<<endl;
while (p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
}
int main()
{
node *head;
int n;
head=Create();
print(head);
head=Sort(head);
print(head);
cout<<"Input the insert number:"<<endl;
cin>>n;
Insert(head,n);
print(head);
cout<<"Input the delete number:"<<endl;
cin>>n;
del(head,n);
print(head);
head=Reverse(head);
print(head);
return 0;
}
0 0
- 单链表的常用操作,包括单链表的创建、插入、删除、排序、逆置以及打印输出等
- 单链表的创建,删除,插入以及打印。
- 单链表的创建、插入、删除、排序以及逆置
- 单链表的创建、插入、删除等操作
- 单链表的创建,插入删除等操作
- 单链表的创建、插入删除等操作
- 数据结构——单链表的创建、删除、遍历以及节点的插入、删除等操作
- 单链表的创建、测长、打印、插入、删除、排序及逆置
- C++单链表的操作(创建,删除,打印,遍历,插入)
- 双循环链表的创建以及插入删除等操作
- 单链表操作,创建,遍历,插入,删除,排序等操作
- c语言实现单链表的操作:创建,删除,插入,反转, 排序等
- 单链表的建立、测长、打印、删除节点、插入节点、排序、逆置操作。
- 单链表的创建、插入,删除、查找等操作
- 线性表的基本操作,包括:创建、插入、删除、查找等基本操作
- C++单链表的创建插入删除以及逆序操作
- 带头结点单链表的基本操作(创建、测长、打印、插入、删除、取值、合并、排序、逆置)
- 单链表的创建, 删除, 插入, 输出
- 目前最细致清晰的NSDictionary以及NSMutableDictionary用法总结
- NSString基本常用方法
- ANR问题: /data/anr/traces.txt
- [贪心+暴力矫正] zoj 3685 Cube
- hdu 2550
- 单链表的常用操作,包括单链表的创建、插入、删除、排序、逆置以及打印输出等
- 虚拟机上网并ssh连接的技巧
- Eclipse下及开发过程中常用设置总结(欢迎补充)
- Word.ApplicationClass application = null; 无法嵌入互操作类型,请改用实用的接口
- 3. 字符数组和字符指针
- KeyPress 和KeyDown 、KeyPress之间的区别
- photoshop查看直方图的方法
- addEventListener注意问题
- 如何编译 libruby.so