struct Students
{
char cName[20];
int iNumber;
struct Student* pNext;
};
int iCount;
//链表的创建
struct Students* create() //struct Students* 函数返回值的类型
{
struct Students* pHead=NULL; //头结点
struct Students* pEnd,*pNew; //尾结点和创建的新结点
iCount=0; //结点的个数
pEnd=pNew=(struct Students*)malloc(sizeof(struct Students));
printf("please first enter name,then enter number\n");
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
while(pNew->iNumber!=0) //数据不为空
{
iCount++;
if(iCount==1) //结点为1表示是新创建的
{
pHead=pNew; //头结点指向 新开辟的空间
pEnd = pNew;
}
else //已经存在结点 将新的结点插入的链表的后面
{
pEnd->pNext=pNew; //将新结点 插入之前的后面
pEnd=pNew; //将尾指针移动到新结点上
}
pNew->pNext=NULL; //或者pEnd->pNext=NULL;
pNew=(struct Students*)malloc(sizeof(struct Students)) //可通过预编译 #define TYPE struct stu
// #define LEN sizeof (struct stu)
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
}
free(pNew);
return pHead;
}
//链表的插入操作
struct Students* Insert(struct Students* pHead)
{
struct Students* pNew;
printf("---Insert member at first---\n");
pNew=(struct Students*)malloc(sizeof(struct Students));
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
pNew-pNext=pHead;
pHead=pNew;
iCount++;
return pHead;
}
//删除链表结点
vod Delete(struct Students* pHead,int iIndex)
{
int i;
struct Students* pTemp;
struct Students* pPre; //删除结点的前结点
pTemp=pHead;
pPre=pTemp;
printf("---delete NO%d member---\n",iIndex);
for(i=1;i<iIndex;i++)
{
pPre=pTemp;
pTemp=pTemp->pNext;
}
pPre->pNext=pTemp->pNext;
free(pTemp);
iCount--;
}
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++;
}
}
{
char cName[20];
int iNumber;
struct Student* pNext;
};
int iCount;
//链表的创建
struct Students* create() //struct Students* 函数返回值的类型
{
struct Students* pHead=NULL; //头结点
struct Students* pEnd,*pNew; //尾结点和创建的新结点
iCount=0; //结点的个数
pEnd=pNew=(struct Students*)malloc(sizeof(struct Students));
printf("please first enter name,then enter number\n");
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
while(pNew->iNumber!=0) //数据不为空
{
iCount++;
if(iCount==1) //结点为1表示是新创建的
{
pHead=pNew; //头结点指向 新开辟的空间
pEnd = pNew;
}
else //已经存在结点 将新的结点插入的链表的后面
{
pEnd->pNext=pNew; //将新结点 插入之前的后面
pEnd=pNew; //将尾指针移动到新结点上
}
pNew->pNext=NULL; //或者pEnd->pNext=NULL;
pNew=(struct Students*)malloc(sizeof(struct Students)) //可通过预编译 #define TYPE struct stu
// #define LEN sizeof (struct stu)
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
}
free(pNew);
return pHead;
}
//链表的插入操作
struct Students* Insert(struct Students* pHead)
{
struct Students* pNew;
printf("---Insert member at first---\n");
pNew=(struct Students*)malloc(sizeof(struct Students));
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
pNew-pNext=pHead;
pHead=pNew;
iCount++;
return pHead;
}
//删除链表结点
vod Delete(struct Students* pHead,int iIndex)
{
int i;
struct Students* pTemp;
struct Students* pPre; //删除结点的前结点
pTemp=pHead;
pPre=pTemp;
printf("---delete NO%d member---\n",iIndex);
for(i=1;i<iIndex;i++)
{
pPre=pTemp;
pTemp=pTemp->pNext;
}
pPre->pNext=pTemp->pNext;
free(pTemp);
iCount--;
}
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++;
}
}