单链表的基本操作:建立,求长度,输出,排序,插入,删除,逆置
2014-08-31 23:14
676 查看
程实现一个单链表的建立,求单链表的长度,打印输出单链表,对单链表进行排序,插入元素,删除元素,对单链表进行逆置。
我是借鉴参考资料,然后自己写规范,对函数都进行了调用,每一次调用,都有输出单链表。程序完整,已调试运行。
源程序:
运行结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201408/f5bbc383f52558a99eb76743c0aadc1e.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201408/4c38604c4ac4e887a21d60d1cbe4b18b.jpg)
总结:单链表的基本操作,包括建立单链表,对单链表求长度,打印输出单链表,对单链表进行排序,删除元素,插入元素,对单链表逆置。这些操作都是程序员必须掌握的,虽然它是一种相对简单的数据结构,但是能够做到熟练掌握运用也是需要花时间的。
我是借鉴参考资料,然后自己写规范,对函数都进行了调用,每一次调用,都有输出单链表。程序完整,已调试运行。
源程序:
#include<iostream> #include<stdio.h> #include<string.h> #include<conio.h> using namespace std; typedef struct student { int data; struct student *next; }node; //建立单链表 node *creat() { node *head,*p,*s; int x,cycle=1; head=(node*)malloc(sizeof(node)); p=head; cout<<"请输入单链表的数据,以0作为结束标识:"<<endl; while(cycle) { cin>>x; if(x!=0)//以0为标识 { s=(node *)malloc(sizeof(node)); s->data=x; p->next=s; p=s; } else cycle=0; } head=head->next; p->next=NULL; cout<<endl; return (head); } //求单链表的长度 int length(node *head) { int n=0; node *p; p=head; while(p!=NULL) { p=p->next; n++; } return(n); } //输出单链表 void print(node *head) { node *p;int n; n=length(head); cout<<"现在,长度为"<<n<<"的单链表更新为:"<<endl; p=head; if(head!=NULL) { while(p!=NULL) { cout<<p->data<<'\t'; p=p->next; } } printf("\n"); } //删除单链表中的某个元素 node *del(node *head,int num) { node *p1,*p2; p1=head; while(num!=p1->data&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(num==p1->data) { if(p1==head) { head=p1->next; free(p1); } else p2->next=p1->next; } else cout<<num<<"没有在单链表中找到!"<<endl;//printf("\n%d 没有在单链表中找到!",num); return head; } //在单链表中插入一个元素 node *insert(node *head,int num) { node *p0,*p1,*p2; p1=head; p0=(node *)malloc(sizeof(node)); p0->data=num; while(p0->data>p1->data&&p1->next!=NULL) { p2=p1;p1=p1->next; } if(p0->data<=p1->data) { if(head==p1) { p0->next=p1; head=p0; } else { p2->next=p0; p0->next=p1; } } else { p1->next=p0; p0->next=NULL; } 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; } //对单链表进行逆置 node *reverse(node *head) { node *p1,*p2,*p3; if(head==NULL||head->next==NULL) return head; p1=head,p2=p1->next; while(p2) { p3=p2->next; p2->next=p1; p1=p2; p2=p3; } head->next=NULL; head=p1; return head; } int main() { node *head; int n,del_num,insert_num; head=creat(); print(head); cout<<"\n单链表的长度: "; n=length(head); cout<<n<<endl; cout<<"\n请对单链表排序:"; head=sort(head); print(head); cout<<"\n请输入单链表中要删除的数据: "; cin>>del_num; head=del(head,del_num); print(head); cout<<"\n请输入单链表中要插入的数据:"; cin>>insert_num; head=insert(head,insert_num); print(head); cout<<"\n请对单链表进行逆置:"; head=reverse(head); print(head); return 0; }
运行结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201408/f5bbc383f52558a99eb76743c0aadc1e.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201408/4c38604c4ac4e887a21d60d1cbe4b18b.jpg)
总结:单链表的基本操作,包括建立单链表,对单链表求长度,打印输出单链表,对单链表进行排序,删除元素,插入元素,对单链表逆置。这些操作都是程序员必须掌握的,虽然它是一种相对简单的数据结构,但是能够做到熟练掌握运用也是需要花时间的。
相关文章推荐
- 程序员面试宝典(第三版)——单链表的基本操作:建立,求长度,输出,排序,插入,删除,逆置(转)
- 程序员面试宝典(第三版)——单链表的基本操作:建立,求长度,输出,排序,插入,删除,逆置
- 链表的基本操作(插入,删除,排序、逆置等)
- 数据结构链表的操作集合(建立,遍历,插入,删除,排序,长度,空判断等)
- 链表的基本操作(C语言版):建立,插入,删除,查找,输出
- 单链表的基本操作,正序/逆序建表(尾插法/头插法)、计算单链表长度、输出单链表内容、插入、删除,给出完整代码
- 单链表的建立,测长度,打印,删除,插入,逆置c++代码实例及运行结果
- 单链表的建立、测长、删除、插入、排序、逆置及打印(数据结构)
- 关于动态存储分配函数的调用,在已经过排序的数组中查找及删除内容的操作,余数的分析,删除字符数组中的空格,对链表的逆置,在源字符串中查找子字符串的个数,函数指针以及函数的调用,循环赋值带来的问题以及插入
- 带头结点单链表的基本操作(创建、测长、打印、插入、删除、取值、合并、排序、逆置)
- 单链表的建立,测长度,打印,删除,插入,逆置c++代码实例及运行结果
- 链表的基本操作之插入、删除、逆置
- 将一个值插入到有序的单链表,实现插入、删除、输出等基本操作
- 链表的基本操作,建立,测长,删除,打印,插入
- 链表基本操作(建立、修改,插入、删除、打印)
- java语言编写链表的基本操作(链表的创建,插入,删除,打印,排序)
- 单链表的建立,测长度,打印,删除,插入,逆置c++代码实例及运行结果
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- 程序员面试宝典_链表基本操作,建立,求长,删除和插入特定值的结点
- 单链表的建立,测长,打印,删除,插入,排序,逆置