链表部分代码(插入,删除等等)
#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;
}