#include <stdio.h>
#include <stdlib.h>
struct doubleList {
int data;
struct doubleList* front;
struct doubleList* rear;
};
struct doubleList* createList()
{
struct doubleList* headNode = (struct doubleList *)malloc(sizeof(struct doubleList));
headNode->front = headNode->rear = headNode;
return headNode;
}
struct doubleList* createNode(int data)
{
struct doubleList* newNode = (struct doubleList *)malloc(sizeof(struct doubleList));
newNode->data = data;
newNode->front = newNode->rear = NULL;
return newNode;
}
void insertNode_head(struct doubleList* headNode, int data)
{
struct doubleList* newNode = createNode(data);
newNode->front = newNode;
newNode->rear = headNode->rear;
headNode->rear->front = newNode;
headNode->rear = newNode;
}
void insertNode_rear(struct doubleList* headNode, int data)
{
struct doubleList* newNode = createNode(data);
struct doubleList* lastNode = headNode;
while(lastNode->rear != headNode)
{
lastNode = lastNode->rear;
}
headNode->front = newNode;
newNode->rear = headNode;
lastNode->rear = newNode;
newNode->front = lastNode;
}
void deleteNode_point(struct doubleList* headNode, int posdata)
{
struct doubleList* posNode = headNode->rear;
struct doubleList* posNodeFront = headNode;
while(posNode->data != posdata)
{
posNodeFront = posNode;
posNode = posNodeFront->rear;
if(posNode->rear == headNode)
{
return;
}
}
posNodeFront->rear = posNode->rear;
posNode->rear->front = posNodeFront;
free(posNode);
}
void printList_rear(struct doubleList* headNode)
{
struct doubleList* t = headNode->rear;
while(t != headNode)
{
printf("%d", t->data);
t = t->rear;
}
printf("\n");
}
void printList_front(struct doubleList* headNode)
{
struct doubleList* t = headNode->front;
while(t != headNode)
{
printf("%d", t->data);
t = t->front;
}
printf("\n");
}
int main()
{
struct doubleList* list = createList();
insertNode_head(list, 1);
insertNode_head(list, 2);
insertNode_head(list, 3);
printList_rear(list);
insertNode_rear(list, 0);
printList_rear(list);
deleteNode_point(list, 3);
printList_rear(list);
return 0;
}
双向循环链表C语言
最新推荐文章于 2024-01-14 23:42:18 发布