单链表学习

创建一个学生结构体数组 存放四个学生的信息 循环调入插入函数,建立整表

任意插入一个新生;

任意删除一个学生

单链表逆置;

#include"link.h"
int main(int argc, const char *argv[])
{
	pnode L=get_head();
	int i;
	for(i=0;i<4;i++)
	{
	head_insert(L);
	
	
	}
	node_output(L);
	student e={20,80,"无敌"};
	any_pos_insert(L,2,e);
	node_output(L);
	any_pos_dele(L,3);
	node_output(L);
	find(L);
	node_output(L);


	return 0;
} 

#include"link.h"
pnode get_head()
{
	pnode p = malloc(sizeof(Node));
	if(p==NULL)
	{
		printf("申请头结点失败\n");
	
		return NULL;
	}
	p->len=0;
	p->next=NULL;
	return p;
}
int head_insert(pnode L)
{
	pnode t=malloc(sizeof(student));
	printf("输入学生信息:\n");
	scanf("%d%d%s",&(t->data.age),&(t->data.score),t->data.name);
//	printf("%d %d %s",t->data.age,t->data.score,t->data.name);
	t->next=L->next;
	L->next=t;
	L->len++;
//	printf("%d %d %s",t->data.age,t->data.score,t->data.name);



}
int node_output(pnode L)
{
	int i;
	pnode t =L;
	for(i=0;i<L->len;i++)
	{
	
		t=t->next;
		printf("%d %d %s\n",t->data.age,t->data.score,t->data.name);

	
	
	
	}
	




}
int any_pos_insert(pnode L,int a,student e)
{
	int i;
	pnode Q=malloc(sizeof(student));
	pnode p=L;

//printf("输入插入的新学生信息;\n");
	//scanf("%d%d%s",&e.age,&e.score,e.name);
	for(i=0;i<a-1;i++)
	{
		p=p->next;
	
	
	}
	Q->data=e;
	Q->next=p->next;
	p->next=Q;
	L->len++;
	
	


}
int any_pos_dele(pnode L,int pos)
{
	int i;
	pnode p=L;
	for(i=0;i<pos-1;i++)
	{
		p=p->next;
	
	
	}
	pnode Q=p->next;
	p->next=Q->next;
	free(Q);
	Q=NULL;
	L->len--;
}
int find(pnode L)
{
	pnode Q=L->next->next;
	pnode t=L->next;
	while(Q!=NULL)
	{
		t->next=Q->next;
		Q->next=L->next;
		L->next=Q;
		Q=t->next;
	
	
	
	}
}


#ifndef _LINK_H_
#define _LINK_H_
#include<myhead.h>
typedef struct stu
{
	int age;
	int score;
	char name[20];

}student;
typedef struct node
{
	union
	{
		int len;
		student data;
	};
	struct node *next;


}Node,*pnode;
pnode get_head();
int head_insert(pnode L);
int node_output(pnode L);
int any_pos_insert(pnode L,int a,student e);
int any_pos_dele(pnode L,int pos);
int find(pnode L);

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值