#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#pragma warning (disable:4996)
typedef int SLDatatype;
//结点
typedef struct SListNode
{
struct SListNode* prev;
SLDatatype data;
struct SListNode* next;
}SListNode;
//一个双向链表初定义
void InitdoubleList(SListNode** firstnode)
{
(*firstnode) = BuydoubleSList(0);
(*firstnode)->next = (*firstnode);
(*firstnode)->prev = (*firstnode);
}
//购买一个双向链表
SListNode* BuydoubleSList(SLDatatype x)
{
SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
if (newnode == NULL)
{
printf("建立失败!\n");
assert(newnode);
}
newnode->next = NULL;
newnode->prev = NULL;
newnode->data = x;
return newnode;
}
//尾插
void pushbackdouble(SListNode**head, SLDatatype x)
{
assert(*head);
SListNode* tail = (*head)->prev;
SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
if (newnode == NULL)
{
printf("建立失败!\n");
assert(newnode);
}
newnode->next = NULL;
newnode->prev = NULL;
newnode->data = x;
tail->next = newnode;
newnode->prev = tail;
newnode->next = *head;
(*head)->prev = newnode;
}
//尾删
void popback(SListNode* head)
{
assert(head);
SListNode* tail = head->prev;
SListNode* tailprev = tail->prev;
tailprev->next = head;
head->prev = tailprev;
free(tail);
tail=NULL;
}
//头插
void pushfront(SListNode**head, SLDatatype x)
{
assert(*head);
assert(*head->next != head);
SListNode *first = head->next;
SListNode *second = first->next;
phead->next = second;
second->prev = head;
free(first);
}
//头删
void popfront(SListNode**head)
//双向链表的打印
void printfList(SListNode* head)
{
assert(head);
SListNode* cur = head->next;
while (cur != head)
{
printf("%d",cur->data);
cur = cur->next;
}
}
int main()
{
SListNode* head = NULL;
InitdoubleList(&head);
system("pause");
return 0;
}
关于双向链表的基本建立
最新推荐文章于 2022-08-04 10:02:20 发布