// Created on iPad.
#include <iostream>
using namespace std;
typedef struct DulNode{
int data;
struct DulNode *prior;
struct DulNode *next;
}DulNode;
//初始化链表
void InitList(DulNode *head,int size){
DulNode *rear=head;
rear->prior=NULL;
for(int i=2;i<=size;i++){
DulNode *newnode=(DulNode*)malloc(sizeof(DulNode));
newnode->next=NULL;
newnode->data=i;
rear->next=newnode;
newnode->prior=rear;
rear=newnode;
}
rear->next=head;
head->prior=rear;
}
//插入元素,头插法
void Listinsert_dul(DulNode *head,int i,int e){
int j=0;
DulNode *p=head;
DulNode *newnode=(DulNode*)malloc(sizeof(DulNode));
while(p!=NULL&&j<i-1){
p=p->next;
j++;
}
newnode->data=e;
newnode->prior=p;
newnode->next=p->next;
p->next->prior=newnode;
p->next=newnode;
}
//遍历链表
void travelLink(DulNode *head){
DulNode *p=head->next;
while(p!=head){
printf("%d\t",p->data);
p=p->next;
}
cout<<endl;
}
//查询链表
DulNode query(DulNode *head,int i){
DulNode *p=head;
while(p&&p->data!=i)
p=p->next;
return p;
}
//删除元素
void delete_dul(DulNode *head,int i){
DulNode *p=head->next;
while(p&&p->data!=i)
p=p->next;
p->next->prior=p->prior;
p->prior->next=p->next;
delete p;
}
int main() {
DulNode *head=(DulNode*)malloc(sizeof(DulNode));
head->prior=NULL;
head->next=NULL;
return 0;
}
C语言双向循环链表
最新推荐文章于 2024-10-19 02:13:46 发布