双向链表:两个指针域,一个指向前结点,一个指向后结点
list.h
#pragma once
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef struct DNode
{
struct DNode *prior;
ElemType data;
struct DNode *next;
}DNode, *DLinkList;
bool InitDLinkList(DLinkList &L);
Status CreatDLinkList(DLinkList &L);
bool InsertNextDNode(DNode *p, DNode *s);
Status DeleteNextDNode(DNode *p, ElemType &e);
void ListTraverse(const DLinkList L);
Status ListInsert(DLinkList &L, int i, ElemType e);
Status ListDelete(DLinkList &L, int i, ElemType &e);
DNode* GetElem(DLinkList L, int i);
void DestoryList(DLinkList &L);
oper_func.cpp
#include <iostream>
#include"list.h"
bool InitDLinkList(DLinkList &L)
{
L = new DNode;
if (L == NULL)
return FALSE;
L->prior = NULL;
L->next = NULL;
return TRUE;
}
Status CreatDLinkList(DLinkList &L)
{
using std::cin;
using std::cout;
using std::endl;
if (InitDLinkList(L))
{
DNode *s,*r = L;
int num;
cout << "输入你需要创建的双链表的个数:";
cin >> num;
for (int i = 1; i <= num; i++)
{
s = new DNode;
cout << "输入第" << i << "个元素:";
cin >> s->data;
s->next = NULL;
InsertNextDNode(r,s);
r = s;
}
return OK;
}
else
{
cout << "内存不够,单链表创建失败!" << endl;
return ERROR;
}
}
bool InsertNextDNode(DNode *p, DNode *s)
{
if (p == NULL || s == NULL)
return FALSE;
s->next = p->next;
if (p->next != NULL)
{
p->next->prior = s;
}
s->prior = p;
p->next = s;
return TRUE;
}
Status DeleteNextDNode(DNode *p, ElemType &e)
{
if(p == NULL)
return FALSE;
DNode *q = p->next;
if (q == NULL)
{
return ERROR;
}
else
{
p->next = q->next;
e = q->data;
if (q->next != NULL)
{
q->next->prior = p;
}
delete q;
return OK;
}
}
void ListTraverse(const DLinkList L)
{
using std::cout;
using std::endl;
int i = 1;
DNode *p = L->next;
if (p == NULL)
{
cout << "双链表为空,无法输出元素!" << endl;
return;
}
while (p)
{
cout << "第" << i++ << "个元素为:" << p->data << endl;
p = p->next;
}
}
Status ListDelete(DLinkList &L, int i, ElemType &e)
{
if (i < 1)
return ERROR;
DNode *p = GetElem(L, i - 1);
return DeleteNextDNode(p,e);
}
DNode* GetElem(DLinkList L, int i)
{
DNode *p = L;
int j = 0;
while (p != NULL && j < i)
{
p = p->next;
j++;
}
return p;
}
void DestoryList(DLinkList &L)
{
using std::cout;
using std::endl;
ElemType temp;
int i = 1;
while (L->next != NULL)
{
DeleteNextDNode(L,temp);
cout << "第" << i++ << "个删除的元素为:" << temp << endl;
}
cout << "双链表全部数据销毁成功!" << endl;
delete L;
cout << "头结点销毁,整个双链表销毁成功!" << endl;
L = NULL;
}
Status ListInsert(DLinkList &L, int i, ElemType e)
{
if (i < 1)
return FALSE;
DNode *p = GetElem(L, i - 1);
DNode *s = new DNode;
s->data = e;
s->next = NULL;
s->prior = NULL;
return InsertNextDNode(p,s);
}
main.cpp
#include <iostream>
#include"list.h"
void showMenu()
{
using std::cout;
using std::cin;
using std::endl;
cout << "*********************************************************" << endl;
cout << "*** 1.指定位置插入元素 2.删除指定位置元素 ***" << endl;
cout << "*** 3.遍历单链表 0.销毁双链表并退出 ***" << endl;
cout << "*********************************************************" << endl;
}
int main()
{
using std::cout;
using std::cin;
using std::endl;
int select = 0, flag = -1;
DLinkList L;
if (CreatDLinkList(L))
{
cout << "双链表创建成功!" << endl;
}
else
cout << "双链表创建失败!" << endl;
while (true)
{
showMenu();
cout << "输入你的选择:";
cin >> select;
switch (select)
{
case 1: {
int position = 0, elem = 0;
cout << "输入插入的位置和元素:";
cin >> position >> elem;
if (ListInsert(L, position, elem))
cout << "指定位置插入元素成功!" << endl;
else
cout << "内存分配失败或者插入位置越界,插入失败!" << endl;
}
break;
case 2: {
int position = 0, elem = 0;
cout << "输入指定位置:";
cin >> position;
if (ListDelete(L, position, elem))
{
cout << "删除指定位置元素成功!元素为:" << elem << endl;
}
else
{
cout << "单链表为空或者删除位置不合理!" << endl;
}
}
break;
case 3: {
ListTraverse(L);
}
break;
case 0: {
DestoryList(L);
system("pause");
return 0;
}
break;
}
}
return 0;
}
|