双向链表
目的要求
1.掌握双向链表的存储结构及其实现。
2.掌握双向链表的插入与删除算法的程序实现。
实验内容
1.利用尾插法建立一个双向链表。
2.遍历双向链表。
3.实现双向链表中删除一个指定元素。
4.在非递减有序双向链表中实现插入元素 e 仍有序的算法。
5.判断双向链表中元素是否对称,若对称返回 1,否则返回 0。
6.设元素为正整型,实现算法把所有奇数排列在偶数之前。
7.在主函数中设计一个简单菜单,调用上述算法。
实验说明
- 双向链表的类型定义
typedef int ElemType; // 元素类型
typedef struct DuLNode
{
9 / 14
ElemType data;
DuLNode *prior, *next;
} DuLNode, *pDuLinkList;
源代码
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType; // 元素类型
typedef struct DuLNode
{
ElemType data;
DuLNode *prior, *next;
}DuLNode, *pDuLinkList;
typedef DuLNode * LNode;
//创建带有头结点的空链表
pDuLinkList SetNullList_Link()
{
pDuLinkList head = (pDuLinkList)malloc(sizeof(struct DuLNode)); //申请头节点空间
if (head != NULL)
{
head->next = NULL;
head->prior = NULL;
}
else
printf("Alloc failure!\n");
return head;
}
//尾插法建立双向链表
void CreateList_Tail(pDuLinkList head)
{
int n;
LNode p = NULL;
LNode q = head;
printf("请输入建立链表的元素个数:");
scanf("%d", &n);
printf("请输入这%d个元素:",n);
for (int i = 0;i < n;i++)
{
p = (LNode)malloc(sizeof(DuLNode));
scanf("%d", &p->data);
p->next = NULL;
q->next = p;
p->prior = q;
q = p;
}
head->prior = q;
}
//输出双向链表
void printList(pDuLinkList head)
{
LNode p = head->next;
int i = 0;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
//逆向输出双链表
void printList2(pDuLinkList head)
{
LNode p = head->prior;
int i = 0;
while (p != head)
{
printf("%d ", p->data);
p = p->prior;
}
printf("\n");
}
//销毁链表,释放空间
void DestroyList(pDuLinkList head)
{
LNode temp = NULL;
while (head->next != NULL) {
temp = head;
head = head->next;
free(temp);
}
free(head);
head = NULL;
}
//删除一个指定元素
void DelValue_Link(pDuLinkList head, ElemType x)
{
LNode p = head->next;
if (head->next == NULL)
return;
while (p)
{
if (p->data == x)
{
if (p->next == NULL) { //如果p是尾结点则直接将前驱结点指空
p->prior->next = NULL;
free(p);
return;
}
else {
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
return;
}
}
else
p = p->next;
}
printf("该链表元素中没有%d", x);
}
//非递减顺序排序(不改变指针,只交换数据域)
int Sort_LinkList(pDuLinkList head)
{
LNode p, q;
LNode min = NULL;
ElemType temp;
if (head->next == NULL) {
printf("The List is NULL!!\n");
return 0;
}
for (p = head->next; p->next; p = p->next)
{
min = p;
for (q = p->next; q; q = q->next)
{
if (q->data < min->data)
min = q;
}
temp = p->data;
p->data = min->data;
min->data = temp;
}
return 1;
}
//在非递减有序双向链表中插入元素
int Insert_sort(pDuLinkList list, ElemType x)
{
LNode p = list;
while (p->next && x>p->next->data)
p = p->next;
LNode q = (LNode)malloc(sizeof(DuLNode));
if (q == NULL) {
printf("Alloc failure!\n");
return 0;
}
if (p->next == NULL) //如果q作为尾结点插入,则头指针的prior指针指向q
{
q->data = x;
q->prior = p;
q->next = p->next;
p->next = q;
list->prior = q;
}
else
{
q->data = x;
q->prior = p;
q->next = p->next;
p->next->prior = q;
p->next = q;
return 1;
}
}
//判断双向链表中元素是否对称
int Symmetry_List(pDuLinkList head)
{
if (head->next == NULL)
return -1;
LNode p1 = head->next;
LNode p2 = head->prior;
while (p1&&p2 != head)
{
if (p1->data != p2->data)
return 0;
p1 = p1->next;
p2 = p2->prior;
}
return 1;
}
//把所有奇数排列在偶数之前
int jiou_Sort(pDuLinkList head)
{
if (head->next == NULL) {
printf("The List is NULL!\n");
return 0;
}
LNode p = head->next;
LNode q = NULL;
LNode tail = head->prior; //保存尾结点
while (p != tail)
{
if (p->data % 2)
p = p->next; //如果是奇数,指针后移
else
{
q = p->next;
//先把偶数结点p从当前位置取出
p->prior->next = p->next;
p->next->prior = p->prior;
//把偶数结点p放到链表尾
p->prior = head->prior;
head->prior->next = p;
p->next = NULL;
head->prior = p;
p = q; //指针后移
}
}
return 1;
}
int main()
{
int Index;
pDuLinkList list;
list = SetNullList_Link();
printf(" 双向链表\n");
printf(" ****************************************************************\n");
printf(" 1.利用尾插法建立一个双向链表\n");
printf(" 2.遍历双向链表\n");
printf(" 3.实现双向链表中删除一个指定元素\n");
printf(" 4.非递减顺序排序\n");
printf(" 5.在非递减有序双向链表中实现插入元素e仍有序的算法\n");
printf(" 6.判断双向链表中元素是否对称,若对称返回 1,否则返回 0\n");
printf(" 7.设元素为正整型,实现算法把所有奇数排列在偶数之前\n");
printf(" 0.退出并删除链表,释放空间\n");
printf(" ****************************************************************\n");
while (true)
{
printf("\n请输入一个序号:");
scanf("%d", &Index);
switch (Index)
{
case 0:
{
DestroyList(list);
return 0;
}
case 1:
{
CreateList_Tail(list);
printf("建表成功: ");
printList(list);
}break;
case 2:
{
if (list->next == NULL)
printf("The List is NULL!\n");
else {
printf("正向遍历:");
printList(list);
printf("逆向遍历:");
printList2(list);
}
}break;
case 3:
{
if (list->next == NULL)
printf("The List is NULL!\n");
else {
ElemType x;
printf("请输入要删除的元素:");
scanf("%d", &x);
DelValue_Link(list, x);
printList(list);
}
}break;
case 4:
{
if (Sort_LinkList(list)) {
printf("非递减顺序排序:");
printList(list);
}
}break;
case 5:
{
ElemType x;
printf("请输入要插入的元素:");
scanf("%d", &x);
if (Insert_sort(list, x))
{
printf("\n插入成功: ");
printList(list);
}
}break;
case 6:
{
if (Symmetry_List(list) == -1)
printf("The List is NULL!\n");
else if (Symmetry_List(list))
printf("双向链表中元素对称!\n");
else
printf("双向链表中元素不对称!\n");
}break;
case 7:
{
if (jiou_Sort(list))
{
printf("把所有奇数排列在偶数之前:");
printList(list);
}
}break;
default:
printf("输入有误,请输入数字0-7!\n");
break;
}
}
printf("\n");
system("pause");
return 0;
}