链表的基本操作(基于C)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include

#include

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=pHead;

pEnd=pNew;

pHead=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;

}

void Print(struct Student* pHead)

{

struct Student *pTemp;

int iIndex=1;

printf("----the List has %d members:----\n",iCount);

printf("\n");

pTemp=pHead;

while(pTemp!=NULL)

{

printf("the NO%d member is:\n",iIndex);

printf("the name is: %s\n",pTemp->cName);

printf("the number is: %d\n",pTemp->iNumber);

printf("\n");

pTemp=pTemp->pNext;

iIndex++;

}

}

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;

}

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

{

pPre=pTemp;

pTemp=pTemp->pNext;

}

pPre->pNext=pTemp->pNext;

free(pTemp);

iCount--;

}

int main()

{

struct Student* pHead;

pHead=Create();

pHead=Insert(pHead);

Delete(pHead,2);

Print(pHead);

return 0;

}

相关文档
最新文档