带头结点的单链表的基本操作

本文介绍带头结点的单链表的基本操作,包括初始化、插入(头插法、尾插法)、删除、逆置、合并(递归与非递归)、排序及遍历等,并通过C语言实现这些操作。

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

带头结点的单链表的基本操作

带头结点的单链表的基本操作初始化、 插入(头插法,尾插法)、删除、逆置、合并(递归、非递归)、排序、遍历

/*
*
* 带头结点的单链表的基本操作
  初始化、 插入(头插法,尾插法)、删除、
  逆置、合并(递归、非递归)、排序、遍历
*
*/
#include <stdio.h>
#include <stdlib.h>

typedef struct node 
{
	int data;
	struct node *next;
}Node, *Linklist;

Linklist init()
{
	Node *head;
	head = (Node *)malloc(sizeof(Node));
	if(NULL==head)
	{
		printf("malloc failed....\n");
		return head;
	}
	head->next = NULL;
	return head;
}

Linklist insert_head(Node *head, int num)
{
	Node *p;
	p = (Node *)malloc(sizeof(Node));
	if(NULL==p)
	{
		printf("malloc failed...\n");
		return NULL;
	}

	p->data = num;
	p->next=head->next;
	head->next=p;
	return head;
}


Linklist insert_tail(Node *head, int num)
{
	Node *p=NULL, *t=NULL;
	p = (Node *)malloc(sizeof(Node));
	if(NULL==p)
	{
		printf("malloc failed...\n");
		return NULL;
	}
	p->data = num;
	t=head;

	while(t->next!=NULL)	
	{
		t = t->next;
	}
	t->next = p;
	p->next = NULL;
	return head;
}


Linklist delete(Node *head, int num)
{
	Node *p=NULL, *t=NULL;
	if(NULL==head || NULL==head->next)
	{
		return head;
	}
	p=head;
	while(p->next!=NULL)
	{
		if(p->next->data==num)
		{
			//	p->next = p->next->next;
			t = p->next;
			p->next = t->next;
			free(t);
		}
		p = p->next;
	}
	return head;
}


Linklist reverse(Node *head)
{
	Node  *p=NULL, *q=NULL;
	if(NULL==head || NULL==head->next)
	{
		return head;
	}
	p = head->next;
	q = p->next; //q=head->next->next;
	p->next = NULL;
	
	while(q!=NULL)
	{
		p = q->next;
		q->next = head->next;
		head->next = q;
		q = p;
	}
	return head;
}


Linklist merge(Node *head1, Node *head2)
{
	if(NULL==head1)
		return head2;
	if(NULL==head2)
		return head1;

	Node *head = NULL;
	if(head1->data < head2->data)
	{
		head = head1;
		head->next = merge(head1->next, head2);
	}
	else
	{
		head = head2;
		head->next = merge(head1, head2->next);
	}
	return head;
}


Linklist merge_list(Node *head1, Node*head2)
{
	if(NULL==head1)
		return head2;
	if(NULL==head2)
		return head1;
	Node *head = NULL, *p=NULL, *q=NULL;

	if(head1->data < head2->data)
	{
		head = head1;
		p = head1->next;
		q = head2;
	}
	else
	{
		head = head2;
		q = head2->next;
		p = head1;
	}

	Node *cur = head;
	while(p!=NULL && q!=NULL)
	{
		if(p->data <= q->data)
		{
			cur->next = p;
			cur = p;
			p = p->next;
		}
		else
		{
			cur->next = q;
            cur = q;
			q = q->next; 
		}
	}
	if(p!=NULL)
	{
		cur->next = p;
	}
	if(q!=NULL)
	{
		cur->next = q;
	}
	return head;
}


Linklist sort(Node *head)
{
	Node *p = NULL, *q = NULL;
	int i=0, j=0, t=0; 
	int	count = 0;
	if(NULL==head || NULL==head->next)
	{
		return head;
	}

	p = head->next;
	while(p!=NULL)
	{
		count++;
		p = p->next;
	}

	for(i=0; i<count-1; i++)
	{
		p = head->next;
		q = p->next;
		for(j=0; j<count-1-i; j++)
		{
			if(p->data > q->data)
			{	
				t = p->data;
				p->data = q->data;
				q->data = t;
			}
			p = p->next;
			q = q->next;
		}
	}
	return head;
}


Linklist traverse(Node *head)
{
	Node *p=NULL;
	if(NULL==head || NULL==head->next)
	{
		return head;
	}
	p=head->next;
	while(p!=NULL)
	{
		printf("  %d", p->data);
		p = p->next;
	}
	return head;
}


int main()
{
	Node *head1, *head2, *t;
	int i=0;
 
	head1 = init();
	for(i=0; i<10; i=i+2)
		insert_tail(head1, i);
	traverse(head1);
	printf("\n");
	 
	head2 = init();
	for(i=0; i<10; i=i+2)
		insert_head(head2, i);
	traverse(head2);
	printf("\n");

	sort(head2);
	traverse(head2);
	printf("\n");
	 
	t = merge(head1->next, head2);
	//t = merge_list(head1->next, head2);
	traverse(t);
	printf("\n");

	delete(t, 8);
	traverse(t);
	printf("\n"); 
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值