数据结构-->c构建线性表

来源:互联网 发布:诺基亚e66软件 编辑:程序博客网 时间:2024/06/05 02:17
#include <stdio.h>#include <stdlib.h>#include <windows.h>/*    --------------------------    VER       ||      1.1    DATA      ||      3/11/2017    AUTHER    ||      WUD    --------------------------*/static int MAXSIZELINE;typedef int Status;typedef int Elemtype;typedef struct{    Elemtype *elem;    int len;    int MAXSIZE;} Line;ShowProgram(){    //HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    //SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_WHITE);    int j=0;    for(j; j<27; j++)    {        printf("-");        Sleep(3);    }    printf("\n");    printf("VER           ||      1.1\n");    printf("FINIDATA      ||      3/11/2017\n");    printf("AUTHER        ||      WUD\n");    for(j=0; j<27; j++)    {        printf("-");        Sleep(3);    }    printf("\n");    Sleep(200);}Status InitLine(Line *L){    //int MAXSIZELINE;    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);    printf("Hello, Please input The Size of Line:     ");    scanf("%d",&MAXSIZELINE);    (*L).elem = (Elemtype *)malloc(MAXSIZELINE*sizeof(Elemtype));    if ((*L).elem==0)    {        printf("InitLine ERROR, Check your original code about malloc  :( n");        exit(0);    }    else if ((*L).elem != 0)    {        Sleep(100);        printf("InitLine Successfully :)\n");        Sleep(30);        printf("The space of Line is %d :)\n\n", MAXSIZELINE);        (*L).len = 0 ;        (*L).MAXSIZE = MAXSIZELINE ;    }    return 0 ;}void InsertLine(Line *L){    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_BLUE);    int temp;    for((*L).len = 0; (*L).len < (*L).MAXSIZE; (*L).len++)    {        printf("Please input the %d number into the Line : ", (*L).len);        scanf("%d", &temp);        (*L).elem[(*L).len] = temp;        //(*L).len+=1;        if ((*L).len == (*L).MAXSIZE)        {            printf("InsertLine Successfully :)\n");            printf("Check your Line.MAXSIZELINE?=MAXSIZE, Maybe You Need To Create Some New Space :)\n");        }    }}void DeleteData(Line *L){    int location;    printf("Please Input The Location That You Want To Delete: ");    scanf("%d", &location);    (*L).elem[location] = 0;    //(*L).len=(*L).len-1;    printf("DELETE SUCCESFULLY :)\n");    //printf("Now the size of line is %d\n\n\n",(*L).len);}void InsertData(Line *L){    int location;    int data,stmp,space,j;    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);    Sleep(30);    printf("Please Input The Location That You want to Insert: ");    scanf("%d", &location);    Sleep(30);    printf("The Location Is %d\n", location);    Sleep(30);    printf("Please Input The DATA You Want To Insert: ");    scanf("%d", &data);    printf("The Date is %d\n", data);    Sleep(30);    printf("Now You Need To Increase Some Space So Save Your Date, Please Input INCREAMENT SPACE:");    Sleep(30);    scanf("%d", &space);    (*L).elem = (Elemtype*)realloc((*L).elem,((*L).len+space)*sizeof(Elemtype));    Sleep(30);    printf("New Space is %d\n", MAXSIZELINE+space);    (*L).len = (*L).len+space;    Sleep(30);    printf("Save Data!\n");    for (j = 0; j<80; j++)    {        printf("=");        Sleep(1);    }    printf("\n");    for(location=location; location<=(*L).MAXSIZE+1; location++)    {        stmp = (*L).elem[location];        (*L).elem[location] = data;        data = stmp;    }    (*L).elem[(*L).len+1] = (*L).elem[(*L).len];    for (j = 0; j<80; j++)    {        printf(">");        Sleep(1);    }    printf("\n");    for (j=0; j<(*L).len-1; j++)    {        printf("%d--->", (*L).elem[j]);        Sleep(30);        if(j == (*L).len-2)        {            printf("%d\n",(*L).elem[j+1]);        }    }//if (j==(*L).len-1){printf("%d\n", L.elem[j]);}    for (j = 0; j<80; j++)    {        printf(">");        Sleep(1);    }    printf("\n");}Status FreeLine(Line *L){    free((*L).elem);    (*L).elem=NULL;    (*L).len=0;    (*L).MAXSIZE=0;    printf("FreeLine SUCCESSFULLY :)\n");    Sleep(30);    printf("Now the Line Size is %d\n\n", (*L).elem);    return 1;}Status DisplayLine(Line L){    int flag = 0;    int i, j;    for (i=0; i<L.len; i++)    {        printf("The %d number in this Line is %d \n", i, L.elem[i]);    }    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);    printf("\nDescribe Line \n");    for (j = 0; j<80; j++)    {        printf(">");        Sleep(1);    }    printf("\n");    for (j = 0; j<L.len-1; j++)    {        printf("%d--->", L.elem[j]);        Sleep(30);        //printf("%d,%d",j,L.len);        if (j==L.len-2)printf("%d\n", L.elem[j+1]);        flag = 1;    }    for (j = 0; j<80; j++)    {        printf(">");        Sleep(1);    }    printf("\n");    return flag;}int main(){    int point;    ShowProgram();    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);    printf("Press 1 to continue else exit :)");    while (scanf("%d",&point)!=EOF)    {        if (point!=1)        {            exit(0);        }        else        {            Line L;            InitLine(&L);            InsertLine(&L);            Sleep(1000);            int flag = DisplayLine(L);            DeleteData(&L);            DisplayLine(L);            InsertData(&L);            if (flag == 1)printf("\nProgram Run Successfully!\n");            else printf("ERROR PROGRAM! check your program!\n");            Sleep(30);            FreeLine(&L);            printf("Press 1 To continue else Exit!");        }    }    return 0 ;    //system(pause);}

这里写图片描述
Markdown编辑器还是有bug的:)