#include <stdio.h>
#include <stdlib.h>
//定义一个结构体
struct Student{
char cName[20];
int iAge;
struct Student*pNext;
};
//记录个数
int iCount = 0;
//创建链表
struct Student*Create(){
struct Student* pHead, *pNew, *pEnd;
pHead = pNew = pEnd = (struct Student*)malloc(sizeof(struct Student));
scanf("%s", &pNew->cName);
scanf("%d", &pNew->iAge);
while (pNew->iAge != 0){
iCount++;
if (iCount == 1){
pNew->pNext = NULL;
pHead = pEnd = pNew;
}
else{
pEnd->pNext = pNew;
pNew->pNext = NULL;
pEnd = pNew;
}
pNew = (struct Student*)malloc(sizeof(struct Student));
scanf("%s", &pNew->cName);
scanf("%d", &pNew->iAge);
}
free(pNew);
return pHead;
}
//从头部插入成员
struct Student*Insert(struct Student*pInsert){
struct Student* pHead, *pNew;
pHead = pNew = (struct Student*)malloc(sizeof(struct Student));
scanf("%s", &pNew->cName);
scanf("%d", &pNew->iAge);
pNew->pNext = pInsert;
return pNew;
}
//从某个位置插入成员
struct Student*InsertForIndex(struct Student*pHead, int iIndex){
int iNum = 0;
if (iCount < 1){
return NULL;
}
struct Student*pTemp, *pCurrent, *pNew;
pTemp = pHead;
//找出具体位置
while (pTemp != NULL){
iNum++;
if (iNum == iIndex){
pCurrent = pTemp;
break;
}
pTemp = pTemp->pNext;
}
pNew = (struct Stuent*)malloc(sizeof(struct Student));
scanf("%s", &pNew->cName);
scanf("%d", &pNew->iAge);
pNew->pNext = pCurrent->pNext;
pCurrent->pNext = pNew;
iCount++;
return pHead;
}
//从某个位置更新成员
struct Student*UpdateForIndex(struct Student*pHead, int iIndex){
int iNum = 0;
struct Student* pTemp, *pCurrent, *pNew;
if (iCount < 1){
return NULL;
}
pTemp = pHead;
while (pTemp != NULL)
{
iNum++;
if (iNum == iIndex){
pCurrent = pTemp;
break;
}
pTemp = pTemp->pNext;
}
scanf("%s", &pCurrent->cName);
scanf("%d", &pCurrent->iAge);
return pHead;
}
//从某个位置删除成员
struct Student*DeleteForIndex(struct Student*pHead, int iIndex){
struct Student* pTemp, *pCurrent, *pForword;
if (iCount < 1){
return NULL;
}
int iNum = 0;
pTemp = pHead;
while (pTemp != NULL){
iNum++;
if (iNum == iIndex - 1){
pForword = pTemp;
break;
}
pTemp = pTemp->pNext;
}
pCurrent = pForword->pNext;
pForword->pNext = pCurrent->pNext;
free(pCurrent);
iCount--;
return pHead;
}
//打印成员
void Print(struct Student* pHead){
struct Student*pTemp;
pTemp = pHead;
while (pTemp != NULL){
printf("%s-%d\n", pTemp->cName, pTemp->iAge);
pTemp = pTemp->pNext;
}
}
int main(){
struct Student* pHead = Create();
//struct Student* pInsert = Insert(pHead);
//struct Student* pInsertCount = InsertForIndex(pHead, 2);
//struct Student* pUpdateCount = UpdateForIndex(pHead, 2);
struct Student* pDeleteCount = DeleteForIndex(pHead, 2);
Print(pDeleteCount);
printf("%d", iCount);
return 0;
}