c语言:双向链表的增删查改(带头节点)

头文件

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct list {
	int data;
	struct list* next;
	struct list* prev;
}SL;
void listinit(SL** phead);
void listpushback(SL** phead, int x);//尾插
void listprintf(SL* phead);
void listpopback(SL** phead);
void listpushfront(SL** head, int x);
void listpopfront(SL** head);
SL* listfindpos(SL** head, int x);//查找某个数,并返回节点
void listerase(SL** pos);//删除某个节点
void listdestroy(SL** phead);//销毁双向链表

主函数

#include"main.h"

void listinit(SL** phead)//初始化双向链表
{
	*phead = (SL*)malloc(sizeof(SL));
	if (*phead == NULL)
	{
		return;
	}
	(*phead)->next = *phead;
	(*phead)->prev = *phead;
}
void listpushback(SL** phead, int x)//尾插
{
	if (phead == NULL) return;
	SL* tail = (*phead)->prev;
	SL*newnode= (SL*)malloc(sizeof(SL));
	if (newnode == NULL)
	{
		return;
	}
	newnode->data = x;
	tail->next = newnode;
	newnode->prev = tail;
	newnode->next =*phead;
	(*phead)->prev = newnode;
}
void listprintf(SL* phead)//打印 
{
	if (phead == NULL) return;
	SL* cur = phead->next;
	while (cur != phead) {
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
void listpopback(SL** phead)//尾删
{
	assert(*phead);
	assert((*phead)->next);
	SL* tail = (*phead)->prev;
	(*phead)->prev = tail->prev;
	tail->prev->next = *phead;
	free(tail);
}
void listpushfront(SL** head, int x)//头插
{
	assert(*head);
	SL* front = (*head)->next;
	SL* newnode = (SL*)malloc(sizeof(SL));
	newnode->data = x;
	(*head)->next = newnode;
	newnode->prev = *head;
	newnode->next = front;
	front->prev = newnode;

}
void listpopfront(SL** head)//头删
{
	assert(*head);
	assert((*head)->next);
	SL* front = (*head)->next;
	SL* afterfront = front->next;
	(*head)->next = afterfront;
	afterfront->prev = *head;
	free(front);
}
SL* listfindpos(SL** head, int x)//查找某个数,并返回节点
{
	SL* cur = (*head)->next;
	while (cur != head)
	{
		if (cur != head)
		{
			if (cur->data == x)
			{

				return cur;

			}
			cur = cur->next;

		}


	}
	return NULL;
}
	void listinsert(SL**pos,int x)  //在某个节点前插入数据
	{
		assert(*pos);
		SL* posprev = (*pos)->prev;
		SL* newnode = (SL*)malloc(sizeof(SL));
		if (newnode == NULL)
		{
			return;
		}
		posprev->next = newnode;
		newnode->prev = posprev;
		newnode->next = pos;
	}
	void listerase(SL** pos)//删除某个节点
	{
		assert(pos);
		SL* prev = (*pos)->prev;
		SL* next = (*pos)->next;
		prev->next = next;
		next->prev = prev;
		free(*pos);
		*pos = NULL;
	}
	void listdestroy(SL**phead)//销毁双向链表
	{
		assert(*phead);
		SL* cur =(*phead)->next;
		while (cur != *phead)
		{
			SL* next = cur->next;
			free(cur);
			cur = next;
		}
		free(phead);
		phead = NULL;


	}

测试

#include"main.h"
test1()
{
    SL* head;
    listinit(&head);
    listpushfront(&head, 2);
    listpushback(&head, 1);
    listpushback(&head, 1);
    listpushback(&head, 1);
    listpopfront(&head);
    listpopback(&head);
    listprintf(head);
    
}




int main() {
   
    test1();

   
    return 0;
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值