数据结构---双向链表的基本操作

  1. 头插法
  2. 遍历链表
  3. 尾插法
  4. 头删法
  5. 尾删法
  6. 按位置插入数据
  7. 按位置删除数据

dooublelinklist.c

cs 复制代码
#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

cs 复制代码
#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

cs 复制代码
#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; 
}
相关推荐
摇滚侠2 小时前
StreamAPI,取出list中的name属性,返回一个新list
数据结构·list
是苏浙5 小时前
零基础入门C语言之C语言实现数据结构之单链表经典算法
c语言·开发语言·数据结构·算法
橘颂TA5 小时前
【剑斩OFFER】算法的暴力美学——点名
数据结构·算法·leetcode·c/c++
迷途之人不知返6 小时前
数据结构之,栈与队列
数据结构
MOONICK9 小时前
数据结构——哈希表
数据结构·哈希算法·散列表
FMRbpm10 小时前
链表5--------删除
数据结构·c++·算法·链表·新手入门
努力学习的小全全12 小时前
【CCF-CSP】05-01数列分段
数据结构·算法·ccf-csp
遗憾是什么.12 小时前
数据结构 -- 栈
数据结构·算法·链表
liuhuapeng030413 小时前
GetMapping自动截取List<String>字符
数据结构·windows·list
仰泳的熊猫13 小时前
1013 Battle Over Cities
数据结构·c++·算法·pat考试