线性表的定义 线性表是最简单最常用的一种数据结构,它是一种能在任意位置进行插入和删除数据元素操作的、由n(n>=0)个相同数据元素组成的线性结构。 线性表的抽象数据类型 数据集合 线性表的数据集合可以表示为a0, a1, a2, a3, ……, an-1,每个数据元素的数据类型都是抽象数据元素的数据类型ElemType。 操作集合(在此仅罗列一部分,详细内容请看代码块) a. 初始化 InitiateList(L):初始化线性表L b. 求当前数据元素个数ListLebgth(L) c. 插入数据元素ListInset(L, i, x): 在线性表L的第i个数据元素前插入数据元素x d. 删除数据元素ListDelete(L, i, x):删除线性表第i个数据元素x e. 取数据元素ListGet(L, i, x): 取线性表的第i个数据元素x
#define ElemType int
#include<malloc.h>
#include<string.h>
#define SIZE 10
#define NEWSIZE 4
typedef struct SeqList
{
ElemType *base;
size_t capacity;
size_t length;
}SeqList;
int NewSpace(SeqList *list )
{
ElemType *newspace = (ElemType*)malloc (sizeof (ElemType)*(list ->capacity + NEWSIZE));
if (newspace == NULL)
{
printf ("Out Of Memory!\n" );
return 0 ;
}
memcpy (newspace, list ->base, sizeof (ElemType)*list ->capacity);
free (list ->base);
list ->base = newspace;
list ->capacity += NEWSIZE;
return 1 ;
}
void InitSeqList(SeqList *list )
{
list ->capacity = SIZE;
list ->base = (ElemType *)malloc (sizeof (ElemType)*list ->capacity);
list ->length = 0 ;
}
int BegInsert(SeqList *list , ElemType a)
{
if (list ->length >= list ->capacity)
{
printf ("空间已满,不能插入!\n" );
return 0 ;
}
if (NULL == list ->base)
{
printf ("out of memory\n" );
return 0 ;
}
for (int i=list ->length; i>0 ; i--)
{
list ->base[i] = list ->base[i-1 ];
}
list ->base[0 ] = a;
list ->length++;
return 1 ;
}
int EndInsert(SeqList *list , ElemType a)
{
if (NULL == list ->base)
{
printf ("out of memory\n" );
return 0 ;
}
if (list ->length >= list ->capacity && !NewSpace(list ))
{
printf ("空间已满,无法插入!\n" );
return 0 ;
}
list ->base[list ->length++] = a;
return 1 ;
}
void ShowList(SeqList *list )
{
for (int i=0 ; i<list ->length; i++)
{
printf ("%d " ,list ->base[i]);
printf (" " );
}
printf ("\n" );;
}
int BegDelete(SeqList *list )
{
if (list ->length == 0 )
{
printf ("顺序表已空,无数据可删!\n" );
return 0 ;
}
for (int i=0 ; i<list ->length-1 ; i++)
{
list ->base[i] = list ->base[i+1 ];
}
list ->length--;
return 1 ;
}
int EndDelete(SeqList *list )
{
if (list ->length == 0 )
{
printf ("顺序表已空,无数据可删!\n" );
return 0 ;
}
list ->length--;
return 1 ;
}
void SortList(SeqList *list )
{
int i, j;
for (i=0 ; i<list ->length; i++)
{
for (j=0 ; j<list ->length-i-1 ; j++)
{
if (list ->base[j] > list ->base[j+1 ])
{
int temp = list ->base[j];
list ->base[j] = list ->base[j+1 ];
list ->base[j+1 ] = temp;
}
}
}
}
int InsertBVal(SeqList *list , ElemType a)
{
if (list ->length >= list ->capacity)
{
printf ("空间已满,不能插入!\n" );
return 0 ;
}
if (list ->length == 0 )
{
list ->base[0 ] = a;
}
else
{
int pos;
for (pos=0 ; pos<list ->length; pos++)
{
if (list ->base[pos] > a)
break ;
}
for (int i=list ->length; i>pos; i--)
{
list ->base[i] = list ->base[i-1 ];
}
list ->base[pos] = a;
}
list ->length++;
return 1 ;
}
int InsertBPos(SeqList *list , int pos, ElemType a)
{
if (list ->length >= list ->capacity)
{
printf ("空间已满,不能插入!\n" );
return 0 ;
}
if (pos<0 || pos>list ->length)
{
printf ("插入元素位置出错!\n" );
return 0 ;
}
for (int i=list ->length; i>pos; i--)
{
list ->base[i] = list ->base[i-1 ];
}
list ->base[pos] = a;
list ->length++;
return 1 ;
}
int DelByPos(SeqList *list , int pos)
{
int i;
if (list ->length == 0 )
{
printf ("顺序表已空,无数据可删!\n" );
return 0 ;
}
for (i=0 ; i<pos; i++)
{
if (i == pos)
break ;
}
for (i=pos; i<list ->length; i++)
{
list ->base[i] = list ->base[i+1 ];
}
list ->length--;
return 1 ;
}
int DelByValue(SeqList *list , ElemType a)
{
int i, j;
if (list ->length == 0 )
{
printf ("顺序表已空,无数据可删!\n" );
return 0 ;
}
for (i=0 ; i<list ->length; i++)
{
if (list ->base[i] == a)
break ;
}
for (j=i; j<list ->length; j++)
{
list ->base[j] = list ->base[j+1 ];
}
list ->length--;
return 1 ;
}
void FindElem(SeqList *list , ElemType a)
{
int i;
if (list ->length ==0 )
{
printf ("顺序表为空,没有数据!\n" );
return ;
}
else
{
for (i=0 ; i<list ->length; i++)
{
if (list ->base[i] == a)
break ;
}
printf ("该数据在顺序表的%d的位置处\n" ,i);
}
}
int ListLength(SeqList *list )
{
printf ("该顺序表中有%d个数据\n" ,list ->length);
return 1 ;
}
void CleanList(SeqList *list )
{
if (list ->length == 0 )
{
printf ("顺序表为空!\n" );
}
else
{
list ->length = 0 ;
printf ("顺序表已清空!\n" );
}
}
void ReverseShow(SeqList *list )
{
if (list ->length == 0 )
{
printf ("顺序表为空!\n" );
}
else
{
for (int i=list ->length-1 ; i>=0 ; --i)
{
printf ("%d " ,list ->base[i]);
}
printf ("\n" );
}
}
void DestroyList(SeqList *list )
{
free (list ->base);
list ->base = NULL;
}
#include<stdio.h>
#include"SeqList.h"
void main()
{
SeqList list ;
InitSeqList(&list );
ElemType x;
int pos;
int select = 1 ;
while (select)
{
printf ("*************************************************\n" );
printf ("*[1] BegInsert [2] EndInsert [3] ShowList *\n" );
printf ("*[4] BegDelete [5] EndDelete [6] SortList *\n" );
printf ("*[7] InsertBPos [8] InsertBVal [9] DelByPos *\n" );
printf ("*[10]DelByValue [11]FindElem [12]ListLength *\n" );
printf ("*[13]CleanList [14]ReverseShow [15]DestroyList*\n" );
printf ("*************** [0] QuitSystem ******************\n" );
printf ("*************************************************\n" );
printf ("请选择: " );
scanf ("%d" ,&select);
switch (select)
{
case 0 :
break ;
case 1 :
printf ("请输入要插入的数据(以-1结束):> " );
while (scanf ("%d" ,&x),x != -1 )
{
BegInsert(&list , x);
}
break ;
case 2 :
printf ("请输入要插入的数据(以-1结束):> " );
while (scanf ("%d" ,&x),x != -1 )
{
EndInsert(&list , x);
}
break ;
case 3 :
ShowList(&list );
break ;
case 4 :
BegDelete(&list );
break ;
case 5 :
EndDelete(&list );
break ;
case 6 :
SortList(&list );
break ;
case 7 :
printf ("请输入要插入的数据:> " );
scanf ("%d" ,&x);
printf ("请输入要插入的位置:> " );
scanf ("%d" ,&pos);
InsertBPos(&list , pos, x);
break ;
case 8 :
SortList(&list );
printf ("请输入要插入的数据:> " );
scanf ("%d" ,&x);
InsertBVal(&list , x);
break ;
case 9 :
printf ("请输入位置:> " );
scanf ("%d" ,&pos);
DelByPos(&list , pos);
break ;
case 10 :
printf ("请输入要删除的元素:> " );
scanf ("%d" ,&x);
DelByValue(&list , x);
break ;
case 11 :
printf ("请输入要查找的数据:> " );
scanf ("%d" , &x);
FindElem(&list , x);
break ;
case 12 :
ListLength(&list );
break ;
case 13 :
CleanList(&list );
break ;
case 14 :
ReverseShow(&list );
break ;
case 15 :
DestroyList(&list );
break ;
}
}
}