#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
//3 4 5 6 7 9 10 -9999
typedef struct DNode {
ElemType Data;
struct DNode* prior, * next;
}DNode,*DLinklist;
void List_length(DLinklist L);
bool initiList(DLinklist& L)
{
L = (DNode*)malloc(sizeof(DNode));
if (NULL == L)
return false;
L->prior = NULL;
L->next = NULL;
return true;
}
bool Empty(DLinklist L)
{
return (NULL == L->next);
}
bool List_HeadInsert(DLinklist L)
{
ElemType e;
printf("INPUT number:\n");
scanf("%d", &e);
while (e!=-9999)
{
DNode *s= (DNode*)malloc(sizeof(DNode));
if (NULL == s) return false;
s->Data = e;
s->next = L->next;
if (L->next != NULL)//插入第一个节点时后一个节点为NULL无需存储前驱节点
{
L->next->prior = s;
}
s->prior = L;
L->next = s;
scanf("%d", &e);
}
return true;
}
bool List_TailInsert(DLinklist L)
{
ElemType e;
printf("INPUT number:\n");
scanf("%d", &e);
DNode* p = L;
while (e!=-9999)
{
DNode* s = (DNode*)malloc(sizeof(DNode));
if (NULL == s) return false;
s->Data = e;
p->next = s;
s->prior = p;
p = s;
scanf("%d", &e);
}
p->next = NULL;
return true;
}
DNode* GetElem(DLinklist L, int i)
{
int j = 1;
if (i == 0 || NULL == L->next) return L;
if (i < 1) return NULL;
DNode* p = L->next;
while (p && j < i)
{
p = p->next;
j++;
}
return p;
}
DNode* LocateElem(DLinklist L, ElemType e)
{
if (NULL == L->next) return NULL;
L = L->next;
while (L && L->Data != e)
{
L = L->next;
}
return L;
}
bool List_IndexInsert(DLinklist L, int i, ElemType e)
{
DNode* p = GetElem(L, i - 1);
if (NULL == p) return false;
DNode* s = (DNode*)malloc(sizeof(DNode));
if (NULL == s) return false;
s->Data = e;
s->next = p->next;
if (p->next != NULL)
{
p->next->prior = s;
}
s->prior = p;
p->next = s;
return true;
}
bool Delete_IndexNode(DLinklist L, int i, ElemType& e)
{
DNode* p = GetElem(L, i - 1);
if (NULL == p) return false;
DNode* q = p->next;
if (NULL == q) return false;
e = q->Data;
p->next = q->next;
if (q->next != NULL)
{
q->next->prior = p;
}
free(q);
q = NULL;
return true;
}
bool printList(DLinklist L)
{
if (NULL == L)
{
printf("no Node");
return false;
}
DNode*p = L->next;
if (NULL == p) return false;
while (p)
{
printf("%4d", p->Data);
p = p->next;
}
printf("\n");
List_length(L);
return true;
}
void DestoryList(DLinklist& L)
{
while (L->next != NULL)
{
/*Delete_NextNode(L->next);*/
DNode* p = L->next;
L->next = p->next;
if (p->next != NULL)
{
p->next->prior = L;
}
free(p);
p = NULL;
}
free(L);
L = NULL;
}
//void Traverse_List(DLinklist L)//遍历
//{
// while (p != NULL)向后遍历
// {
// ....;
// p = p->next;
// }
// while (p != NULL)向前遍历
// {
// ...;
// p = p->prior;
// }
// while (p->prior != NULL)只对包含数据元素进行遍历(即不带头节点)
// {
// ...;
// p = p->prior;
// }
//}
void List_length(DLinklist L)
{
if (NULL == L->next)
{
printf("length = 0\n");
return;
}
int j = 0;
L = L->next;
while (L)
{
j++;
L = L->next;
}
printf("length = %d\n", j);
}
int main()
{
DLinklist L;
initiList(L);
Empty(L);
List_TailInsert(L);
printList(L);
/*DNode* ret = LocateElem(L, 8);
if (ret)
printf("%d", ret->Data);
else
printf("fail");*/
List_IndexInsert(L, 7, 60);
printList(L);
ElemType e;
Delete_IndexNode(L, 1, e);
printf("e = %d\n", e);
printList(L);
DestoryList(L);
printList(L);
return 0;
}
双链表-简单操作
于 2022-03-24 15:17:26 首次发布