C语言链表基本操作(1)

#include <stdio.h>
#include <stdlib.h>

typedef struct myString {
    char ch;
    struct myString *next;
}LinkedList;

void Traverse(LinkedList* list) {
    if (list != NULL) {
        list = list->next; //头结点没存数据
    }
    while (list != NULL) {
        printf("\nTraversing,current ch is: %c\n", list->ch);
        list = list->next;
    }
}

LinkedList* create(int n) {
    if (n >= 0) {
        LinkedList *head, *node, *rear;
        head = (LinkedList*)malloc(sizeof(LinkedList));
        rear = head;
        for (int i = 0; i < n; i++) {
            node = (LinkedList*)malloc(sizeof(LinkedList));
            puts("请输入一个字符:");
            scanf(" %c", &node->ch);
            rear->next = node;
            rear = node;
        }

        rear->next = NULL;
        puts("链表创建成功\n");
        return head;
    }
    else {
        puts("创建失败!\n");
    }
    
}

void change(LinkedList *list, int n) { //n代表位置
    if (n >= 0) {
        LinkedList *t = list;
        int i = 0;
        while (i < n && t != NULL) {
            t = t->next;
            i++;
        }

        if (t != NULL) {
            puts("请输入一个字符\n");
            scanf(" %c", &t->ch);
        }
        else {
            printf("%d号节点不存在!\n", n);
        }
    }
    else {
        printf("%d号节点不存在!\n", n);
    }
    
}

void delete(LinkedList *list, int n) {
    if (n >= 0) {
        LinkedList* p = list;
        int i = 0;
        while (p != NULL && i < n) {
            p = p->next;
            i++;
        }

        if (p == NULL) {
            printf("%d号待删除节点不存在!\n", n);
        }
        else if (n != 0) {
            //这里O(n),如果用双向链表就会好很多
            //其实也可不用  参见法2
            LinkedList* q = list;
            {
                while (q->next != p) { q = q->next; }
                q->next = p->next;
                free(p);
                printf("remove OK!Now traverse\n");

                Traverse(list);
                /*    list = list->next;
                    while (list->next != NULL) {
                        printf("%c\n", list->ch);
                        list = list->next;
                    }
                    printf("%c\n", list->ch);*/
            }
        }
        else {
            printf("待删除节点不存在!\n");
        }
    }
    else {
        printf("待删除节点不存在!\n");
    }
    
}

void delete2(LinkedList* list, int n) {
    if (n >= 0) {
        //这个方法更巧妙,不用重头开始遍历
        LinkedList *p = list, *q = NULL;
        int i = 0;
        while (i < n && p != NULL) {
            q = p;
            p = p->next;
            i++;
        }

        if (p != NULL && q != NULL) {
            q->next = p->next;
            free(p);
            puts("法2删除成功!\n");
            printf("remove OK!Now traverse\n");

            Traverse(list);
        }
        else {
            puts("法2删除失败!不存在该节点!\n");
        }
    }
    else {
        puts("法2删除失败!不存在该节点!\n");
    }
    
}

void insertPrev(LinkedList* list, int n) {
    if (n >= 0 && ((int)n == n)) {
        LinkedList* p = list, *q = NULL;
        int i = 0;

        while (i < n && p != NULL) {
            q = p;
            p = p->next;
            i++;
        }

        if (p != NULL && q != NULL) {
            LinkedList* new = (LinkedList*)malloc(sizeof(LinkedList));
            puts("请输入插入的一个字符:\n");
            scanf(" %c", &new->ch);
            q->next = new;
            new->next = p;
            printf("前插入%c成功!", new->ch);
        }/*
        else if (q == NULL) {
            LinkedList* new = (LinkedList*)malloc(sizeof(LinkedList));
            puts("请输入插入的一个字符:\n");
            scanf_s(" %c", &new->ch);
            new->next = p;
            *pp = new;

        }*/
        else {
            printf("待插入位置不存在!请重新输入!\n");
        }
    }
    else {
        printf("待插入位置不存在!请重新输入!\n");
    }
    
}

void insertNext(LinkedList* list, int n) {
    if (n >= 0) {
        LinkedList *p = list, *q;
        int i = 0;
        while (i < n  && p != NULL) {
            p = p->next;
            i++;
        }
        if (p != NULL) {
            q = (LinkedList*)malloc(sizeof(LinkedList));
            puts("请输出要插入的值:\n");
            scanf(" %c", &q->ch);
            q->next = p->next;
            p->next = q;
            printf("后插%c成功!\n", q->ch);
        }
        else {
            printf("节点不存在!请重新插入!\n");
        }
    }
    else {
        printf("节点不存在!请重新插入!\n");
    }
    
}


int main(int argc, char* argv[]) {
    LinkedList* p = (LinkedList*)malloc(sizeof(LinkedList));
    //LinkedList **pp = &p;
    printf("--------------------Welcome to LinkedList World---------------------\n");
    //printf("Now u can choose below selections.\n");
    char* select[7] = { "0 create\n","1 insertPrev\n","2 insertNext\n","3 change\n","4 delete\n","5 delete2\n","6 traverse\n" };
    /*
    for(int i=0;i<7;i++){
        printf("%s\n",select[i]);
    }
    */

    LinkedList* list = NULL;
    while (1) {
        fflush(stdin);
        int n;
        printf("\nNow u can choose below selections.\n");
        for (int i = 0; i < 7; i++) {
            printf("%s\n", select[i]);
        }

        scanf(" %d", &n);
        
        if ((int)n == n) {
            switch (n) {
            case 0: {
                int nums;
                puts("Input numbers of nodes.\n");
                scanf(" %d", &nums);
                list = create(nums);
                break;
            }
            case 1: {
                puts("Input PrevInsertion position please.\n");
                int pos;
                scanf(" %d", &pos);
                insertPrev(list, pos);
                break;
            }
            case 2: {
                puts("Input NextInsertion position please.\n");
                int pos;
                scanf(" %d", &pos);
                insertNext(list, pos);
                break;
            }
            case 3: {
                puts("Input index for your change position.\n");
                int pos;
                scanf(" %d", &pos);
                change(list, pos);
                break;
            }
            case 4: {
                puts("Input index for your delete position.\n");
                int pos;
                scanf(" %d", &pos);
                delete(list, pos);
                break;
            }
            case 5: {
                puts("Input index for your delete2 position.\n");
                int pos;
                scanf(" %d", &pos);
                delete2(list, pos);
                break;
            }
            case 6: {
                Traverse(list);
                break;
            }
            default: {
                break; //只能跳出switch 跳不出while(1)
            }
            }
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/qq_18287147/article/details/105891327
今日推荐