链表的基本操作
2016-10-11 23:16
239 查看
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; typedef struct node { int data; node* next; }Node; Node* head = NULL; bool buildlist() { //建立一个新的链表 head = (Node*)malloc(sizeof(Node)); if(head == NULL) { return false; } else { head->data = 0; head->next = NULL; return true; } } bool insertnode(Node* node) { //加入新节点 if(head == NULL) { return false; } Node* p = head->next; Node* q = head; while(p != NULL) { q = p; p = p->next; } q->next = node; node->next = NULL; return true; } bool delnode(int front) { //删除节点 if(head == NULL) { return false; } Node* p = head->next; int length = 0; while(p != NULL) { length++; p = p->next; } if(length < front) { return false; } else { Node*q = head; p = head->next; for(int i = 0; i < front; i++) { q = p; p = p->next; } Node* t = p->next; q->next = t; free(p); return true; } } void reverselist() { //逆序排列,非倒序 if(head == NULL) return ; if(head->next == NULL) return ; Node* p = head->next; Node* q = p ->next; Node* t = NULL; while(q != NULL) { t = q->next; q->next = p; p = q; q = t; } head->next->next = NULL; head->next = p; } void sortlist() { //排序 Node* Head = head; if(head == NULL) return ; if(Head->next == NULL) return ; Node* pi = Head->next; Node* pj = pi->next; for(; pi != NULL; pi = pi->next) { for(pj = pi->next; pj != NULL; pj = pj->next) { if(pi->data > pj->data) { int temp = pi->data; pi->data = pj->data; pi->data = temp; } } } } void destroylist() { //删除链表 if(head == NULL) return; if(head->next == NULL) { free(head); head = NULL; return; } Node* p = head->next; while(p != NULL) { Node* tmp = p; p = p->next; free(tmp); } free(head); head = NULL; } void printlist() { //打印链表 node* a = head; node* b = a->next; while(b != NULL) { cout<<b->data; a = b; b = b->next; } } void deleterepeat() { //删除重复的元素 Node* p1 = head; Node* p2 = p1->next; Node* p3 = NULL; while(p2 != NULL) { if(p1->data == p2->data) { p3 = p2->next; p1->next = p3; } p1 = p1->next; p2 = p2->next; } } int listlength() { //返回链表的长度 int num = 0; Node* x = head->next; while(x != NULL) { x = x->next; num++; } return num; } node* mergel(node* list1, node*list2) { //合并两个链表; if(list1 == NULL) { cout<<"Error"<<endl; return list2; } else if(list2 == NULL) { return list1; } node* temp2 = NULL; if(list1->data < list2->data) { temp2 = list1; temp2->next = mergel(list1->next, list2); } else { temp2 = list2; temp2->next = mergel(list1, list2->next); } return temp2; } int main() { if(buildlist()) { Node* node1 = (Node*)malloc(sizeof(Node)); node1->data = 1; node1->next = NULL; Node* node2 = (Node*)malloc(sizeof(Node)); node2->data = 2; node2->next = NULL; Node* node3 = (Node*)malloc(sizeof(Node)); node3->data = 3; node3->next = NULL; Node* node4 = (Node*)malloc(sizeof(Node)); node4->data = 4; node4->next = NULL; insertnode(node1); insertnode(node2); insertnode(node3); insertnode(node4); reverselist(); printlist(); sortlist(); delnode(3); destroylist(); } return 0; }
相关文章推荐
- 算法(05):基本链表的操作及双向链表
- 链表的基本操作
- 双链表基本操作
- 带头结点的单链表的12个基本操作
- C++链表基本操作
- 链表的基本操作
- 单链表的基本操作
- bo2-8.cpp 不带头结点的单链表(存储结构由c2-2.h定义)的部分基本操作(9个)
- bo2-4.cpp设立尾指针的单循环链表(存储结构由c2-2.h定义)的12个基本操作
- 单链表和双链表上的基本操作
- 数据结构类型定义及基本操作汇总(一)--线性表,单链表,栈和队列
- 用C语言编写一个包含链表的初始化、插入、删除、查找等基本操作的程序。
- 链表队列与二叉树基本操作
- bo2-9.cpp 不带头结点的单链表(存储结构由c2-2.h定义)的部分基本操作(2个)
- 链表的基本操作
- 单链表的基本操作
- 双向循环链表的基本操作
- 单链表基本操作c语实现
- 链表的一些基本操作
- 链表的基本操作