| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 926 人关注过本帖, 1 人收藏
标题:帮忙看一下这个线性表,结果总不是我想要的,实现的是线性表的插入,删除, ...
取消只看楼主 加入收藏
余年
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-3-23
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:2 
帮忙看一下这个线性表,结果总不是我想要的,实现的是线性表的插入,删除,合并等
#include<malloc.h>
#include<stdlib.h>
#include<iostream.h>
#include<string.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define TRUE 1
#define FALSE 0

class SqList   // 定义一个名为SqList的类
{
  int *elem;
  int length;
  int listsize;

public:

    int InitList_Sq(SqList &L)    //初始化列表,构建一个空的线性表L
    {
        L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
        if(!L.elem)            
           exit (OVERFLOW);
        L.length=0;
        L.listsize=LIST_INIT_SIZE;
        return OK;
    }
   

    int GetElem_Sq(SqList &L,int i,int *e)//从线性表中获取第i位置元素的值,并用e返回其值
    {  
        e=&(L.elem[i-1]);
        return *e;
    }

    int ListInsert_Sq(SqList L,int i,int e)//在i位置之前插入e
    {
        int *p,*q,*newbase;
        if(i<=1||i>L.length+1)//i值不合法
            return ERROR;
        if(L.length>=L.listsize)//当前存储空间已满,增加分配
        {
            newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
            if(!newbase)//存储分配失败
                exit(OVERFLOW);
            L.elem=newbase;//新基址
            L.listsize+=LISTINCREMENT;//增加存储容量
        }
        q=&(L.elem[i-1]);//q指向第i位置的元素,即q为插入的位置
        for( p=&(L.elem[L.length-1]);p>=q;--p)
            *(p+1)=*p;//p指向最后一个元素
        *q=e;//插入e
        ++L.length;//表长增1
        return OK;
    }


    int ListDelete_Sq(SqList &L,int i,int &e)    //删除i位置上的值,并用e返回其值
    {
        int *p,*q;
        if(i<1||i>=L.length)//i值不合法
            return ERROR;
        p=&(L.elem[i-1]);//p指向第i位置的元素
        e=*p;    //被删除元素的值赋给e
        q=L.elem+L.length-1;//q指向最后一个元素
        for(++p;p<=q;++p)//被删除元素之后的元素往前移
            *(p-1)=*p;
        --L.length;//表长减1
        return OK;
    }


    int compare(int a,int b)
    {
         return ( ((a) == (b)) ? (1) : (0) );
    }

    int LocateElem_Sq(SqList &L,int e)//在线性表中查找元素
    {
        int i=1,*p;//i的初值为第一个元素的位序
        p=L.elem;//p的初值为第一个元素的存储位置
        while((i<=L.length) && !(compare)(*p++,e))
            ++i;
        if(i<=L.length)   
            return i;
        else
            return 0;
    }


    int  Input(int n)//从线性表中输入数据的个数
    {
        cout<<"向线性表中输入这n个数据:"<<endl;
        for(int i=0;i<n;i++)
           cin>>elem[i];
        length=length +n;
        return OK;
    }

    int Output(int n)//从线性表中输出数据的个数
    {
        for(int i=0;i<n;i++)
            cout<<elem[i]<<" ";
            cout<<endl;
        return OK;
    }
   
    int Listlength_Sq(SqList L)
    {
        return L.length;
    }


    SqList Union_Sq(SqList La,SqList Lb)//将所有在线性表Lb中但不在La中的数据元素插入到La中
    {
        int La_len,Lb_len;
        int i;
        int e;
        La_len=Listlength_Sq(La);//求线性表La的长度
         Lb_len=Listlength_Sq(Lb);//求线性表Lb的长度
        for(i=1;i<=Lb_len;i++)
        {
            GetElem_Sq(Lb,i,&e);//取Lb中第i个数据元素赋给e
            if(!LocateElem_Sq(Lb,e))
            {
                if(*Lb.elem!=e)
                 ListInsert_Sq(La,++La_len,e);//La中不存在和e相同的数据元素,则插入之
             }
            return La;
        }
    }

    void Output_List_Sq(SqList &L)
    {
        int i = 0;
        if(L.length == 0)
        {
            cout<<"这是一个空表!";
        }
        while(i < L.length)
        {
            cout<<L.elem[i++];
        }
        cout<<endl;
    }

};



 void main()
    {
        int a,b,c,d,e,n1,n2;
        SqList La,Lb,Lc;
        La.InitList_Sq(La);
        Lb.InitList_Sq(Lb);
        cout<<"请输入线性表La中的数据个数:"<<endl;
        cin>>n1;
        La.Input(n1);
        cout<<"线性表La为:"<<endl;
        La.Output(n1);
        cout<<"请输入线性表Lb中的数据个数:"<<endl;
        cin>>n2;
        Lb.Input(n2);
        cout<<"线性表Lb为:"<<endl;
        Lb.Output(n2);
        La.Union_Sq(La,Lb);
        cout<<"合并成为一个新集合后的线性表的数据为:"<<endl;
        La.Output_List_Sq(La);
        cout<<"请输入线性表La插入位置a和插入的数据b:"<<endl;
        cin>>a>>b;
        La.ListInsert_Sq(La,a,b);
        cout<<"插入后的线性表La为:"<<endl;
        La.Output(n1);   
        cout<<"请输入线性表La删除位置c:"<<endl;
        cin>>c;
        La.ListDelete_Sq(La,c,e);
        cout<<"删除之后的线性表La:"<<endl;
        La.Output(n1);
        cout<<"线性表La删除的数据是"<<e<<endl;
        cout<<"请输入线性表La要查找的数据d:"<<endl;
        cin>>d;
        cout<<"数据d在线性表La中的位置:"<<La.LocateElem_Sq(La,d)<<endl;         
    }
搜索更多相关主题的帖子: include public 线性表 
2013-03-23 16:46
余年
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-3-23
收藏
得分:0 
  我想实现的是顺序表的插入,删除,查找,以及俩个集合的合并并输出,谢谢啦
2013-03-23 22:35
余年
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-3-23
收藏
得分:0 
谢谢你啦   嘿嘿
2013-04-20 16:03
快速回复:帮忙看一下这个线性表,结果总不是我想要的,实现的是线性表的插入,删 ...
数据加载中...
 
   



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

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