赞
踩
这里是对数据结构的复习,好久没有动C++了,现在再来写写链表, 怕真的以后看都看不懂了就尴尬了。逻辑语句不难,应该能看懂。大神别骂我为什么不用C++模板,因为我只想单纯写写链表看看自己还会不会。另外main函数可以自己写写清屏语句,我就不写了。
- #include <iostream>
- using namespace std;
- /* 创建一个单链表 */
- struct Node {
- int data;
- Node* next;
- };
- class Linklist
- {
- public:
- Linklist();
- ~Linklist();
- void Creatlist(int num);
- void Insertlist(int position, int num);
- void Showlist();
- void Delete(int position);
-
- private:
- Node *Head;
- };
-
- Linklist::Linklist()
- {
- Head = new Node;
- Head->next = NULL;
- }
-
- Linklist::~Linklist()
- {
- Node *p;
- while (Head) {
- p = Head;
- Head = Head->next;
- delete p;
- }
- Head = NULL;
- }
-
- void Linklist::Creatlist(int num) {
- Node *L, *Y;
- Y = Head;
- cout << "Please enter your " << num << "number:" << endl;
- for (int i = 0; i < num; i++) {
- L = new Node;
- cin >> L->data;
- L->next = Y->next;
- Y->next = L;
- Y = L;
- };
- }
-
- void Linklist::Insertlist(int position, int num) {
- int i = 0;
- Node *L;
- L = Head;
- while (L&&i < position - 1) {
- L = L->next;
- i++;
- };
- if (!L||i>position-1) {
- cout << "插入位置不对!" << endl;
- }
- else {
- Node *Y;
- Y = new Node;
- Y->data = num;
- Y->next = L->next;
- L->next = Y;
- }
- }
-
- void Linklist::Showlist() {
- Node *L;
- L = Head->next;
- while (L) {
- cout << "--->"<<L->data ;//主义这一句和下一句的顺序,颠倒了就会出现很难发现的问题。
- L = L->next;
- }
- cout << endl;
- }
-
- void Linklist::Delete(int position) {
- int i = 0;
- Node *L;
- Node *Y = Head;
- L = Head;
- while (L->next&&i < position-1) {
- L = L->next;
- i++;
- };
- if (!L->next||i>position-1) {
- cout << "删除位置不对!!" << endl;
- }
- else {
- Y = L->next;
- L->next = Y->next;
- delete Y;
- }
- }
-
- int main()
- {
- //这是我最开始检测链表功能的语句
- //Linklist P;
- //P.Creatlist(4);
- //P.Showlist();
- //P.Insertlist(1, 2);
- //P.Showlist();
- //P.Delete(5);
- //P.Showlist();
- //return 0;
- int num, position;
- Linklist P;
- system("cls");
- int chioce;
- do {
- cout << endl;
- cout << "\t\t\t 链表最基本操作\n" << endl;
- cout << "\t\t---------------------------------\n";
- cout << "\t\t---------------------------------\n";
- cout << " ***1.创建链表***\n";
- cout << " ***2.插入节点(已预定、未预定、)***\n";
- cout << " ***3.删除节点***\n";
- cout << " ***4.打印链表***\n";
- cout << " ***5.退出系统***\n";
- cout << "\t\t----------------------------------\n";
- cout << "\t\t----------------------------------\n";
- cout << "\t\t----*****输入chioce:" << " " << endl;
- cin >> chioce;
- switch (chioce)
- {
- case 1:
- int i;
- cout << "请输入创建的链表的长度:" << endl;
- cin >> i;
- P.Creatlist(i);
- cout << "创建完成!" << endl;
- break;
- case 2:
- {
- cout << "请输入你需要插入的位置" << endl;
- cin >> position;
- cout << endl;
- cout << "请输入你需要插入的数:" << endl;
- cin >> num;
- P.Insertlist(position, num);
- cout << "插入完成!" << endl;
- }break;
- case 3: {
- int position;
- cout << "请输入你需要删除的位置" << endl;
- cin >> position;
- cout << endl;
- P.Delete(position);
- cout << "删除完成!" << endl;
- } break;
- case 4: {
- cout << "链表目前如下:" << endl;
- P.Showlist();
- } break;
- case 5:cout << "\n感谢使用本系统欢迎您下次使用!\n";
- exit(0);
- default:
- cout << "Invalid chioce!\n";
- }
- } while (chioce != 5);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。