- 头插法
- 遍历链表
- 尾插法
- 头删法
- 尾删法
- 按位置插入数据
- 按位置删除数据
dooublelinklist.c
#include <stdio.h>
#include <stdlib.h>
#include "./doublelinklist.h"
doublelinklist* create_doublelinklist(void)
{
doublelinklist* head = (doublelinklist*)malloc(sizeof(doublelinklist));
if(NULL == head)
{
printf("头结点申请失败!\n");
return NULL;
}
head->text.len = 0;
head->next = NULL;
head->prev = NULL;
return head;
}
//头插法
int insertHead_doublelinlist(doublelinklist *head,dataType num)
{
//创建一个新结点
doublelinklist* temp = (doublelinklist*)malloc(sizeof(doublelinklist));
if(NULL == temp)
{
printf("创建失败!\n");
return 0;
}
temp->text.data = num;
temp->next = NULL;
temp->prev = NULL;
if(NULL == temp)
{
printf("创建失败!\n");
return 0;
}
if(1 == isEmpty_doublelinlist(head))
{
temp->next = head->next;
head->next = temp;
}
else
{
//头插法插入数据
temp->next = head->next;
head->next = temp;
head->next->prev = temp;
temp->prev = head;
}
return 0;
}
//遍历链表
void show_doublelinklist(doublelinklist* head)
{
doublelinklist *p = head;
while(p->next != NULL)
{
p = p->next;
printf("%d ",p->text.data);
}
printf("\n");
//更新头结点中记录的链表长度
head->text.len++;
return;
}
//尾插法
int insertTail_doublelinlist(doublelinklist* head,dataType num)
{
doublelinklist* temp = (doublelinklist*)malloc(sizeof(doublelinklist));
if(NULL == temp)
{
printf("创建失败!\n");
return 0;
}
//初始化新结点
temp->text.data = num;
temp->next = NULL;
temp->prev = NULL;
doublelinklist* p = head;
while(p->next != NULL)
{
p = p->next;
}
temp->prev = p;
p->next = temp;
head->text.len++;
return 0;
}
//判空
int isEmpty_doublelinlist(doublelinklist* head)
{
return head->next == NULL?1:0;
}
//按位置插入
void insert_location_doublelinlist(doublelinklist*head,dataType index,dataType num)
{
if(index<1 || index >head->text.len+1)
{
printf("位置非法!");
return;
}
int i;
doublelinklist* temp=head;
doublelinklist* p = (doublelinklist*)malloc(sizeof(doublelinklist));
if(NULL == p)
{
printf("插入失败!\n");
return ;
}
//初始化新结点
p->text.data = num;
p->next = NULL;
p->prev = NULL;
for(i=0;i<index-1;i++)
{
temp = temp->next;
}
if(temp->next == NULL)
{
p->next = temp->next;
temp->next = p;
p->prev = temp;
}else
{
p->next = temp->next;
p->prev = temp;
temp->next->prev = p;
temp->next = p;
}
head->text.len++;
return;
}
//头删
dataType delHead_doublelinlist(doublelinklist* head)
{
if(isEmpty_doublelinlist(head))
{
printf("链表为空!");
return (dataType)0;
}
doublelinklist* temp = head->next;
head->next = head->next->next;
temp->next->prev = head;
dataType num = temp->text.data;
free(temp);
temp = NULL;
head->text.len--;
return num;
}
//尾删
dataType delTail_doublelinlist(doublelinklist* head)
{
if(isEmpty_doublelinlist(head))
{
printf("链表为空!");
return (dataType)0;
}
doublelinklist* p = head;
doublelinklist* temp;
while(p->next != NULL) //p->next->next != NULL
{
temp = p;
p = p->next;
}
temp->next=temp->next->next;
free(p); //free(temp->next)
p = NULL; //temp->next = NULL
head->text.len--;
return 0;
}
//按位置删除
void del_location_doublelinlist(doublelinklist* head,dataType index)
{
if(isEmpty_doublelinlist(head))
{
printf("链表为空!");
return;
}
doublelinklist* temp = head;
for(int i=0;i<index-1;i++)
{
temp = temp->next;
}
doublelinklist* p = temp->next;
temp->next = temp->next->next;
p->prev->prev = temp;
free(p);
p = NULL;
head->text.len--;
return;
}
doublelinklist.h文章来源:https://www.toymoban.com/news/detail-836817.html
#ifndef __DOUBLELINKLIDT_H__
#define __DOUBLELINKLIDT_H__
typedef int dataType;
union msg{
dataType data;
int len;
};
typedef struct node
{
struct node* next;
struct node* prev;
union msg text;
}doublelinklist;
doublelinklist* create_doublelinklist(void);
int insertHead_doublelinlist(doublelinklist *head,dataType num);
void show_doublelinklist(doublelinklist* head);
int insertTail_doublelinlist(doublelinklist* head,dataType num);
int isEmpty_doublelinlist(doublelinklist* head);
void insert_location_doublelinlist(doublelinklist*head,dataType index,dataType num);
dataType delHead_doublelinlist(doublelinklist* head);
dataType delTail_doublelinlist(doublelinklist* head);
void del_location_doublelinlist(doublelinklist* head,dataType index);
#endif
doublemain.c文章来源地址https://www.toymoban.com/news/detail-836817.html
#include <stdio.h>
#include "./doublelinklist.h"
int main(int argc, const char *argv[])
{
doublelinklist* head = create_doublelinklist();
insertHead_doublelinlist(head,11);
insertHead_doublelinlist(head,22);
show_doublelinklist(head);
insertTail_doublelinlist(head,33);
insertTail_doublelinlist(head,44);
show_doublelinklist(head);
insert_location_doublelinlist(head,2,222);
show_doublelinklist(head);
delHead_doublelinlist(head);
show_doublelinklist(head);
delTail_doublelinlist(head);
show_doublelinklist(head);
del_location_doublelinlist(head,2);
show_doublelinklist(head);
return 0;
}
到了这里,关于数据结构---双向链表的基本操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!