#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define per(i, a, b) for(int i = a; i >= b; i --)
using namespace std;
typedef struct DLNode{
int data;
DLNode * next, * prior;
} * DBLNode;
int n;
void InitDLNode(DBLNode & L)
{
L = (DBLNode) malloc (sizeof(DLNode));
if(!L){
cout << "链表申请失败\n";
} else {
L -> next = L;
L -> prior = L;
cout << "链表申请成功\n";
}
return ;
}
void printDLNode(DBLNode & L)
{
cout << "链表输出如下:";
DBLNode head, p;
head = L;
while(head -> next != L){
cout << head -> next -> data << " ";
head = head -> next;
}
cout << endl;
}
void ScannerDLNode(DBLNode & L)
{
DBLNode head, p;
head = L;
cout << "请输入你要输入的元素数量:";
cin >> n;
cout << "请输入你要输入的元素:";
rep(i, 1, n){
p = (DBLNode) malloc (sizeof(DLNode));
int num;
cin >> num;
p -> data = num;
p -> prior = head;
p -> next = L;
head -> next = p;
head = head -> next;
}
printDLNode(L);
}
void InsertDLNode(DBLNode & L)
{
DBLNode head, p;
head = L;
cout << "请输入你要插入的元素和位置:";
int pos, val;
cin >> val >> pos;
int cnt = 0;
rep(i, 1, n){
if(cnt == pos - 1){
p = (DBLNode) malloc (sizeof(DLNode));
p -> data = val;
p -> next = head -> next;
head -> next = p;
head -> next -> prior = p;
p -> prior = head;
n ++;
break;
} else {
head = head -> next;
cnt ++;
}
}
printDLNode(L);
}
void DeleteDLNode(DBLNode & L)
{
DBLNode head;
head = L;
cout << "请输入你要删除的元素:";
int tmp;
cin >> tmp;
int cnt = 0, c = 0;
bool f = false;
rep(i, 1, n){
if(head -> next -> data == tmp){
f = true;
DBLNode p = head -> next;
p -> prior -> next = p -> next;
p -> next -> prior = p -> prior;
free(p);
c ++;
} else {
head = head -> next;
}
}
n -= c;
if(f){
printDLNode(L);
} else {
cout << "未查询到该元素\n";
}
}
void InsertNewDLNOde(DBLNode & L)
{
DBLNode DL2, h1, h2, p;
InitDLNode(DL2);
ScannerDLNode(DL2);
h1 = L;
h2 = DL2;
while(h1 -> next != L && h2 -> next != DL2){
if(h1 -> next -> data > h2 -> next -> data){
p = (DBLNode) malloc (sizeof(DLNode));
p -> data = h2 -> next -> data;
p -> prior = h1;
p -> next = h1 -> next;
h1 -> next = p;
h1 = p;
h2 = h2 -> next;
} else if(h1 -> next -> data < h2 -> next -> data){
h1 = h1 -> next;
} else {
h1 = h1 -> next;
h2 = h2 -> next;
}
}
if(h1 -> next == L){
h2 -> next -> prior = h1;
h1 -> next = h2 -> next;
h1 = h1 -> next;
} else {
while(h1 -> next != L){
h1 = h1 -> next;
}
}
h1 -> next = L;
printDLNode(L);
}
void FindDLNode(DBLNode & L)
{
DBLNode head = L;
cout << "请输入你要查询的元素:";
int val;
cin >> val;
int cnt = 1;
bool f = false;
while(head -> next != L){
if(head -> next -> data == val){
f = true;
cout << "该元素在第" << cnt << "个位置\n";
break;
} else {
cnt ++;
head = head -> next;
}
}
if(!f){
cout << "该元素不存在!\n";
}
}
int main()
{
DBLNode DL;
bool f = true;
while(f){
int op;
cout << "操作菜单如下:\n";
cout << "1. 初始化一个链表\n";
cout << "2. 输入一个链表\n";
cout << "3. 在某一位置插入元素\n";
cout << "4. 删除某一个元素\n";
cout << "5. 插入一个新的有序链表,并合并\n";
cout << "6. 查询链表中元素第一次出现的位置\n";
cout << "7. 退出程序\n";
cout << "请输入你要进行的操作:";
cin >> op;
if(op == 1) {
InitDLNode(DL);
} else if(op == 2) {
ScannerDLNode(DL);
} else if(op == 3) {
InsertDLNode(DL);
} else if(op == 4) {
DeleteDLNode(DL);
} else if(op == 5) {
InsertNewDLNOde(DL);
} else if(op == 6) {
FindDLNode(DL);
} else {
break;
}
}
return 0;
}
2021-09-27 双向循环链表
最新推荐文章于 2024-07-23 12:29:02 发布