Main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "BothNode.h"
void main()
{
Node *h;
char c,index,ch='0';
int n;
h=(Node *)malloc(sizeof(Node));
printf("创建一个双向链表:\n");
CreateNode(h);
PrintNode(h);
while(ch!=27)
{
fflush(stdin);
printf("请按Enter键继续...");
ch=getchar();
system("cls");
SelectMenu();
scanf("%c",&index);
switch(index)
{
case '1':
printf("头部添加一个结点,输入结点元素:");
fflush(stdin);
scanf("%c",&c);
AddHead(h,c);
PrintNode(h);
break;
case '2':
printf("尾部添加一个结点,输入结点元素:");
fflush(stdin);
scanf("%c",&c);
AddTail(h,c);
PrintNode(h);
break;
case '3':
printf("插入一个结点,输入结点元素和位置:");
fflush(stdin);
scanf("%c%d",&c,&n);
InsertNode(h,n,c);
PrintNode(h);
break;
case '4':
printf("删除一个结点,输入删除结点位置:");
fflush(stdin);
scanf("%d",&n);
DeleteNode(h,n,c);
printf("删除结点%c\n",c);
PrintNode(h);
break;
case '5':
printf("获得一个结点,输入结点位置:");
fflush(stdin);
scanf("%d",&n);
GetNodeIndex(h,n,c);
printf("查到结点是:%c\n",c);
PrintNode(h);
break;
case '6':
printf("修改一个结点,输入结点元素和位置:");
fflush(stdin);
scanf("%c%d",&c,&n);
SetNodeIndex(h,n,c);
PrintNode(h);
break;
case '0':exit(0);break;
default:
printf("\n非法操作,请重新选择!");
break;
}
}
//释放整条链表
DestroyList(h);
}
BothNode.h
#ifndef __BOTHNODE__H
#define __BOTHNODE__H
typedef char DataType;
#define ASSERT(p) {if(!(p)) {printf("%d Failed\n",__LINE__);exit(1);}}
typedef struct Node
{
DataType data;
struct Node *front;
struct Node *rear;
}Node;
void CreateNode(Node *head);//创建一个双向链表
void AddHead(Node *head,DataType e);//在头部添加一个结点
void AddTail(Node *head,DataType e);//在尾部添加一个结点
bool InsertNode(Node *head,int i,DataType e);//在i位置插入一个结点
bool DeleteNode(Node *head,int i,DataType &e);//在i位置删除一个结点,把结点内容赋值给e
bool GetNodeIndex(Node *head,int i,DataType &e);//获得第i个位置的结点元素
bool SetNodeIndex(Node *head,int i,DataType e);//修改第i个位置结点的元素
void DestroyList(Node *head);//释放整条链表
void SelectMenu();//选择操作菜单
void PrintNode(Node *head);//输出双链表的内容
#endif
BothNode.cpp
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "BothNode.h"
//////////////////////////
void CreateNode(Node *head)//创建一个双向链表
{//头结点内容为空
ASSERT(head!=NULL);//断言
Node *h, *p;
char c;
head->front=NULL;
h=head;
h->rear=NULL;
c=getchar();
while(c!='\n')
{
p=(Node *)malloc(sizeof(Node));
p->data=c;
c=getchar();
h->rear=p;//上一个结点的后指针指向新结点p
p->front=h;//p的前结点指向上一个结点
p->rear=NULL;
h=p;
}
}
void AddHead(Node *head,DataType e)//在头部添加一个结点
{
ASSERT(head!=NULL);//断言
Node *p,*h;
h=head;
p=(Node *)malloc(sizeof(Node));
p->data=e;
p->front=h;
p->rear=h->rear;
h->rear->front=p;
h->rear=p;
}
void AddTail(Node *head,DataType e)//在尾部添加一个结点
{
ASSERT(head!=NULL);//断言
Node *p,*h;
h=head->rear;
p=(Node *)malloc(sizeof(Node));
p->data=e;
while(h->rear!=NULL)
{
h=h->rear;
}
h->rear=p;
p->front=h;
p->rear=NULL;
}
bool InsertNode(Node *head,int i,DataType e)//在i位置插入一个结点
{
ASSERT(head!=NULL);//断言
Node *p,*h;
h=head->rear;
int n=1;
if(n>i)return false;
while(n<i && h!=NULL)
{
h=h->rear;
n++;
}
if(h==NULL) return false;
p=(Node *)malloc(sizeof(Node));
p->data=e;
p->front=h;
p->rear=h->rear;
h->rear->front=p;
h->rear=p;
return true;
}
bool DeleteNode(Node *head,int i,DataType &e)
{//在i位置删除一个结点,把结点内容赋值给e
ASSERT(head!=NULL);//断言
int n=1;
Node *p;
p=head->rear;
if(n>i)return false;
while(n<i && p!=NULL)
{
p=p->rear;
n++;
}
if(p==NULL) return false;
if(p->rear==NULL)//判断删除的结点是否在链表尾部
{
e=p->data;
p->front->rear=NULL;
}
else
{
e=p->data;
p->front->rear=p->rear;
p->rear->front=p->front;
}
free(p);
return true;
}
bool GetNodeIndex(Node *head,int i,DataType &e)//获得第i个位置的结点元素
{
ASSERT(head!=NULL);//断言
int n=1;
Node *p;
p=head->rear;
if(n>i)return false;
while(n<i && p!=NULL)
{
p=p->rear;
n++;
}
if(p==NULL) return false;
e=p->data;
return true;
}
bool SetNodeIndex(Node *head,int i,DataType e)//修改第i个位置结点的元素
{
ASSERT(head!=NULL);//断言
int n=1;
Node *p;
p=head->rear;
if(n>i)return false;
while(n<i && p!=NULL)
{
p=p->rear;
n++;
}
if(p==NULL) return false;
p->data=e;
return true;
}
void DestroyList(Node *head)//释放整条链表
{
ASSERT(head!=NULL);//断言
Node *h,*p;
h=head;
p=h->rear;
while(h->rear!=NULL)
{
free(h);
h=NULL;
h=p;
p=h->rear;
}
free(h);
h=NULL;
}
void PrintNode(Node *head)
{
Node *p;
p=head->rear;
while(p!=NULL)
{
printf("%c\t",p->data);
p=p->rear;
}
printf("\n\n");
}
void SelectMenu()//选择操作菜单
{
printf("\n\n\n双向链表操作请选择...\n");
printf("\t\t=============================\n");
printf("\t\t1、头部添加一个结点 \n");
printf("\t\t2、尾部添加一个结点 \n");
printf("\t\t3、插入一个结点 \n");
printf("\t\t4、删除一个结点 \n");
printf("\t\t5、查找一个结点 \n");
printf("\t\t6、修改一个结点 \n");
printf("\t\t0、退出 \n");
printf("\t\t=============================\n");
}