单链表的增删改查操作

mark一下自己的学习过程,继续坚持✊✊✊

//
//  linklist.cpp
//  Love
//
//  Created by wpln on 2018/11/8.
//

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;

typedef struct student{
    int num;
    float score;
    struct student * pnext;
}stu, *pstu;

#define IN  //IN        表示输入参数,指针指向的值不会修改
#define OUT //OUT    表示输出参数,指针指向的值会修改,且不会读
#define INOUT //INOUT  表示输入输出参数,指针指向的值会修改,且会读取

void list_head_insert(IN OUT pstu *pphead, IN OUT pstu *pptail, IN int i){
    pstu pnew = (pstu)malloc(sizeof(stu));
    memset(pnew, 0, sizeof(stu));
    pnew->num = i;
    if(NULL == *pphead){
        *pphead = pnew;
        *pptail = pnew;
    }else{
        pnew->pnext = *pphead;
        *pphead = pnew;
    }
}
void list_tail_insert(IN OUT pstu* pphead, IN OUT pstu *pptail, IN int i){
    pstu pnew = (pstu)malloc(sizeof(stu));
    memset(pnew, 0, sizeof(stu));
    pnew->num = i;
    if(NULL == *pphead){
        *pphead = pnew;
        *pptail = pnew;
    }else{
        (*pptail)->pnext = pnew;
        *pptail = pnew;
    }
}

//按从小到大的顺序
void list_sort_insert(IN OUT pstu *pphead, IN OUT pstu *pptail, IN OUT int i){
    pstu pcur, ppre;
    pstu pnew = (pstu)malloc(sizeof(stu));
    memset(pnew, 0, sizeof(stu));
    pnew->num = i;
    pcur = *pphead;
    ppre = *pphead;
    if(NULL == *pphead){
        *pphead = pnew;
        *pptail = pnew;
    }else if((*pphead)->num > i){ //在开头位置插入
        pnew->pnext = *pphead;
        *pphead = pnew;
    }else{
        while (pcur != NULL) {
            if(pcur->num > i){
                ppre->pnext = pnew;
                pnew->pnext = pcur;
                break;
            }
            ppre = pcur;
            pcur = pcur->pnext;
        }
        //到达尾部
        if(NULL == pcur){
            (*pptail)->pnext = pnew;
            *pptail = pnew;
        }
    }
}

void list_delete(IN OUT pstu *pphead, IN OUT pstu *pptail, IN int i){
    pstu ppre, pcur;
    ppre = *pphead;
    pcur = *pphead;
    if(NULL == *pphead)
        return;
    else if((*pphead)->num == i){ //删除第一个节点
        *pphead = pcur->pnext;
        if(NULL == *pphead)
            *pptail = NULL;
    }
    else{
        while (pcur != NULL) {
            if(pcur->num == i){
                ppre->pnext = pcur->pnext;
                break;
            }
            ppre = pcur;
            pcur = pcur->pnext;
        }
        //将最后一个节点删除了
        if(pcur == *pptail){
            *pptail = ppre;
        }
    }
    //没有找到
    if(pcur == NULL){
        cout << "value " << i << " don't in this list" << endl;
    }else{
        free(pcur);
        pcur = NULL;
    }
}

void list_modify(pstu phead, int i, float f){
    while(phead != NULL){
        if(phead->num == i){
            phead->score = f;
            break;
        }
        phead = phead->pnext;
    }
}

void list_print(pstu phead){
    while(phead != NULL){
        cout << "Num " << phead->num << "' score is " << phead->score << endl;
        phead = phead->pnext;
    }
}

int main()
{
    pstu phead = NULL, ptail = NULL;
    int i;
    float f;
    while(scanf("%d", &i) != EOF){
        list_sort_insert(&phead, &ptail, i);
    }
    list_print(phead);
    
    while ((printf("please input modify num and score:\n"), scanf("%d,%f", &i, &f)) != EOF) {
        list_modify(phead, i, f);
        list_print(phead);
    }
    
    while(printf(("please input delete num:\n"),scanf("%d",&i))!=EOF)
    {
        list_delete(&phead,&ptail,i);
        list_print(phead);
    }
    
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值