链表crud

 

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

#define DATA_TYPE int
typedef struct node
{
    DATA_TYPE data;
    struct node* next;
}LinkList, *Pointer;

void menu();                     /*显示菜单*/
int readChoice();                   /*读菜单选项*/
void cleanBuffer();                 /*清除缓存*/
void addElement();                  /*添加元素*/
void showElement();                 /*显示所有元素*/
Pointer moveToTag(int tag);         /*根据tag移动偏移量*/
int removeByPostion();              /*根据位置删除元素*/
int getLinkListSize();              /*获得链表大小*/
void frontToBack(Pointer tail);     /*从前向后添加数据*/
void backToFront(Pointer tail);     /*从后向前插入元素*/
Pointer createLinkList();           /*从前向后创建链表*/
Pointer moveTail();                 /*移动到链表尾部*/
DATA_TYPE findElementByPostion();   /*根据位置查找元素*/

Pointer pointer = NULL;
int main(void)
{
    int choice = 0;
    DATA_TYPE data = -1;

    menu();
    while (1)
    {
        printf("\n");
        choice = readChoice();

        switch (choice)
        {
            case 1:
                addElement();
                printf("add success!!!");
                break;
            case 2:
                if (removeByPostion())
                {
                    printf("Remove success!!!");
                }
                else
                {
                    printf("Positon error!!");
                }
                break;
            case 3:
                data = findElementByPostion();
                if (data != -1)
                {
                    printf("%d", data);
                }
                else
                {
                    printf("haven't data!!");
                }
                break;
            case 4:
                showElement();
                break;
            case 5:
                exit(0);
                break;
            default: printf("Option does not match!!!");
                break;
        }
    }

    getch();
    return 0;
}

void menu()
{
    printf("\t\n#################################\n");
    printf("*\t 1.AddElement\t\t*\n");
    printf("*\t 2.RemoveByPostion\t*\n");
    printf("*\t 3.FindElementByPostion\t*\n");
    printf("*\t 4.ShowElement\t\t*\n");
    printf("*\t 5.Quit\t\t\t*");
    printf("\t\n#################################\n\n");
}

int readChoice()
{
    int choice = 5;
    printf("\nPlease input choice:");
    while (scanf("%d", &choice) != 1)
    {
        printf("Input error!!!\n");
        printf("Please input again:");
        cleanBuffer();
    }

    return choice;
}

void cleanBuffer()
{
    int c = 0;
    while ((c = getchar()) != '\n' && c != EOF );
}

void addElement()
{
    int o = 0;
    Pointer tail = NULL;

    if (pointer == NULL)
    {
        tail = pointer = createLinkList();
    }
    else
    {
        tail = moveTail();
    }
   
    printf("1.From the front to the back\t");
    printf("other.From the back to the front\n");
    printf("Please input add data method:");
    while (scanf("%d", &o) != 1)
    {
        printf("Input error!!!\n");
        printf("Please input again:");
        cleanBuffer();
    }

    printf("[*Notice : Please enter -1 to end]\n");
    if (o == 1)
    {
        frontToBack(tail);
    }
    else
    {
       backToFront(tail);
    }
}

Pointer createLinkList()
{
    pointer = (Pointer)malloc(sizeof(LinkList));
    pointer->next = NULL;
    pointer->data = 0;

    return pointer;
}


Pointer moveTail()
{
    Pointer current = pointer->next;
    while (current->next != NULL)
    {
        current = current->next;
    }

    return current;
}

int getLinkListSize()
{
    int count = 0;
    Pointer current = pointer;
    while (current->next != NULL)
    {
        current = current->next;
        count++;
    }

    return count;
}

void showElement()
{
    Pointer current = pointer->next;
    if (current == NULL)
    {
        printf("Linklist is empty!!!");
    }
    else
    {
        printf("Elements list:\n");
        while (current != NULL)
        {
            if (current->next == NULL)
            {
                printf("%d", current->data);
            }
            else
            {
                printf("%d-->", current->data);
            }
            current =  current->next;
        }
    }

    printf("\n");
}

Pointer moveToTag(int tag)
{
    int i = 0;
    Pointer current = pointer;
    while (i++ < tag)
    {
        current = current->next;
    }

    return  current;
}

int removeByPostion()
{
    int pos = 0;
    Pointer front = NULL;
    Pointer rear = NULL;
    Pointer current = NULL;

    printf("Please input position:");
    while (scanf("%d", &pos) != 1)
    {
        printf("Input error!!!\n");
        printf("Please input again:");
        cleanBuffer();
    }

    if (pos > getLinkListSize() || pos <= 0)
    {
        return 0;
    }

    front = moveToTag(pos - 1);
    current = moveToTag(pos);
    rear = moveToTag(pos+1);

    front->next = rear;
   
    free(current);

    return 1;
}

DATA_TYPE findElementByPostion()
{
    int pos = 0;
    Pointer current = NULL;
    printf("Please input position:");

    while (scanf("%d", &pos) != 1)
    {
        printf("Input error!!!\n");
        printf("Please input again:");
        cleanBuffer();
    }

    if (pos > getLinkListSize() || pos <= 0)
    {
        return -1;
    }

    current = moveToTag(pos);

    return current->data;
}


void frontToBack(Pointer tail)
{
    Pointer current = NULL;
    while (1)
    {
        current = (Pointer)malloc(sizeof(LinkList));
        printf("Please input element:");
        while (scanf("%d", &current->data) != 1)
        {
            printf("Input error!!!\n");
            printf("Please input again:");
            cleanBuffer();
        }

        if (current->data == -1)
        {
            break;
        }

        current->next = NULL;
        tail->next = current;
        tail = current;
    }
}

void backToFront(Pointer tail)
{
    Pointer current = NULL;
    while (1)
    {
        current = (Pointer)malloc(sizeof(LinkList));
        printf("Please input element:");
        while (scanf("%d", &current->data) != 1)
        {
            printf("Input error!!!\n");
            printf("Please input again:");
            cleanBuffer();
        }
        if (current->data == -1)
        {
            break;
        }
        current->next = tail->next;
        tail->next = current;
    }
}

转载于:https://my.oschina.net/997155658/blog/113050

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值