算法及数据结构之---链表
来源:互联网 发布:淘宝 身份证 重新开店 编辑:程序博客网 时间:2024/05/22 00:32
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int data;
- struct node* pNext;
- }Node;
- //链表的操作,以有头节点为例,无头节点类似
- Node* head = NULL;
- //创建链表,头结点data=0,pNext=NULL;
- bool createNodeList()
- {
- head = (Node*) malloc(sizeof(Node));
- if(NULL == head)
- {
- return false;
- }
- else
- {
- head->data = 0;
- head->pNext = NULL;
- return true;
- }
- }
- //增加节点
- bool addNode(Node* node)
- {
- if(NULL == head)
- {
- return false;
- }
- Node* p = head->pNext;
- Node* q = head;
- while(NULL != p)
- {
- q = p;
- p = p->pNext;
- }
- q->pNext = node;
- node->pNext = NULL;
- return true;
- }
- //删除节点
- bool deleteNode(int index)
- {
- if(NULL == head)
- {
- return false;
- }
- Node* p = head->pNext;
- int length = 0;
- while(NULL != p)
- {
- length ++;
- p = p->pNext;
- }
- if(length < index)
- {
- return false;
- }
- else
- {
- Node* q = head;
- p = head;
- for(int i=0;i<index;i++)
- {
- q = p;
- p = p->pNext;
- }
- Node* t = p->pNext;
- q->pNext = t;
- free(p);
- return true;
- }
- }
- //逆序
- void reverseNodeList()
- {
- if(NULL == head)
- {
- return;
- }
- //如果链表长度为1
- if(head->pNext == NULL)
- {
- return;
- }
- Node* p = head->pNext;
- Node* q = p->pNext;
- Node* t = NULL;
- while(NULL != q)
- {
- t = q->pNext;
- q->pNext = p;
- p = q;
- q = t;
- }
- head->pNext->pNext = NULL;
- head->pNext = p;
- }
- //排序(降序)
- void sort()
- {
- //冒泡排序
- Node* pHead = head;
- if(head == NULL)
- {
- return;
- }
- if(pHead->pNext == NULL)
- {
- return;
- }
- Node* pi = pHead->pNext;
- Node* pj = pi->pNext;
- for(;pi != NULL;pi=pi->pNext)
- {
- for(pj = pi->pNext;pj != NULL;pj=pj->pNext)
- {
- if(pj->data>pi->data)
- {
- int tmp = pj->data;
- pj->data = pi->data;
- pi->data = tmp;
- }
- }
- }
- }
- //销毁
- void destroyNodeList()
- {
- if(NULL == head)
- {
- return;
- }
- if(NULL == head->pNext)
- {
- free(head);
- head = NULL;
- return;
- }
- Node* p = head->pNext;
- while(NULL != p)
- {
- Node* tmp = p;
- p = p->pNext;
- free(tmp);
- }
- free(head);
- head = NULL;
- }
- void main()
- {
- createNodeList();
- Node* node1 = (Node*)malloc(sizeof(Node));
- node1->data = 1;
- node1->pNext = NULL;
- Node* node2 = (Node*)malloc(sizeof(Node));
- node2->data = 2;
- node2->pNext = NULL;
- addNode(node1);
- addNode(node2);
- reverseNodeList();
- Node* node3 = (Node*)malloc(sizeof(Node));
- node3->data = 3;
- node3->pNext = NULL;
- addNode(node3);
- sort();
- deleteNode(2);
- destroyNodeList();
- }
阅读全文
0 0
- 算法及数据结构之---链表
- 常见数据结构及算法分析之java
- 数据结构之算法特性及分类
- 算法及数据结构之散列表
- 数据结构之链表及相关算法
- 数据结构及算法-何谓数据结构
- 数据结构及算法综述
- 数据结构及算法
- 数据结构及算法
- 算法及数据结构(下)
- 数据结构及算法序言
- 常用数据结构及算法
- 数据结构及算法
- Linux C 函数参考之数据结构及算法篇
- Linux C Function()参考之数据结构及算法篇
- C++学习之数据结构及算法——冒泡排序
- 数据结构及简单算法的总结----之【排序】
- 数据结构之排序算法(二)-冒泡排序及改进
- 操作系统 (Po学校 计算机科学 笔记整理)
- python3 请求RESTful接口
- 细数那些能让人惊喜的windows技巧
- map的遍历的4种方法
- TortoiseGit安装与配置
- 算法及数据结构之---链表
- lua 冒泡排序
- securefx连接linux后文件夹中文乱码问题解决
- mysql开发规范
- maven settings文件常用配置
- Git master branch has no upstream branch的解决
- Python代码中捕捉性能-环境设置
- textarea 设置width 100%和padding后会溢出
- Python Web Server Gateway Interface(WSGI)