1.选择菜单
2.头插法
void insertHead(DLinkList& L) {
DNode* s; int x;
L = (DLinkList)malloc(sizeof(DNode));
L->next = NULL;
L->prior = NULL;
scanf("%d", &x);
while (x != 999) {
s = (DLinkList)malloc(sizeof(DNode));
s->data = x;
s->next = L->next;
if (L->next != NULL)
{
L->next->prior = s;
}
s->prior = L;
L->next = s;
scanf("%d", &x);
}
}
3.尾插法
void insertEnd(DLinkList& L) {
int x;
L = (DLinkList)malloc(sizeof(DNode));
DNode* s, * r = L;
L->prior = NULL;
scanf("%d", &x);
while (x != 999) {
s = (DNode*)malloc(sizeof(DNode));
s->data = x;
r->next = s;
s->prior = r;
r = s;
scanf("%d", &x);
}
r->next = NULL;
}
4.修改元素
bool modifyNode(DLinkList& L, int index ,ElemType data) {
DLinkList temp = L->next;
int count = 0;
while (temp != NULL) {
count++;
if (count == index) {
temp->data = data;
return true;
}
else {
temp = temp->next;
}
}
return false;
}
5.查找元素
DLinkList searchNode(DLinkList L , int index) {
int count = 0;
DLinkList temp = L->next;
if (index<1 || index>size(L)) {
return NULL;
}
while (temp != NULL) {
count++;
if (count == index) {
return temp;
}
else {
temp = temp->next;
}
}
return NULL;
}
6.删除元素
bool deleteNode(DLinkList& L, int index ) {
if (index<1 || index>size(L)) {
return false;
}
else {
int count = 0;
DLinkList temp = L->next;
if (index == 1) {
L->next = L->next->next;
L->next->prior = L;
return true;
}
while (temp != NULL) {
count++;
if(count == index){
DLinkList parent = temp->prior;
parent->next = temp->next;
temp->prior = parent;
temp->next = NULL;
return true;
}
else {
temp = temp->next;
}
}
}
return false;
}
7.遍历元素
void printfList(DLinkList L) {
DLinkList temp = L->next;
int count = 0;
while (temp != NULL) {
count++;
printf("DlinkList[%d] = %d\n", count, temp->data);
temp = temp->next;
}
}
8.插入元素
bool insertNode(DLinkList& L, int index, ElemType data) {
int count = 0;
if (index<1 || index>size(L)) {
return false;
}else {
DLinkList temp = L->next;
while (temp != NULL) {
count++;
if (count == index) {
DLinkList parent = temp->prior;
DLinkList newNode = (DLinkList)malloc(sizeof(DNode));
newNode->data = data;
newNode->next = temp;
newNode->prior = parent;
parent->next = newNode;
return true;
}
else {
temp = temp->next;
}
}
return false;
}
}
9.链表长度
int size(DLinkList L) {
DLinkList temp = L->next;
int count = 0;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
10.汇总
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct DNode {
ElemType data;
struct DNode* prior;
struct DNode* next;
}DNode,* DLinkList;
void insertHead(DLinkList& L);
void insertEnd(DLinkList& L);
bool deleteNode(DLinkList& L,int index);
bool modifyNode(DLinkList& L, int index,ElemType data);
bool insertNode(DLinkList& L, int index, ElemType data);
void printfList(DLinkList L);
void choice(DLinkList& L);
void menu(DLinkList& L);
int size(DLinkList L);
DLinkList searchNode(DLinkList L, int index);
bool insertNode(DLinkList& L, int index, ElemType data) {
int count = 0;
if (index<1 || index>size(L)) {
return false;
}else {
DLinkList temp = L->next;
while (temp != NULL) {
count++;
if (count == index) {
DLinkList parent = temp->prior;
DLinkList newNode = (DLinkList)malloc(sizeof(DNode));
newNode->data = data;
newNode->next = temp;
newNode->prior = parent;
parent->next = newNode;
return true;
}
else {
temp = temp->next;
}
}
return false;
}
}
bool deleteNode(DLinkList& L, int index ) {
if (index<1 || index>size(L)) {
return false;
}
else {
int count = 0;
DLinkList temp = L->next;
if (index == 1) {
L->next = L->next->next;
L->next->prior = L;
return true;
}
while (temp != NULL) {
count++;
if(count == index){
DLinkList parent = temp->prior;
parent->next = temp->next;
temp->prior = parent;
temp->next = NULL;
return true;
}
else {
temp = temp->next;
}
}
}
return false;
}
bool modifyNode(DLinkList& L, int index ,ElemType data) {
DLinkList temp = L->next;
int count = 0;
while (temp != NULL) {
count++;
if (count == index) {
temp->data = data;
return true;
}
else {
temp = temp->next;
}
}
return false;
}
int size(DLinkList L) {
DLinkList temp = L->next;
int count = 0;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
DLinkList searchNode(DLinkList L , int index) {
int count = 0;
DLinkList temp = L->next;
if (index<1 || index>size(L)) {
return NULL;
}
while (temp != NULL) {
count++;
if (count == index) {
return temp;
}
else {
temp = temp->next;
}
}
return NULL;
}
void insertHead(DLinkList& L) {
DNode* s; int x;
L = (DLinkList)malloc(sizeof(DNode));
L->next = NULL;
L->prior = NULL;
scanf("%d", &x);
while (x != 999) {
s = (DLinkList)malloc(sizeof(DNode));
s->data = x;
s->next = L->next;
if (L->next != NULL)
{
L->next->prior = s;
}
s->prior = L;
L->next = s;
scanf("%d", &x);
}
}
void insertEnd(DLinkList& L) {
int x;
L = (DLinkList)malloc(sizeof(DNode));
DNode* s, * r = L;
L->prior = NULL;
scanf("%d", &x);
while (x != 999) {
s = (DNode*)malloc(sizeof(DNode));
s->data = x;
r->next = s;
s->prior = r;
r = s;
scanf("%d", &x);
}
r->next = NULL;
}
void printfList(DLinkList L) {
DLinkList temp = L->next;
int count = 0;
while (temp != NULL) {
count++;
printf("DlinkList[%d] = %d\n", count, temp->data);
temp = temp->next;
}
}
void choice(DLinkList& L) {
int choice;
printf("请输入你的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
insertHead(L);
break;
case 2:
insertEnd(L);
break;
case 3: {
int index;
ElemType data;
printf("请输入要你修改的序号:");
scanf("%d", &index);
printf("修改的值为:");
scanf("%d", &data);
bool isModify = modifyNode(L, index, data);
if (isModify) {
printf("修改成功\n");
}
else {
printf("修改失败\n");
}
break;
}
case 4: {
int index;
printf("请输入你要查找的序号:");
scanf("%d", &index);
DLinkList search = searchNode(L, index);
if (search != NULL) {
printf("查找成功,结点数据为%d\n", search->data);
}
else {
printf("查找失败\n");
}
break;
}
case 5: {
int index;
printf("请输入你要删除的序号:");
scanf("%d",&index);
bool isDelete = deleteNode(L, index);
if (isDelete) {
printf("删除成功\n");
}
else {
printf("删除失败\n");
}
break;
}
case 6:
printfList(L);
break;
case 7: {
int index;
ElemType data;
printf("请输入你要插入的位置:");
scanf("%d", &index);
printf("请输入你要添加的值");
scanf("%d", &data);
bool isInsert = insertNode(L, index, data);
if (isInsert) {
printf("插入成功\n");
}
else {
printf("插入失败\n");
}
break;
}
case 8:
printf("长度为%d\n", size(L));
break;
default:
break;
}
menu(L);
}
void menu(DLinkList& L) {
printf("----------------双向链表-----------------\n");
printf(" 1.头插法 \n");
printf(" 2.尾插法 \n");
printf(" 3.修改元素 \n");
printf(" 4.查找元素 \n");
printf(" 5.删除元素 \n");
printf(" 6.遍历元素 \n");
printf(" 7.插入元素 \n");
printf(" 8.链表长度 \n");
choice(L);
}
int main() {
DLinkList L;
menu(L);
return 0;
}