实现一个单向链表的:创建、插入、删除、排序(冒泡)、逆向、搜索中间节点

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std; typedef struct student
{
int data;
struct student *next;
} node; //创建链表
node *create()
{
//1. 定义变量
node *head = NULL;
node *p = NULL;
node *pnew = NULL; int x = ;
int cycle = ; //2. 新建头节点
head = (node*)malloc(sizeof(node));
p = head; //3. 添加新节点
while (cycle)
{
printf("input data:");
scanf("%d", &x);
if (x != )
{
pnew = (node*)malloc(sizeof(node));
pnew->data = x;
p->next = pnew;
p = pnew;
}
else
{
cycle = ;
}
} //4. 释放头节点
p->next = NULL;
p = head;
head = head->next;
free(p);
p = NULL; //5. 返回链表
return head;
} //计算链表长度
int length(node *head)
{
//1. 定义变量
node *p = NULL;
int n = ; //2. 遍历累加
p = head;
while (p != NULL)
{
p = p->next;
n++;
}
printf("%d\n", n); //3. 返回计数
return n;
} //显示
void show(node *head)
{
//1. 定义变量
node *p = NULL; //2. 遍历打印
p = head; while(p != NULL)
{
printf("data:%d ", p->data);
p = p->next;
}
printf("\n"); } //插入节点(升序)
node *insert (node *head, int num)
{
//1. 定义变量
node *p0 = NULL;
node *p1 = NULL;
node *p2 = NULL; //2. 新建节点
p0 = (node*)malloc(sizeof(node));
p0->data = num; //3. 定位插入位置(升序)
p1 = head;
while (p0->data > p1->data && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
} //4. 插入节点
if (p0->data > p1->data) //末尾
{
p1->next = p0;
p0->next = NULL;
}
else
{ if (head == p1) //头
{
p0->next = p1;
head = p0;
}
else //中间
{
p2->next = p0;
p0->next = p1;
}
} //5. 返回头
return head;
} //删除链表中指定节点
node *del(node *head, int num)
{
//1. 定义变量
node *p1 = NULL;
node *p2 = NULL; //2. 定位删除位置
p1 = head;
while (num != p1->data && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
} //3. 删除节点
if (num != p1->data)
{
printf("not found data to delete\n");
}
else
{
if(p1 == head)
{
head = p1->next;
free(p1);
p1 = NULL;
}
else
{
p2->next = p1->next;
free(p1);
p1 = NULL;
}
} //4. 返回头
return head; } //链表升序排序(冒泡算法)
node *sort(node *head)
{
//1. 定义变量
node *p = NULL;
int n = ;
int temp = ; if (head == NULL || head->next == NULL)
{
return head;
} //2. 获取链表长度
n = length(head); //3. 排序
for (int j=; j<n; ++j) //遍历所有节点
{
p = head;
for(int i=; 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;
}
} //4. 返回头
return head; } //链表逆置
node *reverse(node *head)
{
//1. 定义变量
node *p1 = NULL;
node *p2 = NULL;
node *p3 = NULL; if (head == NULL || head->next == NULL)
{
return head;
} //2. 逆置
p1 = head;
p2 = p1->next;
while(p2 != NULL)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
} //3. 调换头尾节点
head->next = NULL; //转置完后头节点成为尾节点
head = p1; //转置完后尾节点成为头节点 //4. 返回头
return head; } //搜索链表中间节点
//算法:以步长2和1单位同时遍历链表,步长2到末尾,步长1到中间
void searchmid(node *head, node *mid)
{
//1. 定义变量
node *p1 = NULL;
node *p2 = NULL; //2. 定位中间节点
p1 = head;
p2 = head;
while (p2->next != NULL && p2->next->next != NULL)
{
p1 = p1->next;
mid = p1;
p2 = p2->next->next;
} printf("mid:%d\n", mid->data);
} int main()
{
node *head = create();
int len = length(head);
show(head); head = insert(head, );
show(head); head = del(head, );
show(head); head = sort(head);
show(head); head = reverse(head);
show(head); node *mid;
searchmid(head, mid);
}

