标题:线性表查找操作的问题
只看楼主
zqm0209
Rank: 1
等 级:新手上路
帖 子:142
专家分:0
注 册:2008-1-2
结帖率:100%
 问题点数:0 回复次数:1 
线性表查找操作的问题
这是vc环境下线性表基本操作的实验,但使用查找操作时,如果建立的线性表中有大于10的数,查找的结果就不正确,这是怎么回事,请指点
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct
{
    ElemType *elem;   //
    int length;
    int listsize;
}SqList;
Status InitList_Sq(SqList &L)    //初始化线性表
{
    int i;
    L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));//动态分配LIST_INIT_SIZE*sizeof(ElemType)个内存空间
    if(!L.elem) exit(OVERFLOW);
    L.listsize=LIST_INIT_SIZE;     //
    printf("请输入初始线性表长度:n=");
    scanf("%d",&L.length);
    printf("请输入位序从1到%d的各元素(整数),例如:2 4 5 6 7...\n",L.length);
    for(i=0;i<L.length;i++)
        scanf("%d",&L.elem[i]);
    return OK;
}
Status FreeList_Sq(SqList &L)
{
    free(L.elem);//用free释放由malloc()分配的内存
    return OK;
}
Status ListInsert_Sq(SqList &L,int i,ElemType e)//插入
{
    ElemType *newbase,*p,*q;
    if(i<1||i>L.length+1)return ERROR;//超出结点长度的范围
    if(L.length>=L.listsize)
    {
        newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
        if(!newbase)exit(OVERFLOW);
        L.elem=newbase;      //
        L.listsize+=LISTINCREMENT;
    }
    q=&(L.elem[i-1]);
    for(p=&(L.elem[L.length-1]);p>=q;--p)
    *(p+1)=*p;     //
    *q=e;       //
    L.length++;
    return OK;
}
Status ListDelete_Sq(SqList &L,int i,ElemType &e)
{
    ElemType *p,*q;
    if((i<1)||(i>L.length)) return ERROR;
    p=&(L.elem[i-1]);
    e = *p;
    q=L.elem+L.length-1;
    for(++p;p<=q;++p)
        *(p-1)=*p;
    L.length--;
    return OK;
}
void ListDisplay(SqList L)
{
    int i;
    for(i=0;i<L.length;i++)
        printf("[%d:%d]",i+1,L.elem[i]);
    printf("\n");
}
int LocateElem_Sq(SqList L,ElemType e,Status(*compare)(ElemType,ElemType))
{
    int i=1;
    ElemType *p;
    p=L.elem;

    if( e>L.length )
    {
        return 0;
    }

    while(i<L.length && !(compare)(*p++,e)) ++i;
    if(i<=L.length) return i;
    else return 0;
}
void Listlength(SqList L)
{
    printf(":%d\n",L.length);
}
Status equal(ElemType x,ElemType y)
{
    return x==y;
}
void main()
{
    SqList L;
    Status temp;
    int c,i,e = 0,flag=1;
    system("CLS");
    printf("    ---------------------------------------\n");
    printf("    ---------------------------------------\n");
    printf("         顺序表演示程序!                   \n");
    printf("    可以实现查找、插入、删除等操作!        \n");
    printf("    ---------------------------------------\n");
    printf("    ---------------------------------------\n");
    printf("\n");
    InitList_Sq(L);
    while(flag)
    {
        printf(" 请选择:\n");
        printf("1.显示所有元素\n");
        printf("2.插入一个元素\n");
        printf("3.删除一个元素\n");
        printf("4.查找一个元素\n");
        printf("5.线性表的长度\n");
        printf("6.退出程序    \n");
        scanf("%d",&c);
        switch(c)
        {
        case 1:
                 ListDisplay(L);
                break;
        case 2:
            printf("请输入要插入元素的位置和数值:\n");
            printf("格式:位置,数值;例如:2,68\n");
            scanf("%d,%d",&i,&e);
            temp=ListInsert_Sq(L,i,e);
            if(temp==ERROR)
                printf("插入失败!\n");
            else
            {
                printf("插入成功\n");ListDisplay(L);
            }
            break;
        case 3:
            printf("请输入要删除元素的位置:");
            scanf("%d",&i);
            temp=ListDelete_Sq(L,i,e);
            if(temp==OK)
            {
                printf("删除了一个元素:%d\n",e);
                ListDisplay(L);
            }
            else
                printf("该元素不存在!\n");
            break;
        case 4:
            printf("请输入要查找元素的值:\n");
            scanf("%d",&e);
            i=LocateElem_Sq(L,e,equal);
            if(i)
                printf("该元素所在位置:%d\n",i);
            else
                printf("%d 不存在!\n",e);
            break;
        case 5:
            Listlength(L);
            break;
        case 6:
            flag=0;
            printf("程序结束,按任意键退出!\n");
            getch();
        }
    }
    FreeList_Sq(L);
    }
搜索更多相关主题的帖子: 线性表 define windows int include 
2008-01-02 17:59
zhuzhuchar
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-11-24
得分:0 
楼主可以看看这个博客文章
顺序表的实现 [url]http://www.[/url]

/*
 顺序表的功能实现:

  创建CreateList, 销毁DestoryList, 清空ClearList , 追加ListAppend,  
   插入ListInsert, 删除ListDelete,  得到GetElementType, 打印PutList
*/

typedef char ElementType;

typedef struct{
    ElementType * buffer;   /*顺序表空间*/
    int length;             /*顺序表长度*/
    int max;                /*顺序表空间长度*/
}list;

list * CreatList(int);
void DestoryList(list *);
void ClearList(list *);
int ListAppend(list *,ElementType);
int ListInsert(list *,int ,ElementType);
int ListDelet(list *,int );
int GetElementType(list *,int ,ElementType *);
void PutList(list *);
...
...
2008-01-02 18:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-195503-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.881208 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved