91学习

陪伴你每一天!

插入节点

#include <stdio.h>

#include <stdlib.h>



struct Clerk {

char cName[20];

int iNumber;

struct Clerk* pNext;

};

int iCount;


struct Clerk* creat(){

            struct Clerk* pHead;

struct Clerk* pEnd,*pNew;

iCount=0;

pEnd=pNew=(struct Clerk*)malloc(sizeof(struct Clerk));

printf("please input 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 Clerk*)malloc(sizeof(struct Clerk));

scanf("%s",&pNew->cName);

scanf("%d",&pNew->iNumber);

}

free(pNew);

return pHead;

}


void Print(struct Clerk* pHead){

struct Clerk* 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 Clerk * insert(struct Clerk* pHead){

struct Clerk* pNew;

printf("-----insert into member at first----\n");

pNew=(struct Clerk*)malloc(sizeof(struct Clerk));

scanf("%s",&pNew->cName);

scanf("%d",&pNew->iNumber);

pNew->pNext=pHead;

pHead=pNew;

iCount++;

return pHead;

}




int main(){

struct Clerk* pHead;

pHead=creat();

pHead=insert(pHead);

Print(pHead);

return 0;

}

加威信18790284560免费送POS机.jpg

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。