数据结构复习之顺序表

来源:互联网 发布:花桥远洋数据地址 编辑:程序博客网 时间:2024/05/17 00:58

#include "stdio.h"
#include "alloc.h"


#define MaxSize 3

typedef int DataType;

typedef struct
{
    DataType list[MaxSize];
    int size;
}SeqList;


void ListInitiate(SeqList *L);
int ListLength(SeqList *L);
int ListInsert(SeqList *L,int i,DataType x);
int ListDelete(SeqList *L,int i,DataType *x);

void main()
{
    int i,j,ch;
    int pois,Num;

    SeqList *mylist=0;
    for(i=0;i<MaxSize;i++)
    {
         printf("Please insert %d Num/n",i+1);
         scanf("%d",mylist->list[i]);
    }
    printf("The List Is:");
    for(j=0;i<MaxSize;j++)
    {
        printf("%d/n",mylist->list[i]);
    }

    printf("/n/n/n/t**************************/n");
    printf("/t*****  1.ListInser   *****/n");
    printf("/t*****  2.ListDelete  *****/n");
    printf("/t*****  3.ListInitiat *****/n");
    printf("/t*****  4.ListLength  *****/n");
    printf("/t*****  5.Exit        *****/n");
    printf("/t**************************/n");
    printf("Please choice...");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:{
                    printf("Please Insert the Num poistion and the Num");
                    scanf("%d%d",&pois,&Num);
                    if(ListInsert(mylist,pois,Num)==1)
                    {
                        printf("Sucess Insert the Num");
                    }
                    else
                    {
                        printf("Error!!");
                    }

               }; break;
        case 2:{
                    printf("Please Delete the Num poistion and the Num");
                    scanf("%d",&pois);
                    ListDelete(mylist,pois,&Num);
                    if(ListDelete(mylist,pois,&Num)==1)
                    {
                        printf("Sucess Delete the Num");
                    }
                    else
                    {
                        printf("Error!!");
                    }
               } ;break;
        case 3:ListInitiate(mylist);break;
        case 4:ListLength(mylist);break;
        case 5:close();break;
        default: printf("输入错误!");

    }
    getch();
}

void ListInitiate(SeqList *L)
{
    L->size=0;
}

int ListLength(SeqList *L)
{
    return L->size;
}

int ListInsert(SeqList *L,int i,DataType x)
{
    int j;
    if(L->size>=MaxSize)
    {
        printf("顺序表已满无法插入!/n");
        return 0;
    }
    else if(i<0||i>L->size)
    {
        printf("插入位置出错!");
        return 0;
    }
    else
    {
        for(j=L->size;j<i;j--)
        {
            L->list[j]=L->list[j-1];
        }
        L->list[i]=x;
        L->size++;
        return 0;
    }
}

int ListDelete(SeqList *L,int i,DataType *x)
{
    int j=0;
    if(L->size<=0)
    {
        printf("顺序表已空,没有元素可以删除!");
        return 0;
    }
    else if(i<0||i>L->size-1)
    {
        printf("i的位置不合法");
    }
    else
    {
        *x=L->list[i];
        for(j=j+1;j<=L->size-1;j++)
        {
            L->list[j-1]=L->list[j];
        }
        L->size--;
        return 0;
    }
}

原创粉丝点击