单链表头插法以及所有功能的实现(纯代码)

纯代码,想看懂只需要了解一下指针即可。

#include <iostream>
#include <stdio.h>
using namespace std;
typedef struct LNode {//抽象数据类型
    int data;
    struct LNode* next;
};

void Judge(bool judge) {
    if (judge == true)
        cout << "Opration Successful\n";
    else
        cout << "Opration failed\n";
}

bool AddNode(int num, LNode *H) {//在下标为num的结点后方添加新的结点。
    try {
        int x;  LNode *S = H;
        LNode* R = (LNode*)malloc(sizeof(LNode));
        for (int i = 1; i <= num; i++) {
            if (S == NULL) throw 1;
            S = S->next;
        }
        R->next = S->next;
        S->next = R;
        cout << "Please input the value\n";
        cin >> x;    R->data = x;
        return true;
    }
    catch (int e) {
        return false;
    }
}

bool SearchValueByOrderNumber(int num, LNode *H) {//找到下标为num的结点的Value。
    try {
        LNode *S = H;
        for (int i = 1; i <= num; i++) {
            S = S->next;
            if (S == NULL) throw 1;
        }
        cout << "The value you are searching for is "<< S->data << "\n";
        return true;
    }
    catch (int e) {
        return false;
    }
}

bool SearchOrderNumberByValue(int num, LNode *H) {//找到Value=num的第一个结点的下标。
    try {
        LNode* S = H;
        cout << "The order number you are looking for is ";
        for (int i = 1; i <= INT_MAX; i++) {
            S = S->next;
            if (S->data == num && S != NULL) {
                cout << i << "\n";
                break;
            }
            else if (S->next == NULL) throw 1;
        }
        return true;
    }
    catch (int e) {
        return false;
    }
}

bool DeleteNodeByOrderNumber(int num, LNode *H) {//删除下标为num的结点
    try {
        LNode* S = H;
        LNode* R;
        for (int i = 1; i < num; i++) {
            if (S->next == NULL)  throw 1;
            S = S->next;
        }
        R = S->next;
        S->next = R->next;
        free(R);
        return true;
    }
    catch (int e) {
        return false;
    }
}

bool DeleteNodeByValue(int num, LNode *H) {//删除Value为num的下标
    try {
        LNode* S = H;
        LNode* R;
        for (int i = 1; i <= INT_MAX; i++) {
            if (S->next == NULL) throw 1;
            if (S->next->data == num) {
                break;
            }
            S = S->next;
        }
        R = S->next;
        S->next = R->next;
        free(R);
        return true;
    }
    catch (int e) {
        return false;
    }
    
}
/*注:未初始化变量函数传参得引用。*/
bool InitializeLinkList(LNode* &H) { //头插法初始化单链表。
    try {
        LNode* S; int x;
        H = (LNode*)malloc(sizeof(LNode));//创建头节点.
        H->next = NULL;
        for (int i = 1; i <= INT_MAX; i++) { //头插法实现.
            S = (LNode*)malloc(sizeof(LNode));
            cin >> x;   if (x == -1)  break;
            S->data = x;
            S->next = H->next;
            H->next = S;
        }
        return true;
    }
    catch (exception e) {
        return false;
    }
    
}

bool ReadLinkList(LNode* H) {//打印单链表。
    try {
        LNode* S = H->next;
        for (int i = 1; i <= INT_MAX; i++) {
            if (S == NULL) break;
            cout << S->data << " ";
            S = S->next;
        }
        cout << "\n";
        return true;
    }
    catch(exception e){
        return false;
    }
}
int main() {
    LNode *Head; 
    cout << "Initialize the linklist, input -1 to stop\n";
    InitializeLinkList(Head);

    cout << "Input 1 to ReadLinkList\n"
            "Input 2 to DeleteNodeByOrderNumber\n"
            "Input 3 to DeleteNodeByValue\n"
            "Input 4 to AddNode\n"
            "Input 5 to SearchValueByOrderNumber\n"
            "Input 6 to SearchOrderNumberByValue\n"
            "Input anyothers to end\n";
    int choice,num;
    bool judge;
    while (1) {
        cin >> choice;
        switch (choice){
        case 1:
            judge = ReadLinkList(Head);
            Judge(judge);
            break;
        case 2:
            cout << "Please input the order number\n";
            cin >> num;
            judge = DeleteNodeByOrderNumber(num, Head);
            Judge(judge);
            break;
        case 3:
            cout << "Please input the node value\n";
            cin >> num;
            judge = DeleteNodeByValue(num, Head);
            Judge(judge);
            break;
        case 4:
            cout << "Please input the order number\n";
            cin >> num;
            judge = AddNode(num, Head);
            Judge(judge);
            break;
        case 5:
            cout << "Please input the order number of the node you are searching for\n";
            cin >> num;
            judge = SearchValueByOrderNumber(num, Head);
            Judge(judge);
            break;
        case 6:
            cout << "Please input the value of the node you are searching for\n";
            cin >> num;
            judge = SearchOrderNumberByValue(num, Head);
            Judge(judge);
            break;
        default:
            return 0;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值