用c语言的链表实现信息系统的增删录入操作(不完善版,求大佬提建议)

本文介绍了如何使用C语言实现一个简单的链表结构,包括创建链表、在指定位置插入节点以及删除节点的方法,并提供相应的函数和示例代码.
摘要由CSDN通过智能技术生成

#include<stdio.h>
#include<stdlib.h>
struct Student                                       
{
    char cName[20]; 
    int age;
    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("fitst enter Name, then Age:\n输入0结束录入\n"); 
    scanf("%s",&pNew->cName);
    scanf("%d",&pNew->age);
    while(pNew->age!=0)
    {
        iCount++;
        if(iCount==1)
        {
            pNew->pnext=pHead;
            pHead=pEnd=pNew; 
         } 
        else
        {
            pNew->pnext=NULL;
            pEnd->pnext=pNew;
            pEnd=pNew;
        }
        pNew=(struct Student *)malloc(sizeof(struct Student));
        scanf("%s",&pNew->cName);
        scanf("%d",&pNew->age);
    } 
    free(pNew);
    return pHead;
}
void student_print(struct Student *pHead)
{
    struct Student *pTemp;
    int Index=1;
    printf("---the list have %d members---\n",iCount);
    printf("\n");
    pTemp=pHead;
    while(pTemp!=NULL)
    {
        printf("the %d个 member is:\n",Index);
        printf("%s\n",pTemp->cName);
        printf("%d\n",pTemp->age);
        Index++;
        pTemp=pTemp->pnext;
    }
}
struct Student *Insert(struct Student *pHead,int Index)
{
    struct Student *pPre;
    struct Student *pNew;
    struct Student *pTemp;
    pNew=(struct Student *)malloc(sizeof(struct Student));
    printf("请输入name and age\n");
    scanf("%s",&pNew->cName);
    scanf("%d",&pNew->age);
    pTemp=pHead;
    for(int i=1;i<Index;i++)
    {
        pPre=pTemp;
        pTemp=pTemp->pnext;
    }
    pNew->pnext=pTemp;
    pPre->pnext=pNew;
    return pHead;
    iCount++; 
}
struct Student *Delete(struct Student *pHead,int Index)
{
    struct Student *pPre;
    struct Student *pTemp;
    pTemp=pHead;
    for(int i=1;i<Index;i++)
    {
        pPre=pTemp;
        pTemp=pTemp->pnext;
    }
    pPre->pnext=pTemp->pnext; 
    return pHead;
}
int main()
{
    struct Student *pHead;
    pHead=Create();
    while(1)
    {
        int iCount;
        printf("请输入接下来要执行的操作:输入1为添加节点,输入0为删除节点,输入其它数字为结束\n");
        scanf("%d",&iCount); 
        if(iCount==1)
        {
            printf("请输入想要添加节点的位置\n");
            int i;
            scanf("%d",&i);
            Insert(pHead,i);
            continue;
        }
        else if(iCount==0)
        {
            printf("请输入想要删除节点的位置\n");
            int j;
            scanf("%d",&j);
            Delete(pHead,j);
            continue;
        }
        else
        {
            break;
        }
    }
    student_print(pHead);
    return 0;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值