关于顺序表的操作

/*顺序表的基本操作*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 3
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct {
     int *elem;
     int length;
     int listsize;
     }SqList;

Status Initlist_Sq(SqList &L) /*初始化顺序表*/
{ L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  if(!L.elem) exit(OVERFLOW);
  L.length=0;
  L.listsize=LIST_INIT_SIZE;
  return OK;
}

void Destroylist(SqList &L) /*销毁顺序表*/
{
  free(L.elem);
  L.elem=NULL;
  L.length=0;
}

void Clearlist_Sq(SqList &L) /*清空顺序表*/
{ L.length=0; }

Status Listempty_Sq(SqList L) /*测试顺序表是否为空*/
{
   if(L.length!=0) return(FALSE);
   return(TRUE);
}

Status ListInsert_Sq(SqList &L, int i,ElemType e) /*在第i个位置上插入一个元素*/
{ int j,*newbase;
  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;
       }
  for (j=L.length; j>=i ; --j)
     L.elem[j]=L.elem[j-1];
  L.elem[j]=e ;
  ++L.length ;
  return OK;
}

int LocateElem_Sq(SqList L,ElemType e) /*返回元素e在顺序表中的位置*/
{ int i=1;
  ElemType *p;
  p=L.elem;
  while(i<=L.length &&!(*p==e)) {i++; p++;}
  if(i<=L.length) return i;
  return FALSE;
}

Status ListDelete_Sq(SqList &L, int i, int &e) /*删除第i个位置上的元素*/
{ int j;
  if(i<1 || i>L.length) return ERROR;
  e=L.elem[i-1];
  for( j=i; j<L.length;j++)
     L.elem[j-1]=L.elem[j];
  --L.length;
  return OK;
}

void Print_Sq(SqList L) /*输出顺序表*/
{    int i;
     if (Listempty_Sq(L))
       printf("/nSequential List's length is %d. It's empty!",L.length);
     else
       printf("/nSequential List's length is %d. These element are : ",L.length);
     for(i=0;i<L.length;i++)
       printf(" %d",L.elem[i]);
}


void menu()
{  printf("/n");
   printf("/n* * * * * * * * * * * * * * * * * * * * * * * * * */n");
   printf("  1 ------- Print the Sequential List./n");
   printf("  2 ------- Insert a data in the Sequential List./n");
   printf("  3 ------- Delete a data in the Sequential List./n");
   printf("  4 ------- Get a element's locationt in the SqList./n");
   printf("  5 ------- Clear the Sequential List./n");
   printf("  6 ------- Exit./n");
   printf("* * * * * * * * * * * * * * * * * * * * * * * * * */n");
   printf("/n");
}

void menuselect(SqList L)
{ int k,i,done=1;
  ElemType e;
  while (done)
   { menu();
     printf("Please choose: ");
     scanf("%d",&k);
     getchar();
     switch(k)
      { case 1:  Print_Sq(L); break;
 case 2:{
     printf("/nInput the location and data you want to Insert: ");
     scanf("%d,%d",&i,&e);
     if (ListInsert_Sq(L,i,e)==ERROR) printf("Insert location is not correct!/n");
     break;
    }

 case 3:{ printf("/nInput the location you want to delete data: ");
   scanf("%d",&i);
   if (ListDelete_Sq(L,i,e)==ERROR)
    printf("Delete location is not exit!/n");
   else
    printf("/nDelete data is %d.",e);
   break; }
 case 4:{ printf("/nInput the data you want to find: ");
   scanf("%d",&e);
    if (LocateElem_Sq(L,e)!=FALSE)
      printf("/nData %d location in the Sequential List is %d.", e,LocateElem_Sq(L,e));
    else printf("/nData %d is not in the Sequential List./n", e);
   break; }
 case 5: Clearlist_Sq(L);;   break;
 case 6: done=0;
      }
  }
}
void main()
{    ElemType e;
     SqList La;
     int n,i;
     clrscr();
     Initlist_Sq(La);
     printf("Input a number of the element in the Sequential List (n<=%d):",LIST_INIT_SIZE);
     scanf("%d",&n);
     printf("Enter these elements:");
     for(i=1;i<=n;i++)
  {scanf("%d",&e);
   ListInsert_Sq(La,i,e);}
     menuselect(La);
     getch();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值