单链表基本操作实现
2017-09-30 15:53
302 查看
#include<iostream> #include<stack> using namespace std; class List{ private: struct Node{ int data; Node *next; Node(const int &d){ data=d; next=NULL; } }; Node *head; public: List(){ creat_List(); } void creat_List(); void insert(const int &d); void print(); void sort(); void insert_pos(const int &d, const int &d1); void erase(const int &d); void reverse(); Node * find(const int &d){ Node *p=head; for(;p;p=p->next) if(p->next->data==d) return p; return p; } }; void List::creat_List(){ head=new Node(0); } void List::insert(const int &d){ Node *p=new Node(d); p->next=head->next; head->next=p; delete p; p=NULL; } void List::print(){ Node *p=head->next; while(p){ cout<<p->data<<" "; p=p->next; } cout<<endl; } void List::sort(){ Node *p=head->next; for(;p;p=p->next){ for(Node *q=p->next;q;q=q->next){ if(p->data<q->data) swap(p->data,q->data); } } } void List::insert_pos(const int &d, const int &d1){ Node *p=find(d); Node *q=new Node(d1); q->next=p->next; p->next=q; } void List::erase(const int &d){ Node *p=find(d); Node *q=p->next; p->next=p->next->next; delete q; } void List::reverse(){ Node *p=head->next; stack<Node *> m; while(p){ m.push(p); p=p->next; } Node *q=head; while(!m.empty()){ q->next=m.top(); q=m.top(); m.pop(); } q->next=NULL; } int main(){ List list; list.insert(5); list.insert(6); list.insert(4); list.sort(); list.insert_pos(5,3); list.erase(4); list.print(); list.reverse(); list.print(); return 0; }
相关文章推荐
- 单链表基本操作的实现
- php实现链表的基本操作
- 对链表的基本实现和各种操作
- 链表基本操作的实现
- 单链表基本操作实现
- c/c++ 数据结构-线性表(单链表基本操作的实现)
- 链表基本操作的实现
- 链表的基本操作实现(创建,增,删,逆置)
- 单链表基本操作c语实现
- 单链表基本操作的实现
- 实现无头结点单链表的基本操作函数
- 16.单向链表的一些基本操作实现(链表反转,链表有环无环判断,链表冒泡排序,链表快速排序)
- 带头结点的链表实现线性表的基本操作
- 链表及其基本操作的JAVA实现。
- 数据结构学习之单向链表的基本操作(非递归实现)
- Java实现双向链表的基本操作
- 数据结构—链表-单链表基本操作实现
- c++实现链表的基本操作
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现
- c语言实现单链表数据结构及其基本操作