数据结构和算法学习总结03 线性表---单链表
单链表
单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。
单链表的优点在于 当大量的进行插入和删除操作时,它的效率是比顺序表高的。
缺点在于 当只是进行读取元素时,它的效率比顺序表底。
以下为单链表(C++)的代码:
#include <stdio.h> #include <iostream> using namespace std; typedef struct ListNode { int data; struct ListNode *next; } Node; class SingleList { public: SingleList() { head = new Node; head->data = 0; head->next = NULL; } ~SingleList() {delete[] head;} void CreateList(int n); //创建链表 void InsertNode(int position, int d); //在指定位置插入结点 void DeleteNode(int position); //删除指定位置结点 int CheckList(int position); //查询指定位置 bool IsEmpty(); //判断链表是否为空 int GetLength(); //链表长度 void ReverseList(); //单链表反转/逆序 void DestroyList(); //销毁整个链表 头指针为空 void ClearList(); //清空链表 头指针不为空 void OutputList(); //遍历链表 private: Node *head; }; void SingleList::CreateList(int n) { //尾插法创建单链表 if(n<0) { cout<<"输入节点数量错误"<<endl; exit(0); } else { Node *tempN, *newN; tempN = head; while(n-->0) { newN=new Node; cout<<"请输入结点值:"<<endl; cin>>newN->data; newN->next = NULL; tempN->next = newN; tempN = newN; } } //头插法创建单链表 输入顺序和链表顺序相反 /* if(n<0) { cout<<"输入节点数量错误"<<endl; exit(0); } else { Node *tempN, *newN; tempN = new Node; head->next = NULL; while(n-->0) { newN=new Node; cout<<"请输入结点值:"<<endl; cin>>newN->data; newN->next = head->next; head->next = newN; } } */ } void SingleList::InsertNode(int position, int d) { if(position<0||position >GetLength()+1) { cout<<"输入位置错误!"<<endl; exit(0); } else { Node *tempN, *newN; tempN = head; newN = new Node; newN->data = d; newN->next = NULL; int i = 1; while(i<position) { tempN = tempN->next; i++; } newN->next = tempN->next; tempN->next = newN; } } void SingleList::DeleteNode(int position) { if (position < 0 || position > GetLength()) { cout << "输入位置错误!" << endl; exit(0); } else { Node *tempN,*deleteN; tempN = head; int i = 1; while(i<position) { tempN = tempN->next; i++; } deleteN = tempN->next; tempN->next = deleteN->next; delete deleteN; } } bool SingleList::IsEmpty() { if (head->next == NULL) return true; else return false; } int SingleList::GetLength() { Node *p = head->next; int n = 0; while (p != NULL) { n++; p = p->next; } return n; } void SingleList::OutputList() { Node *p = head->next; while (p != NULL) { cout << p->data << " "; p = p->next; } cout << endl; } void SingleList::DestroyList() { Node *p; while(head) { p=head->next; delete head; head=p; } } void SingleList::ClearList() { Node *p,*q; p=head->next; while(p) { q=p->next; delete p; p=q; } head->next=NULL; } int SingleList::CheckList(int position) //查询 { int i; Node *p; p = head->next; i=1; while(p&&i<position) { p = p->next; i++; } if(!p || i>position) cout<<"查询的元素不存在!"<<endl; return p->data; } void SingleList::ReverseList() { Node *current, *p; current = head->next; while (current->next) { p = current->next; current->next = p->next; p->next = head->next; head->next = p; } } int main() { int num,v,p; cout<<"********************************************"<<endl; cout<<" 【1】创建 【2】插入" <<endl; cout<<" 【3】删除 【4】查询" <<endl; cout<<" 【5】清空 【6】翻转" <<endl; cout<<" 【7】遍历 【8】退出" <<endl; cout<<"********************************************"<<endl; cout<<"请输入你的操作序号:"; SingleList list; while(1) { cin>>num; switch(num) { case 1: cout<<"要创建的链表长度:"<<endl; cin>>v; list.CreateList(v); cout<<"创建成功"<<endl; break; case 2: cout<<"要插入的元素的位置:"<<endl; cin>>p; cout<<"要插入的元素:"<<endl; cin>>v; list.InsertNode(p,v); break; case 3: cout<<"要删除的元素的位置:"<<endl; cin>>p; list.DeleteNode(p); //删除 break; case 4: cout<<"要查询的元素的位置:"<<endl; cin>>p; cout<<"查询结果"<<list.CheckList(p)<<endl; //查询 break; case 5: list.ClearList(); //清空 break; case 6: list.ReverseList(); //翻转 break; case 7: list.OutputList(); //显示 break; case 8: list.DestroyList(); //销毁 exit(0); break; } cout<<"请输入你的操作序号:"; } }