c实现单向链表的更多相关文章

  1. Reverse Linked List II 单向链表逆序(部分逆序)

    0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...

  2. 【编程题目】输入一个单向链表,输出该链表中倒数第 k 个结点

    第 13 题(链表):题目:输入一个单向链表,输出该链表中倒数第 k 个结点.链表的倒数第 0 个结点为链表的尾指针.链表结点定义如下: struct ListNode {int m_nKey;Lis ...

  3. 输出单向链表中倒数第k个结点

    描述 输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第0个结点为链表的尾指针. 链表结点定义如下: struct ListNode { int       m_nKey; ListNode* ...

  4. Linus:利用二级指针删除单向链表

    Linus大神在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是他所喜好的,大婶表述了自己一些观点之后,举了一个指针的例子,解释了什么才是core low-level codi ...

  5. 【转】Linus:利用二级指针删除单向链表

    原文作者:陈皓 原文链接:http://coolshell.cn/articles/8990.html 感谢网友full_of_bull投递此文(注:此文最初发表在这个这里,我对原文后半段修改了许多, ...

  6. C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)

    #include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...

  7. 数据结构——Java实现单向链表

    结点类: /** * @author zhengbinMac * 一个OnelinkNode类的对象只表示链表中的一个结点,通过成员变量next的自引用方式实现线性表中各数据元素的逻辑关系. */ p ...

  8. 输入一个单向链表,输出该链表中倒数第K个结点

    输入一个单向链表,输出该链表中倒数第K个结点,具体实现如下: #include <iostream> using namespace std; struct LinkNode { publ ...

  9. 单向链表JAVA代码

        //单向链表类 publicclassLinkList{       //结点类     publicclassNode{         publicObject data;         ...

  10. C++ 单向链表反转

    单向链表反转,一道常见的面试题,动手实现下. #include "stdafx.h" #include <stdlib.h> struct Node{ int data ...

随机推荐

  1. pause的作用

    重要概念:Pod内的容器都是平等的关系,共享Network Namespace.共享文件 pause容器的最主要的作用:创建共享的网络名称空间,以便于其它容器以平等的关系加入此网络名称空间 pause ...

  2. atomikos 优化JDBC性能

    JDBC performance comes for free with our pooling DataSource classes: AtomikosDataSourceBean for XA-e ...

  3. Laravel with 查询指定的字段(非复制的哦)

    问题: 在with里面指定查询字段,结果是null. 在模型里面指定查询字段,结果是null. 解决办法: 在查询指定字段的时候要顺带着查询关联的外键,例: // user 表 id name // ...

  4. AngularJS入门教程之数据绑定原理详解

    这篇文章主要是写给新手的,是给那些刚刚开始接触Angular,并且想了解数据帮定是如何工作的人.如果你已经对Angular比较了解了,那强烈建议你直接去阅读源代码. Angular用户都想知道数据绑定 ...

  5. 运动的border,仿当当简易效果

    突然想到以前看到当当上有个效果,当鼠标移上去,图片边框是运动添加上的,还以为是css3或者是canvas做的呢,做完幽灵按钮后,才知道,so  easy,只不过是animate+position的杰作 ...

  6. ASE19团队项目 beta阶段 model组 scrum4 记录

    本次会议于12月5日,19时30分在微软北京西二号楼sky garden召开,持续10分钟. 与会人员:Lei Chai, Linfeng Qi, Xueqing Wu, Yutong Ling (Z ...

  7. exp/imp 数据库数据导出/导入

    一.exp数据导出 1.导出全部数据 exp 用户名/密码@服务名 file=文件存储路径/xxx.dmp log=日志存储路径/xxx.log full=y 例: [oracle@dbservice ...

  8. java_数据类型转换

    一.自动转换 目的类型比原来的类型要大,两种数据类型是相互兼容的. byte--->short short--->int char--->int int--->long/dou ...

  9. 用.htaccess 禁止IP访问

    用.htaccess 禁止某IP访问 Order Allow,Deny Allow from all Deny from 1.1.1.1 2.2.2.2 3.3.3.3 允许所有,禁止xxxx,请将里 ...

  10. 什么是OAuth授权

    什么是OAuth授权?   一.什么是OAuth协议 OAuth(开放授权)是一个开放标准. 允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息. 而这种授权无需将用户提供用户名和密 ...