链表的建立和一些基本功能的实现
2014-12-10 13:01
316 查看
#include<iostream> using namespace std; typedef int T; class List { struct Node{ Node*next; T data; Node( const T t = T() ) : data(t), next(NULL) {} }; int len ; Node *head; public: List() : len(0), head(NULL) {} void push_front(T t) { Node *p = new Node(t); Node *it = head; if(it == NULL) { head = p; len++; }else { cout<<"push fornt"<<endl; p->next = it; head = p; len++; } } void push_back(T t) { Node *p = new Node(t); Node *tem = NULL, *it = head; if(it == NULL) { head = p; len++; }else { while(it) { tem = it; it = it->next; } tem->next = p; len++; } } void insert(T t, int pos) { if(pos <= 0 || pos > len) { return; } else if(pos == 1) { push_front(t); } else { Node *p = new Node(t); Node *it = head; for (int i = 1; i < pos-1; i++) { it = it->next; } p->next = it->next; it->next = p; len++; } } void erase(int pos) { Node *it = head; if(pos <= 0 || pos > len) { return; } else if (pos == 1) { it = it->next; head = it; len--; } else { Node *tem = NULL; for (int i = 1; i < pos-1; i++) { it = it->next; } tem = it->next; if (pos == len) { delete tem; } else it->next = tem->next; len--; } } void set (T t, int pos) { Node *p = new Node(t); Node *it = head, *tem = NULL; if (pos <= 0 || pos >len) { return; } else if (pos == 1) { it = it->next; p->next = it; head = p; } else { for (int i = 1; i < pos-1; i++) { it = it->next; } tem = it->next; it->next = p; p->next = tem->next; } } int find(T t) { int count = 0; Node *it = head; for (int i = 1; i <= len; i++) { if (it->data == t) { count++; } it = it->next; } return count; } int size() { return len; } void travel() { Node *it = head; cout << "========This List is ========== " << endl; while (it) { cout << it->data <<" "; it = it->next; } cout<<endl; /* cout << it->data << " "; for (int i = 1; i < size(); i++) { it = it->next; cout << it->data << " "; } */ } void clear() { Node *it = head; for (int i = 1; i <= len; i++) { delete it; it = it->next; } } }; void function (List &list) { int t; cout << "put data to List" <<endl; while (cin >> t && t != 0) { list.push_front(t); } cout << "The List size is " << list.size() << endl; list.travel(); // cin.ignore(1024,'/n'); } int main() { List list; function(list); int w, t, pos,count; cout<<"consloe:"<<endl; //cin.ignore(1024,'/n'); while (cout<<"please enter a commond:"&& cin >> w && w !=0 ) { switch (w) { case 1: cout << "Insert a Node at List front "<<endl; cout << "The List size is " << list.size() << endl; cout << "Please input a data of t : "; cin >> t ; list.push_front(t); break; case 2: cout << "Insert a Node at List After" << endl; cout << "The List size is " << list.size() <<endl; cout << "please input a data of t : "; cin >> t; list.push_back(t); break; case 3: cout << "Insert a Node at anyplace" << endl; cout << "The List size is " << list.size() <<endl; cout << "Please input two data of t and pos: "; cin >> t >> pos; list.insert(t, pos); break; case 4: cout << "Change the Node at anyplace"<< endl; cout << "The List size is " << list.size() <<endl; cout << "Please input two data of t and pos: "; cin >> t >> pos; list.set( t, pos); break; case 5: cout << "delete a Node at anyplace " << endl; cout << "The List size is " << list.size() <<endl; cout << "Please input a data of pos: "; cin >> pos; list.erase( pos); break; case 6: cout << "Find the List have how much Node are equal the t " << endl; cout << "Please input a data of t " ; cin >> t; count = list. find(t); cout << "count = " << count << endl; break; } list.travel(); //cin.ignore(1024,'/n'); } system("pause"); return 0; }
相关文章推荐
- 双链表一些基本功能的简单实现
- 八.二叉树各种操作的C语言实现 树的一些基本的操作,包括,树的建立,树的深度,
- [Flex+JAVA]建立Flex+java项目,并实现基本功能
- 实现单向链表的基本功能
- 链表队列基本功能的实现
- 数据结构C语言版--单链表的基本功能实现
- Queue类模板的链表实现(基本功能)
- 数据结构--单链表基本功能实现程序…
- C++实现双链表的基本功能
- 链表的可变数组的实现和一些基本操作
- 线性表中单链表基本功能实现
- C#+Mapxtreme 实现一些GIS系统基本的功能
- JavaScript HTML5 手机版手机版网站起码要实现一些基本的功能吧:
- 用自定义的队列实现一些基本功能
- 手机版网站起码要实现一些基本的功能
- HashMap存取效率高原因、ArrayList和LinkedList区别、JAVA实现链表的基本功能
- 单链表的基本功能 与扩展功能的实现
- 用C实现基本链表功能
- c语言单向链表的基本功能的实现
- [置顶] Android中一些基本功能的实现