单链表的建立、测长、打印、删除节点、插入节点、排序、逆转
来源:互联网 发布:离线数据大魔王 编辑:程序博客网 时间:2024/05/21 14:51
不多说,直接贴测试通过的代码(C++)。。。
// singlelist.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>// singlelist.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#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;while(cycle){cout<<"Please input the data:";cin>>x;if (x!=0){s=(node*)malloc(sizeof(node));s->data=x;//cout<<s->data;p->next=s;p=s;}elsecycle=0;}head=head->next;p->next=NULL;cout<<head->data;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<<"These"<<n<<"records are: "<<endl;p=head;while(p!=NULL){cout<<p->data;p=p->next;}cout<<endl;}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);}elsep2->next=p1->next;}elsecout<<num<<"could not been found!"<<endl;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 (p1==head) //插入到链表头{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 i=1;i<n;++i){p=head;for(int j=0;j<n-i;++j){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 _tmain(int argc, _TCHAR* argv[]){node *head;int del_num,insert_num;head=creat();cout<<"长度为:"<<length(head)<<endl;print(head);head=sort(head);print(head);cout<<"输入要插入的数字:";cin>>insert_num;head=insert(head,insert_num);print(head);cout<<"请输入要删除的数字:";cin>>del_num;head=del(head, del_num);print(head);head=reverse(head);print(head);return 0;}
0 0
- 单链表的建立、测长、打印、删除节点、插入节点、排序、逆转
- 单链表的建立、测长、打印、删除节点、插入节点
- 单链表的建立、测长、打印、删除节点、插入节点、排序、逆置操作。
- 单链表的建立,测长,打印,删除,插入,排序,逆置
- 单链表的建立,测长,打印,删除,插入,排序,逆置
- 单链表的建立,测长,打印,删除,插入,排序,逆置
- 链表:检测是否有环,是否相交,删除节点,插入节点,打印节点,建立节点,节点排序,节点逆序,
- 单链表的测长和打印和节点查找、插入、删除、逆置
- 链表的建立、删除节点、侧长和打印;
- 数据结构---单链表(建立,节点删除,节点插入)
- 数据结构---单链表(建立,节点删除,节点插入)
- 使用JAVA实现单链表的建立、测长、打印、删除、插入、排序、逆置
- 【数据结构】单链表的建立,测长,打印,删除,插入, 排序, 逆置
- 单链表的建立、测长、删除、插入、排序、逆置及打印(数据结构)
- 实现一个单链表的建立、测长、打印、删除、插入
- 单链表创建、测长度、打印、删除节点、插入节点、排序、逆置
- 单链表的创建,表长,插入,查找,逆置,中间元素,删除节点,打印
- 双链表的建立、测长、打印、删除、插入
- 4.4Bootstrap学习js插件篇之滚动监听
- 静态方法的体悟
- 黑马程序员_C#中几种比较字符串的方法比较
- 打印行号
- 任意石子合并_DP
- 单链表的建立、测长、打印、删除节点、插入节点、排序、逆转
- 为什么Java byte 类型的取值范围是-128~127
- 从汇编角度来理解linux下多层函数调用堆栈运行状态
- supre关键字
- mariadb安装
- 教育的目的
- ansj中引用自己训练的CRF模型
- 简单工厂模型
- 著名的图像检索系统