回 帖 发 新 帖 刷新版面

主题:数据结构--顺序表(望大家来评论看看怎么改进更好! 谢谢!)

[b][size=4][color=FF00FF]我的blog,[url=http://www.9ite.cn]http://www.9ite.cn[/url]欢迎大家来踩[/color][/size][/b]


/*线性表子程序*/
#include "stdio.h"
#include "malloc.h"
#include "conio.h"
#include "stdlib.h"
#define MAX 80
typedef struct
{
    int data[MAX];
    int last;
}Seqlist;

Seqlist *Init_Seqlist()
{
    Seqlist *L;
    L=new Seqlist;
    L->last=-1;
    return L;
}

int Insert_Seqlist(Seqlist *L,int i,int x)
{
    int j;
    if(L->last==MAX-1)
    {printf("表满,不能插入!");
    return -1;}
    if(i<1||i>L->last+2)
    {printf("插入位置错!");
    return 0;}
    for(j=L->last;j>=i-1;j--)
        L->data[j+1]=L->data[j];
    L->data[i-1]=x;
    L->last++;
    return 1;
}

int Del_Seqlist(Seqlist *L,int i)
{
    int j;
    if(i<1||i>L->last+1)
    {printf("位置错!");
        return 0;}
    for(j=i;j<=L->last;j++)
      L->data[j-1]=L->data[j];
    L->last--;
    return 1;
}
int Location_Seqlist(Seqlist *L,int x)
{
    int i=0;
    while(i<=L->last&&L->data[i]!=x)
        i++;
    if(i<=L->last)
        return i;
    else
        return -1;
}
void Print_Seqlist(Seqlist *L)
{
    int i;
    for(i=0;i<=L->last;i++)
        printf("%d ",L->data[i]);
}
void main()
{
    Seqlist *L;
    int i,x,flag,k;
    do{
        printf("\n\n\n");
        printf("\t\t\t      顺序表子系统\n");
        printf("\t\t\t************************\n");
        printf("\t\t\t******1--初始化表*******\n");
        printf("\t\t\t******2--插    入*******\n");
        printf("\t\t\t******3--删    除*******\n");
        printf("\t\t\t******4--查    找*******\n");
        printf("\t\t\t******5--显    示*******\n");
        printf("\t\t\t******0--返    回*******\n");
        printf("\t\t\t************************\n");
        printf("\t\t\t  请选择菜单(0-5):");
        scanf("%d",&k);
        switch(k)
        {
        case 1:
            L=Init_Seqlist();
            printf("顺序表已初始化!按任意键返回...");
            getch();
            system("cls");
            break;
        case 2:
            printf("\n请输入插入的位置i和元素x(i,x):");
            scanf("%d,%d",&i,&x);
            flag=Insert_Seqlist(L,i,x);
            if(flag==1)
            printf("\n插入成功!按任意键返回...");
            else
                printf("\n按任意键返回...");
            getch();
            system("cls");
                                break;
        case 3:
            printf("\n请输入删除位置i:");
            scanf("%d",&i);
            flag=Del_Seqlist(L,i);
            if(flag==1)
            printf("\n删除成功!按任意键返回...");
                                getch();
            system("cls");
            break;
        case 4:
            printf("\n请输入要查找的值:");
            scanf("%d",&x);
            flag=Location_Seqlist(L,x);
            if(flag!=-1)
    printf("\n值为%d在顺序表中的位置为%d!按任意键返回...",x,flag);
            else
            printf("\n顺序表中无此元素!按任意键返回...");
            getch();
            system("cls");
            break;
        case 5:
            printf("\n顺序表的元素如下:\n");
            Print_Seqlist(L);
            printf("\n按任意键返回...");
            getch();
            system("cls");
            break;
        }
    }while(k!=0);
}




回复列表 (共12个回复)

11 楼

谢谢指点

12 楼


Seqlist *Init_Seqlist()
{
    Seqlist *L;
    L=(Seqlist*)malloc(MAX);
    L->last=-1;
    return L;
}
改成这样可用,但运行结果不对

我来回复

您尚未登录,请登录后再回复。点此登录或注册