#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//3 4 5 6 7 8 9 10 - 9999
typedef int ElemType;
typedef struct DCNode {
ElemType data;
struct DCNode* prior, *next;
}DCNode,*LinkList;
bool Empyt(LinkList L)
{
return (L->next == L);
}
bool List_TailInsert(LinkList& L)
{
L = (LinkList)malloc(sizeof(DCNode));
if (NULL == L) return false;
L->prior = L;
L->next = L;
DCNode* p = L;
ElemType e;
printf("INPUT number:\n");
scanf("%d", &e);
while (e != -9999)
{
DCNode* s = (DCNode*)malloc(sizeof(DCNode));
if (NULL == s) return false;
s->data = e;
s->next = p->next;
p->next->prior = s;
s->prior = p;
p->next = s;
p = s;
scanf("%d", &e);
}
return true;
}
DCNode* GetElem(LinkList L, int i)
{
int j = 1;
if (i == 0 || NULL == L->next) return L;
if (i < 1) return NULL;
DCNode* p = L->next;
while (p && j < i)
{
p = p->next;
j++;
}
return p;
}
bool List_IndexInsert(LinkList L, int i, ElemType e)
{
DCNode* p = GetElem(L, i - 1);
if (NULL == p) return false;
DCNode* s = (DCNode*)malloc(sizeof(DCNode));
if (NULL == s) return false;
s->data = e;
s->next = p->next;
p->next->prior = s;
s->prior = p;
p->next = s;
return true;
}
bool printList(LinkList L)
{
if (NULL == L->next) return false;
DCNode* p = L;
printf("尾节点为:%d\n", p->prior->data);
p = p->next;
//判断当前节点是否为尾节点 p->next==L;true 则为尾节点
while (p !=L)
{
printf("%4d", p->data);
p = p->next;
}
printf("\n");
printf("第一个节点数据元素为:%d\n", p->next->data);
return true;
}
int main()
{
LinkList L;
List_TailInsert(L);
printList(L);
List_IndexInsert(L, 3, 60);
printList(L);
return 0;
}
简易循环双链表-带头结点
于 2022-03-24 16:25:33 首次发布