线性表问题

#include<iostream>

#include<string>

using namespace std;

#define LIST_INIT_SIZE 100

#define LISTINCREMENT 10

typedef int Status,ElemType;

typedef struct

{

         ElemType *elem;

         int length;

         int listsize;

}SqList;

void Init_Sq(SqList &L)

{

         L.elem=new ElemType[LIST_INIT_SIZE];

         if(L.elem==NULL)

                   exit(1);

         L.length=0;

         L.listsize=LIST_INIT_SIZE;

//      return true;

}

void creat(SqList &L)

{

         cout<<"请建立数据(输入-1结束)"<<endl;

         cin>>L.elem[0];

         while(L.elem[L.length]!=-1)

         {

                   //L.length++;

                   cin>>L.elem[++L.length];

         }

}

void ListInert_Sq(SqList &L,int i,ElemType e)

{

         ElemType *newbase;

         if(i<1||i>(L.length+1))

                   return;

         if(L.length>=L.listsize)

         {

                   newbase=new ElemType[LIST_INIT_SIZE+LISTINCREMENT];

                   if(!newbase)

                            exit(0);

                   L.elem=newbase;

                   L.listsize=LIST_INIT_SIZE+LISTINCREMENT;

         }

         ElemType *q,*p;

         q=&(L.elem[i-1]);

         for(p=&(L.elem[L.length-1]);p>=q;--p)

                   *(p+1)=*p;

         *q=e;

         ++L.length;

}

void output(SqList&L)

{

         cout<<"输出数据为:"<<endl;

         for(int i=0;i<L.length;i++)

                   cout<<L.elem[i]<<'\t';

         cout<<endl;

}

void ListDelete(SqList&L,int i,ElemType &e)

{

         ElemType *p,*q;

         if((i<1)||(i>L.length))

                   return;

         p=&(L.elem[i-1]);

         e=*p;

         q=&(L.elem[L.length-1]);

         for(++p;p<=q;++p)

                   *(p-1)=*p;

         --L.length;

}

void main()

{

         SqList a;

         Init_Sq(a);

         creat(a);

         output(a);

         cout<<"请输入要插入的位置和元素值"<<endl;

         int num,num1;

         cin>>num>>num1;

         ListInert_Sq(a,num,num1);

         output(a);

    cout<<"请输入要删除的位置"<<endl;

         cin>>num;

         ListDelete(a,num,num1);

         output(a);

 

}

 

 

 

 

 

 

                  

 

 

转载于:https://www.cnblogs.com/-wang-xin/archive/2012/09/26/2704867.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值