今天又忘了链表简单的几个操作怎么写了,awsl,awsl。
完整的链表操作代码:
库文件:
#include<stdio.h>
#include<stdlib.h>
结构体和全局变量:
struct Student
{
char cName[20];
int iNumber;
struct Student* pNext;
};
int iCount;
输入函数:
struct Student* Create()
{
struct Student* pHead=NULL;
struct Student* pEnd,*pNew;
iCount=0;
pEnd=pNew=(struct Student*)malloc(sizeof(struct Student));
printf("please first enter Name,then Number\n");
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
while(pNew->iNumber!=0)
{
iCount++;
if(iCount==1)
{
pNew->pNext=NULL;//原来的是=phead
pHead=pNew;
pEnd=pNew;
}
else
{
pNew->pNext=NULL;
pEnd->pNext=pNew;
pEnd=pNew;
}
pNew=(struct Student*)malloc(sizeof(struct Student));
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
}
free(pNew);
return pHead;
}
插入函数:
struct Student* Insert(struct Student* pHead)
{
struct Student* pNew;
printf("---Insert member at first---\n");
pNew=(struct Student*)malloc(sizeof(struct Student));
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
pNew->pNext=pHead;
pHead=pNew;
iCount++;
return pHead;
}
delete函数:
void Delete(struct Student* pHead,int ilndex)
{
int i;
struct Student* pTemp;
struct Student* pPre;
pTemp=pHead;
pPre=pTemp;
printf("---delete NO%d member ---\n",ilndex);
for(i=1;i<ilndex;i++)
{
pPre=pTemp;
pTemp=pTemp->pNext;
}
pPre->pNext=pTemp->pNext;
free(pTemp);
iCount--;
}
输出函数:
void Print(struct Student* pHead)
{
struct Student *pTemp;
int ilndex=1;
printf("---the list has %d members:---\n",iCount);
printf("\n");
pTemp=pHead;
while(pTemp!=NULL)
{
printf("the NO%d member is:\n",ilndex);
printf("the name is %s\n",pTemp->cName);
printf("the number is %d\n",pTemp->iNumber);
printf("\n");
pTemp=pTemp->pNext;
ilndex++;
}
}
main主体:
int main()
{
struct Student* pHead;
pHead=Create();
pHead=Insert(pHead);
Delete(pHead,2);
Print(pHead);
return 0;
}
运行结果:
我打代码像cxk,jinitaimei!