单链表的增删改查操作

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

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

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

 

你可能感兴趣的:(单链表的增删改查操作)