C链表操作

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值