博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
链表crud
阅读量:7175 次
发布时间:2019-06-29

本文共 4653 字,大约阅读时间需要 15 分钟。

hot3.png

 

#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

你可能感兴趣的文章
修改Eclipse/MyEclipse项目的默认编码
查看>>
数据库中如何使用SQL查询连续号码段(转载)
查看>>
BPP
查看>>
Eclipse和PyDev搭建python开发环境
查看>>
IronPython脚本调用C#dll示例
查看>>
LuaInterface简介
查看>>
FreeBSD暂时用9.X系列为宜
查看>>
Item 33: 避免覆盖(hiding)“通过继承得到的名字”
查看>>
前端工程优化:javascript的优化小结
查看>>
mat之一--eclipse安装Memory Analyzer
查看>>
【编程题目】求子数组的最大和 ☆
查看>>
Unity3d Web Player 的server端联网配置
查看>>
Linux系统下如何配置SSH?如何开启SSH?
查看>>
Quartus II 12.0 下载、安装和破解
查看>>
几种任务调度的 Java 实现方法与比较
查看>>
《我与葡萄城的故事》— 征文大赛
查看>>
VB6 GDI+ 入门教程[3] 笔、刷子、矩形、椭圆绘制
查看>>
Python 访问set
查看>>
oracle Constraint[相似 constraint使用方法总结 I]
查看>>
huffman编码——原理与实现
查看>>