线性表问题

#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);

 

}

 

 

 

 

 

 

                  

 

 

posted @ 2012-09-26 23:14  ♂咱說 ろ算  阅读(209)  评论(0编辑  收藏  举报