单链表的增删改查操作

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

 

单链表是一种常用的数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。单链表操作包括增加节点、删除节点、修改节点和找节点。下面是C语言实现单链表增删改查示例代码: ```c #include <stdio.h> #include <stdlib.h> // 节点结构体 typedef struct node { int data; // 数据元素 struct node* next; // 指向下一个节点的指针 } Node; // 在单链表末尾增加节点 void addNode(Node** head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if (*head == NULL) { *head = newNode; } else { Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } // 在单链表指定位置插入节点 void insertNode(Node** head, int index, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; if (index == 0) { newNode->next = *head; *head = newNode; } else { Node* current = *head; for (int i = 0; i < index - 1 && current != NULL; i++) { current = current->next; } if (current != NULL) { newNode->next = current->next; current->next = newNode; } else { printf("Index out of range\n"); } } } // 在单链表指定位置删除节点 void deleteNode(Node** head, int index) { if (*head == NULL) { printf("List is empty\n"); return; } Node* current = *head; Node* previous = NULL; if (index == 0) { *head = current->next; free(current); } else { for (int i = 0; i < index && current != NULL; i++) { previous = current; current = current->next; } if (current != NULL) { previous->next = current->next; free(current); } else { printf("Index out of range\n"); } } } // 修改单链表指定位置节点的值 void modifyNode(Node* head, int index, int data) { if (head == NULL) { printf("List is empty\n"); return; } Node* current = head; for (int i = 0; i < index && current != NULL; i++) { current = current->next; } if (current != NULL) { current->data = data; } else { printf("Index out of range\n"); } } // 单链表指定位置节点的值 int searchNode(Node* head, int index) { if (head == NULL) { printf("List is empty\n"); return -1; } Node* current = head; for (int i = 0; i < index && current != NULL; i++) { current = current->next; } if (current != NULL) { return current->data; } else { printf("Index out of range\n"); return -1; } } // 打印单链表 void printList(Node* head) { if (head == NULL) { printf("List is empty\n"); return; } Node* current = head; printf("List: "); while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { Node* head = NULL; // 在单链表末尾增加节点 addNode(&head, 1); addNode(&head, 2); addNode(&head, 3); printList(head); // 在单链表指定位置插入节点 insertNode(&head, 1, 4); insertNode(&head, 0, 5); insertNode(&head, 6, 6); printList(head); // 在单链表指定位置删除节点 deleteNode(&head, 2); deleteNode(&head, 0); deleteNode(&head, 4); printList(head); // 修改单链表指定位置节点的值 modifyNode(head, 1, 7); modifyNode(head, 3, 8); modifyNode(head, 5, 9); printList(head); // 单链表指定位置节点的值 printf("Value at index 2: %d\n", searchNode(head, 2)); printf("Value at index 5: %d\n", searchNode(head, 5)); printf("Value at index 7: %d\n", searchNode(head, 7)); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值