首页 > 代码库 > 链表的基本操作
链表的基本操作
链表的基本操作
分类: 数据结构与算法2014-04-09 15:59 8903人阅读 评论(0) 收藏 举报
链表逆序排序销毁增加节点
内容包括链表的创建,增加、删除节点,链表的逆序、排序和销毁等。
[cpp] view plaincopy
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int data;
- node* pNext;
- }Node;
- //链表的操作,以有头节点为例,无头节点类似
- Node* head = NULL;
- //创建链表,头结点data=http://www.mamicode.com/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();
- }
链表的基本操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。