[c++]单链表(class)
2015-12-09 22:20
363 查看
//list.h
#include<iostream> typedef char Type; using namespace std; class List { public: List *head; ~List(); List(Type body, List *add = NULL); List(); void Creat(List *&head); void Insert(); void Delete(List *&head); void Print(); void Release(); private: bool search(Type item, List *&p); Type body; List *next; Type item; };
</pre><pre name="code" class="cpp">//list.cpp
#include<iostream> #include"list.h" using namespace std; typedef char Type; //******************************************************************** void List::Creat(List *&head)//生成函数 { int n; cout << "please enter the length of the list: " << endl; cin >> n; Type *temp = new Type ; cout << "please enter the content of the list" << endl; for (int i = 0; i < n;i++) cin >> temp[i]; List *p = NULL; this->head = new List(temp[0]); p = this->head; for (int i = 1; i < n; i++) { p->next = new List(temp[i]); p = p->next; } p->next = NULL; cout << "Save succeed!" << endl; } //********************************************************************** bool List::search(Type item, List *&p)//搜索函数 {p = new List('0'); p->next = this->head; List *temp = this->head; if (temp->body == item) return true; else { p = this->head; temp = p->next; } while (p&&temp->body != item) { p = p->next; List *temp = p->next; } if (p) return true; else return false; } //********************************************************************** void List::Insert()//插入函数 { Type n; cout << "please enter the item you want to add on: " << endl; cin >> n; List *p = NULL; if (search(n,p)) { p = p->next; Type m; cout << "please enter the item you want to add: " << endl; cin >> m; List *q = new List(m,p->next); p->next = q; cout << "Insert success!" << endl; } else cout << "Don't have this item." << endl; } //*********************************************************************** void List::Delete(List *&head)//删除函数 { Type n; cout << "please enter the item you want to delete:" << endl; cin >> n; List *p = NULL; if (this->search(n,p)&&head->body==n) { List *q = p->next; head = q->next; p->next = head; delete q; cout << "Delete success!" << endl; } else if (this->search(n, p)) { List *q = p->next; p->next = q->next; delete q; cout << "Delete success!" << endl; } else cout << "Don't have this item." << endl; } //************************************************************************ void List::Print()//输出函数 { List *p = this->head; while (p) { cout << p->body<<' '; p = p->next; } cout << endl << "Print Succeed!" << endl; } //************************************************************************ void List::Release()//释放函数 { List *p = this->head; while (p) { head = head->next; delete p; } cout << "Release succeed!" << endl; } //************************************************************************ List::List(Type body, List *add)//构造函数 { this->body = body; this->next = add; } //************************************************************************** List::~List()//析构函数 { next = NULL; } //**************************************************************** List::List() { }
//main.cpp
#include<iostream> #include"list.h" using namespace std; typedef char Type; void main() { cout << "**************************************MENU*************************************" << endl; cout << "1.Creat a list." << endl; cout << "2.Insert a item." << endl; cout << "3.Delete a item." << endl; cout << "4.Print the list." << endl; cout << "-1.END" << endl; cout << "FBI WARNING: please press '0'to release before end the programm!" << endl; List list1; int n=1; while (n!=-1) { cout << "please choose the number above." << endl; cin >> n; switch (n) { case 1: list1.Creat(list1.head); break; case 2: list1.Insert(); break; case 3: list1.Delete(list1.head); break; case 4: list1.Print(); break; case 0: list1.Release(); default: cout << "Wrong Input" << endl; break; } } }
相关文章推荐
- C++左值引用和右值引用
- C语言易错点:字符串的连接
- Effective C++阅读笔记_条款2:尽量以const,enum,inline替换#define
- [C++] Test question(1-16)
- C语言:sizeof导致的Memory access violation
- 链表的分化问题(C++)
- 实战c++中的string系列--指定浮点数有效数字并转为string
- 实战c++中的string系列--指定浮点数有效数字并转为string
- (学习)C++ Primer(1)
- 实战c++中的string系列--string与整型或浮点型互转
- 实战c++中的string系列--string与整型或浮点型互转
- C语言快排模板 qsort();函数应用
- HDU2089 不要62【数位DP】
- c++ 11 右值引用原理
- 设计Qt风格的C++API
- C++ offsetof
- c语言的笔记
- 一个刚出生的奶牛,4年生一只,以后每年生一只,现在有一只刚出生的奶牛,问20年以后一共多少只奶牛?
- c++
- VC++ 最小化到托盘、恢复