C语言 不带头结点的双链表的实现和相关操作。

C语言 不带头结点的双链表的实现和相关操作。

#include<stdio.h>
#include<stdlib.h>
typedef int DAT;
typedef struct node
{
	struct node *prior;//指向前一节点 
	DAT data;//数据 
	struct node *next;//指向后一节点 
}Node;
void init_list(Node **pL)//初始化链表
{
	(*pL)=NULL;
 }
Node* mack_node(DAT data)//重建新的节点
{
	Node *newnode=(Node*)malloc(sizeof(Node));
	if(newnode==NULL)
	{
		printf("内存分配失败");
		return; 
	}
	newnode->prior =NULL;
	newnode->data =data;
	newnode->next =NULL;
	return newnode;
 } 
void push_back_list(Node **pL,DAT data)//尾插法插入链表
{
	Node *newnode=mack_node(data);
	if((*pL)==NULL)
	{
		(*pL)=newnode;
		return;
	}
	Node *cur=(*pL);
	while(cur->next)
	{
		cur=cur->next;
	}
	cur->next=newnode;
	newnode->prior=cur;
 }
void push_front_list(Node **pL,DAT data)//头插法插入链表
{
	Node *newnode=mack_node(data);
	if((*pL)==NULL)
	{
		(*pL)=newnode;
		return;
	}
	newnode->next=(*pL);
	(*pL)->prior=newnode;
	(*pL)=newnode;
 }
void pop_front_list(Node **pL)//头删法删除链表
{
	if((*pL)==NULL)
	{
		printf("链表为空,删除失败。");
		return; 
	}
	Node *cur=(*pL); 
	if(cur->next==NULL)
	{
		free(cur);
		(*pL)=NULL;
		cur=NULL;
		return;
	}
	(*pL)=cur->next;
	(*pL)->prior=NULL;
	free(cur);
	cur=NULL;
 }
void pop_back_list(Node **pL)//尾删法删除链表
{
	if((*pL)==NULL)
	{
		printf("链表为空,删除失败。");
		return; 
	}
	Node *cur=(*pL);
	Node *pre=NULL; 
	if(cur->next==NULL)
	{
		free(cur);
		(*pL)=NULL;
		cur=NULL;
		return;
	}
	while(cur->next)
	{
		pre=cur;
		cur=cur->next;
	}
	free(cur);
	pre->next=NULL;
	cur=NULL;
 } 
void destroy_list(Node **pL)//销毁链表
{
	if((*pL)==NULL)
	return;
	Node *cur=(*pL);
	Node *pre=NULL;
	while(cur)
	{
		pre=cur;
		free(pre);
		pre=NULL;
		cur=cur->next;
	 } 
	(*pL)=NULL;
 } 
void print_list(Node *pL)//打印链表
{
	if(pL==NULL)
	{
		printf("空链表\n");
		return;
	}
	Node *cur=pL;
	printf("NULL");
	while(cur)
	{
		printf("<- %d -> ",cur->data);
		cur=cur->next;
	 }
	 printf("NULL\n");
 } 
int main()
{
	Node *first;
	init_list(&first);//初始化链表 
	print_list(first);//打印链表
	push_back_list(&first,1);//尾插法插入链表
	print_list(first);//打印链表
	push_back_list(&first,12);//尾插法插入链表
	print_list(first);//打印链表	
	push_back_list(&first,123);//尾插法插入链表
	print_list(first);//打印链表	
	push_front_list(&first,4);//头插法插入链表 
	print_list(first);//打印链表
	push_front_list(&first,45);//头插法插入链表 
	print_list(first);//打印链表	
	push_front_list(&first,456);//头插法插入链表 
	print_list(first);//打印链表	
	pop_front_list(&first);//头删法删除链表 
	print_list(first);//打印链表
	pop_back_list(&first);//尾删法删除链表
	print_list(first);//打印链表
	destroy_list(&first);//销毁链表
	print_list(first);//打印链表	 
	return 0;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以帮你解答关于C语言实现带头点的链表的问题。你可以使用构体来定义节点,同时在头文件中声明头点,并在初始化函数中为头点分配空间,这样就可以方便地插入、删除和遍历链表了。具体实现可以参考下面的代码: ``` #include <stdio.h> #include <stdlib.h> //定义构体,包含两个成员:数据和下一个节点的指针 typedef struct node { int data; struct node *next; } Node; //头文件中声明头点 Node *head; //初始化函数,为头点分配空间 void init() { head = (Node *)malloc(sizeof(Node)); head->next = NULL; } //插入函数 void insert(int x) { Node *p = head; Node *new_node = (Node *)malloc(sizeof(Node)); if (new_node == NULL) { printf("Error: memory allocation failed!\n"); exit(1); } new_node->data = x; new_node->next = NULL; while (p->next != NULL) { p = p->next; } p->next = new_node; } //删除函数 void delete(int x) { Node *p = head->next; Node *prev = head; while (p != NULL) { if (p->data == x) { prev->next = p->next; free(p); return; } prev = p; p = p->next; } printf("Error: element not found!\n"); } //遍历函数 void traverse() { Node *p = head->next; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); } //主函数 int main() { init(); insert(1); insert(2); insert(3); traverse(); delete(2); traverse(); return 0; } ``` 希望这能够帮助到你!如果你还有其他问题,可以继续问我哦。现在请听一个不太好笑的笑话:为什么程序员总是固执地认为 Unix 是最好的操作系统?因为他们自己编译了它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值