链表插入删除c语言程序

链表部分代码(插入,删除等等)

#include <stdio.h>
#include <stdlib.h>//This is heade function of "malloc"
int icount;

struct Student
{
	char name[20];
	int number;
	struct Student* next;
};/建立一个学生节点
struct Student* Creat()
{
	struct Student* phead=NULL;
	struct Student* pend;
	struct Student* pnew;
	pend=pnew=(struct Student*)malloc(sizeof(struct Student));
	scanf("%s",&pnew->name);
	printf("\n");
	scanf("%d",&pnew->number);
	icount=0;
	while(pnew->number!=0)
	{
		icount++;
		//printf("%d",icount);
		if(icount==1)
		{
			pnew->next=phead;
			pend=pnew;
			phead=pnew;
		}
		else 
		{
			pnew->next=NULL;
			pend->next=pnew;
			pend=pnew;		
		}

		pnew=(struct Student*)malloc(sizeof(struct Student));
		scanf("%s",&pnew->name);
		printf("\n");
		scanf("%d",&pnew->number);
	}
	free(pnew);
	return phead;
}
void Print(struct Studet* phead)
{
	struct Student* ptemp;
	int idex=1;
	ptemp=phead;
	while(ptemp!=NULL)
	{
		printf("NUMBER[%d]",idex);
		printf("%s,%d",ptemp->name,ptemp->number);
		printf("\n");
		ptemp=ptemp->next;
		idex++;	
	}
}
struct Student* Insert(struct Student* phead)
{
	struct Student* pnew;
	pnew=(struct Student*)malloc(sizeof(struct Student));
	scanf("%s",&pnew->name);
	printf("\n");
	scanf("%d",&pnew->number);

	pnew->next=phead;
	phead=pnew;
	icount++;
	return phead;
}
void Delete(struct Student* phead,int iIndex)//删除指定链表节点
{
	int i;
	struct Student* ptemp;
	struct Student* ppre;
	ptemp=phead;
	ppre=ptemp;
	printf("delete No_%d member----\n",iIndex);
	for(i=1;i<iIndex;i++)
	{
		ppre=ptemp;
		ptemp=ptemp->next;
	}
 	ppre->next=ptemp->next;
	free(ptemp);
	icount--;
}
void yd_charu(struct Student* phead,int iIndex)
{
	int i;
	struct Student* ptemp;
	struct Student* ppre;
	struct Student* p_new;
	ptemp=phead;
	ppre=ptemp;
	printf("Insert No_%d member----\n",iIndex);
	for(i=1;i<iIndex;i++)
	{
		ppre=ptemp;
		ptemp=ptemp->next;
	}
 	//ppre->next=ptemp;
	p_new=(struct Student*)malloc(sizeof(struct Student));//apply for new node
	scanf("%s",&p_new->name);
	printf("\n");
	scanf("%d",&p_new->number);
	p_new->next=ptemp;
	ppre->next=p_new;
	icount++;
}


int main()
{//使用的时候,想要结束输入number=0即可,如zhangsan    0则退出循环
struct Student* phead;
phead=Creat();//创建新的节点
//phead=Insert(phead);//从头部开始插入
//Delete(phead,2);//删除第二个节点
yd_charu(phead,2);//插入第二个节点
Print(phead);//输出链表
return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyd__

你的打赏和鼓励我会珍惜!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值