单项链表及双向链表的简单操作

博客介绍了单项链表和双向链表的相关操作。单项链表包括建立、插入、删除节点、头插法、尾插法、排序及输出函数、释放节点;双向链表涵盖构建、有序插入、头插法、尾插法、删除和输出操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单项链表的 建立、插入(有序,构造有序链表),删除节点,头插法,尾插法,排序(交换数据)及输出函数,释放节点;

#include<stdio.h>
#include<stdlib.h>
struct node{
	int  data;
	struct node *next;
};
struct node *head=NULL,*t,*q,*p;
void built()//建立链表; 
{
	while(1)
	{
		p=(struct node*)malloc(sizeof(struct node));//calloc(1,sizeof(struct node))
		scanf("%d",&p->data);
		if(p->data<=0) break; 
		p->next=NULL;
		if(head==NULL) head=p;
		else q->next=p;
		q=p;
	}
	return ;
}
void hadd()//头插; 
{
	p=(struct node*)malloc(sizeof(struct node));
	scanf("%d",&p->data);
	p->next=NULL;
	if(head==NULL) head=p;
	else 
	{
		p->next=head;
		head=p;
	}
	return ;
}
void tadd()//尾插; 
{
	p=(struct node*)malloc(sizeof(struct node));
	scanf("%d",&p->data);
	p->next=NULL;
	if(head==NULL) head=p;
	else 
	{
		t=head;
		while(t->next!=NULL) t=t->next;
		t->next=p;
	}
	return ;
}
void print()//输出函数; 
{
	t=head;
	while(t!=NULL)
	{
		printf("%d ",t->data);
		t=t->next;
	}
	return ;
}
void input()//有序插入; 
{
	p=(struct node*)malloc(sizeof(struct node));
	scanf("%d",&p->data);
	p->next=NULL;
	if(head==NULL) head=p;
	else 
	{
		t=head;
		while(t!=NULL)
		{
			if(t->next==NULL||t->next->data>p->data)
			{
				p->next=t->next;
				t->next=p;
				break;
			}
			t=t->next;
		}
	}
	return ;
}
void delet()//删除; 
{
	p=(struct node*)malloc(sizeof(struct node));
	scanf("%d",&p->data);
	t=head;
	if(t->data==p->data&&t==head)
	{ head=head->next; return ;}
	while(t->next!=NULL)
	{
		if(t->next->data==p->data)
		{
			t->next=t->next->next;
			return ;
		}
		t=t->next;
	}
	printf("error\n");
	return ;
}
void sort()//排序(交换数据) ; 
{
	t=head;
	while(t!=NULL)
	{
		p=t;
		while(p->data>p->next->data)
		{
			int  temp=p->data;
			p->data=p->next->data;
			p->next->data=temp;
		}
		t=t->next;
	}
	return ; 
}
void Free()//释放节点; 
{
	t=head;
	while(head!=NULL)
	{
		free(t);
		head=head->next;
		t=head;
	}
	return ;
}
int main()
{
	built();
	//hadd();
	//tadd();
	//input();
	delet();
	print();
	Free();
	return 0;
}

双向链表的构建,有序插入,头插法,尾插法,删除,输出操作;

#include<stdio.h>
#include<stdlib.h>
struct node{
	int data;
	struct node *next;
	struct node *prev;
};
struct node *head,*p,*q,*t,*s;
void built()//建立双向链表; 
{
	q=NULL;
	while(1)
	{
		p=(struct node*)malloc(sizeof(struct node));
		scanf("%d",&p->data);
		if(p->data<=0) break;
		p->next=NULL; p->prev=NULL;
		if(head==NULL) head=p;
		else 
		{
			q->next=p;
			p->prev=q;
		}
		q=p;
	}
	return ;
}
void hadd()//头插; 
{
	p=(struct node*)malloc(sizeof(struct node));
	scanf("%d",&p->data); p->next=NULL;p->prev=NULL;
	if(head==NULL) 
	{
		head=p;
		return ;
	}
	p->next=head;
	head->prev=p;
	head=p;
	return ;
}
void tadd()//尾插; 
{
	p=(struct node*)malloc(sizeof(struct node));
	scanf("%d",&p->data); p->next=NULL;p->prev=NULL;
	t=head; 
	if(head==NULL) 
	{
		head=p;
		return ;
	}
	while(t->next!=NULL) t=t->next;
	t->next=p;
	p->prev=t;
	return ;
}
void input()//有序插入(建立有序链表) 
{
	p=(struct node*)malloc(sizeof(struct node));
	scanf("%d",&p->data); p->next=NULL;p->prev=NULL;
	t=head;
	if(head==NULL) head=p;
	else
	{
		if(head->data>p->data) 
		{
			p->next=head;
			head->prev=p;
			head=p;
			return ;
		}
		while(t!=NULL)
		{
			if(t->next==NULL||t->next->data>p->data)
			{
				t->next->prev=p;p->next=t->next;
				t->next=p;
				p->prev=t;
				t=p;
				break;
			}
			t=t->next;
		}
	}
	return ;
}
void delet()//删除; 
{
	p=(struct node*)malloc(sizeof(struct node));
	scanf("%d",&p->data); p->next=NULL;p->prev=NULL;
	t=head;
	if(head->data==p->data) 
	{
		head->next->prev=NULL;
		head=head->next;
		return ;
	}
	while(t->next!=NULL)
	{
		if(t->next->data==p->data)
		{
			if(t->next->next==NULL)
			{
				t->next->prev=NULL;
				t->next=NULL;
				return ;
			}
			t->next->next->prev=t;
			t->next=t->next->next;
			return ;
		}
		t=t->next;
	}
	printf("error\n");
	return ;
}
void print()//输出函数; 
{
	int flag=1;
	t=head;
	while(t!=NULL)
	{
		printf("%d ",t->data);
		if(t->next==NULL) p=t;
		t=t->next;
	}
	while(p!=NULL)
	{
		printf("%d ",p->data);
		p=p->prev;
	}
	return ;
}
int main()
{
	built();
	//hadd();
	tadd();
	//input();
	//delet();
	print();
	return 0;
} 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值