注册 登录
编程论坛 数据结构与算法

关于顺序表,这个要怎么修改呢

realll 发布于 2010-07-17 21:00, 619 次点击
只有本站会员才能查看附件,请 登录

这个显示结果错误····不知道怎么修改
//head
#include<iostream>
#include<stdlib.h>
using namespace std;
#define LIST_INIT_SIZE 10 /*线性表初始存储空间分配量*/
#define LISTINCREAMENT 2 /*线性表存储空间分配增量*/
#define ERROR 0
#define OK 1
typedef int Elemtype;

typedef struct
{
    Elemtype *elem;     /*存储空间地址*/
    int length;         /*当前长度*/
    int listsize;       /*当前分配的存储量*/
}Sqlist;

int Initlist(Sqlist &L );
int Get(Sqlist L,int i,Elemtype e);
int compare(Sqlist L,Elemtype e);
int Insertlist(Sqlist &L,int i,Elemtype e);
void Union(Sqlist &La,Sqlist Lb);

//fun
#include"head.h"
int Initlist(Sqlist &L )
{
    L.elem=new Elemtype[LIST_INIT_SIZE];
    if(!L.elem)
        return ERROR;
    L.length=0;
        L.listsize=LIST_INIT_SIZE;
        return OK;
}


int Get(Sqlist L,int i,Elemtype e)
{
    if(i<L.length)
        e=L.elem[i];
    return e;

}
int compare(Sqlist L,Elemtype e)
{
    for(int i=0;i<L.length;i++)
    {
        if(L.elem[i]==e)
        {
            return OK;
            break;
        }
        else
            return ERROR;
    }
}
int Insertlist(Sqlist &L,int i,Elemtype e)
{
    int *newbase,*p,*q;
    if(i<1||i<L.length+1)
        return ERROR;
    if(L.length>L.listsize)
    {

        newbase=(Elemtype *)realloc(L.elem,(L.listsize+LISTINCREAMENT)*(sizeof(Elemtype)));
        if(!newbase)
            return ERROR;
        L.elem=newbase;
        L.listsize+=LISTINCREAMENT;
        q=&L.elem[i-1];
        for(p=&L.elem[L.length-1];p>=q;p--)
        {
            *(p+1)=*p;
            *q=e;
            ++L.length;
            return OK;

        }
    }

}

void Union(Sqlist &La,Sqlist Lb)
{
    Elemtype e;
    int i;
    for(i=0;i<Lb.length;i++)
    {
        Get(Lb,i,e);
        if(!compare(La,e))
            Insertlist(La,i,e);
    }
}

//main
#include"head.h"
void main()
{
    Sqlist La;
    Sqlist Lb;
    La.length=5;
    Lb.length=5;

    Initlist(La);
    Initlist(Lb);
    for(int i=0;i<5;i++)
    {
        La.elem[i]=i+1;
    }
   
    for(int j=0;j<5;j++)
    {
        Lb.elem[j]=(j+5);
    }
    Union(La,Lb);
    for(int k=0;k<=9;k++)
        cout<<La.elem[k]<<endl;
    system("pause");
}



[ 本帖最后由 realll 于 2010-7-17 21:02 编辑 ]
1 回复
1