链表基本操作(完整代码)

链表基本操作(完整代码)

首先汇总一下单链表的基本操作(带头结点)

1.单链表的创建
(1)头插法创建链表 (2)尾插法创建链表
2.单链表的打印
3.单链表的查询
4.结点插入
(1)头部插入 (2)任意位置插入 (3)尾部插入
5.结点删除


更加详细请看链表基本操作(详解).


完整代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
	int number;    //学号 
	char name[20];  //姓名 
	struct student *next;
};

struct student *init()  //头结点初始化 
{
	struct student *head;
	head = (struct student *)malloc(sizeof(struct student));
	head->next = NULL;
	return head;
	
}

void Creat(struct student *head)  //链表的创建(尾插法) 
{
	struct student *s,*r;
	r = head;
	int num;
	char name[20];
	while(1)
	{
	printf("please input the name:\n");
	scanf("%s",name);
	printf("please input the number:\n");
	scanf("%d",&num);
	if(num <= 0)
	{
		break;
	}
	s = (struct student *)malloc(sizeof(struct student));
	strcpy(s->name ,name);
	s->number = num;
	//尾插法
	r->next = s;
	r = s;
	}
	s->next = NULL;
}

//void Creat(struct student *head) //链表创建(头插法) 
//{
//	struct student *s;
//	int num;
//	char name[20];
//	while(1)
//	{
//		printf("please input the name:\n");
//		scanf("%s",name);
//		printf("please input the number:\n");
//		scanf("%d",&num);
//		if(num <= 0)
//		{
//			break;
//		}
//		s = (struct student *)malloc(sizeof(struct student));
//		strcpy(s->name ,name);
//		s->number = num;
//		//
//		s->next = head->next ;
//		head->next = s;
//	}
//}

struct student *search(struct student *head, int pos)  //查询  pos查询位置 
{
	int n;
	struct student *temp = head  ;
	int i=0;
	while(temp && i < pos)
	{
		temp = temp->next ;
		i++;
	}
	if(temp == NULL) 
	{
		printf("error\n");
	}
	return temp;
}


void print(struct student *head)  //打印 
{
	struct student *temp = head->next ;
	printf("******信息如下:******\n");
	while(temp)
	{
		printf("%s\n",temp->name);
		printf("%d\n",temp->number );
		printf("\n");
		temp = temp->next ;
	}
}

void insert(struct student *head)   //任意位置插入 
{
	int n;
	printf("please input the insert number:\n");
	scanf("%d",&n);
	struct student *p = search(head,n-1);
	struct student *s = p->next;
	s = (struct student *)malloc(sizeof(struct student));
	printf("please input the insert name:\n");
	scanf("%s",&s->name );
	printf("please input the insert number:\n");
	scanf("%d",&s->number );
	// 
	s->next = p->next ;
	p->next = s;
}

//void insert (struct student *head) //头部插入 
//{
//	struct student *s;
//	s = (struct student *)malloc(sizeof(struct student));
//	printf("please input the insert name:\n");
//	scanf("%s",&s->name );
//	printf("please input the insert number:\n");
//	scanf("%d",&s->number );
//	//
//	s->next = head->next ;
//	head->next = s;
//}

//void insert (struct student *head)  //尾部插入 
//{
//	int i = 0;
//	struct student *s,*r;
//	r = head;
//	while(r->next && r )
//	{
//		r = r->next ;
//		i++;
//	}
//	s = (struct student *)malloc(sizeof(struct student));
//	printf("please input the insert name:\n");
//	scanf("%s",&s->name );
//	printf("please input the insert number:\n");
//	scanf("%d",&s->number);
//	//
//	r->next = s;
//	s->next = NULL;
//}

void length(struct student *head)
{
	struct student *temp = head->next ;
	int cnt = 0;
	while(temp)
	{
		temp = temp->next ;
		cnt++;
	}
	printf("一共%d个成员\n",cnt);
}

void Delete(struct student *head)  //删除 
{
	struct student *p,*s;
	int n;
	printf("please input the delete the number:\n");
	scanf("%d",&n);
	p = search(head,n-1);
	s = p->next ;
	if(p ==NULL || p->next ==NULL)
	{
		printf("error\n");	
	} 
	else
	//
	p->next = s->next ;
	free(s);
}

int main()
{
	int n;
	struct student *head = init();   //初始化 
	Creat(head);                     //链表创建 (2种) 
	print(head);					//打印 
	length(head);					//链表长度 
	printf("please input the search number:\n");
	scanf("%d",&n);
	struct student *p = search(head,n);
	printf("%s %d\n",p->name ,p->number );
	insert(head);					//链表插入(3种) 
	print(head);
	Delete(head);					//删除 
	print(head);				
}


  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值