双向循环链表
Posted lazy-cat
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了双向循环链表相关的知识,希望对你有一定的参考价值。
定义一个循环链表结构
typedef struct LNode { Elemtype data; struct LNode *prev; struct LNode *next; }*Linklist;
初始化链表
void CreatList(Linklist &L) { Linklist p, h; cout << "请输入链表的长度" << endl; int n; cin >> n; L = (Linklist)malloc(sizeof(LNode)); L->next = L; L->prev = L->next; h = L; if (!L) { cout << "申请空间失败" << endl; } cout << "请输入新节点的数值" << endl; while (n != 0) { p = (Linklist)malloc(sizeof(LNode)); cin >> p->data; p->next = h->next; h->next = p; p->prev = h; h = h->next; n--; } L->prev = h; }
按位置查找
void GetElem(Linklist &L) { cout << "请输入要查找的位置" << endl; int e; cin >> e; Linklist p = L; while (p->next != L && e != 0) { p = p->next; e--; } if (e != 0) { cout << "您所查找的位置不在本链表中" << endl; } else { cout << "您所查找的元素为:" << p->data << endl; } }
插入一个元素
void InsertElem(Linklist &L) { Elemtype data; int e; cout << "请输入插入的元素值和位置(以空格隔开)" << endl; cin >> data; cin >> e; Linklist s, p = L; s = (Linklist)malloc(sizeof(LNode)); s->data = data; while (p->next != L && e != 1) { p = p->next;; e--; } if (e > 1) { cout << "您输入的位置超过本表的长度啦!" << endl; } s->next = p->next; p->next = s; }
删除一个元素
void DeleteElem(Linklist &L) { int e; Linklist p = L, q = NULL; cout << "请输入删除元素位置" << endl; cin >> e; while (p->next != L && e != 1) { p = p->next; e--; } if (e > 1) { cout << "您输入的位置超过本表的长度啦!" << endl; } else { q = p->next; p->next = q->next; free(q); } }
遍历一遍链表
void ShowList(Linklist &L) { cout << "遍历一遍链表" << endl; Linklist l = L; while (l->next != L) { l = l->next; cout << l->data << ‘ ‘; } cout << endl; }
完整代码
#include"stdafx.h" #include <iostream> #include<stdlib.h> using namespace std; typedef int Elemtype; //定义一个循环链表结构 typedef struct LNode { Elemtype data; struct LNode *prev; struct LNode *next; }*Linklist; //初始化链表 void CreatList(Linklist &L) { Linklist p, h; cout << "请输入链表的长度" << endl; int n; cin >> n; L = (Linklist)malloc(sizeof(LNode)); L->next = L; L->prev = L->next; h = L; if (!L) { cout << "申请空间失败" << endl; } cout << "请输入新节点的数值" << endl; while (n != 0) { p = (Linklist)malloc(sizeof(LNode)); cin >> p->data; p->next = h->next; h->next = p; p->prev = h; h = h->next; n--; } L->prev = h; } //按位置查找 void GetElem(Linklist &L) { cout << "请输入要查找的位置" << endl; int e; cin >> e; Linklist p = L; while (p->next != L && e != 0) { p = p->next; e--; } if (e != 0) { cout << "您所查找的位置不在本链表中" << endl; } else { cout << "您所查找的元素为:" << p->data << endl; } } //插入一个元素 void InsertElem(Linklist &L) { Elemtype data; int e; cout << "请输入插入的元素值和位置(以空格隔开)" << endl; cin >> data; cin >> e; Linklist s, p = L; s = (Linklist)malloc(sizeof(LNode)); s->data = data; while (p->next != L && e != 1) { p = p->next;; e--; } if (e > 1) { cout << "您输入的位置超过本表的长度啦!" << endl; } s->next = p->next; p->next = s; } //删除一个元素 void DeleteElem(Linklist &L) { int e; Linklist p = L, q = NULL; cout << "请输入删除元素位置" << endl; cin >> e; while (p->next != L && e != 1) { p = p->next; e--; } if (e > 1) { cout << "您输入的位置超过本表的长度啦!" << endl; } else { q = p->next; p->next = q->next; free(q); } } //遍历一遍链表 void ShowList(Linklist &L) { cout << "遍历一遍链表" << endl; Linklist l = L; while (l->next != L) { l = l->next; cout << l->data << ‘ ‘; } cout << endl; }
觉得文章不错,点个赞和关注哟.
以上是关于双向循环链表的主要内容,如果未能解决你的问题,请参考以下文章