// test.cpp : 定義主控台應用程式的進入點。
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
typedef int elemType;
/************************************************************************/
/* 以下是关于线性表顺序存储操作的16种算法 */
/************************************************************************/
struct List{
elemType *list;
int size;
int maxSize;
};
void againMalloc(struct List *L)
{
/* 空间扩展为原来的2倍,并由p指针所指向,原内容被自动拷贝到p所指向的存储空间 */
elemType *p = (elemType *)realloc(L->list, 2 * L->maxSize * sizeof(elemType));
if(!p){ /* 分配失败则退出运行 */
printf("存储空间分配失败! ");
exit(1);
}
L->list = p; /* 使list指向新线性表空间 */
L->maxSize = 2 * L->maxSize; /* 把线性表空间大小修改为新的长度 */
}
/* 1.初始化线性表L,即进行动态存储空间分配并置L为一个空表 */
void initList(struct List *L, int ms)
{
if(ms<0)
{
printf("Error length!\n");
}
L->size=0;
L->maxSize=ms;
L->list=(elemType *)malloc(ms*sizeof(elemType));
if(!L->list)
{
printf("Failed to malloc!\n");
}
}
/* 2.清除线性表L中的所有元素,释放存储空间,使之成为一个空表 */
void clearList(struct List *L)
{
if(!L->list)
{
free(L->list);
L->maxSize=0;
L->size=0;
}
}
/* 3.返回线性表L当前的长度,若L为空则返回0 */
int sizeList(struct List *L)
{
return L->size;
}
/* 4.判断线性表L是否为空,若为空则返回1, 否则返回0 */
int emptyList(struct List *L)
{
if(L->size ==0){
return 1;
}
else{
return 0;
}
}
/* 5.返回线性表L中第pos个元素的值,若pos超出范围,则停止程序运行 */
elemType getElem(struct List *L, int pos)
{
if(pos<1||pos>L->maxSize)
{
printf("Error id!\n");
return 0;
}
return *(L->list+pos-1);
}
/* 6.顺序扫描(即遍历)输出线性表L中的每个元素 */
void traverseList(struct List *L)
{
int i;
printf("Max Length=%d\n",L->maxSize);
printf("Data Length=%d\n",L->size);
for(i=0;i<L->size;i++)
{
printf("%d ",*(L->list+i));
}
printf("\n");
}
/* 7.从线性表L中查找值与x相等的元素,若查找成功则返回其位置,否则返回-1 */
int findList(struct List *L, elemType x)
{
int i;
if(L->size==0)
return -1;
for(i=0;i<L->size;i++)
{
if(*(L->list+i)==x)
return i+1;
}
return -1;
}
/* 8.把线性表L中第pos个元素的值修改为x的值,若修改成功返回1,否则返回0 */
int updatePosList(struct List *L, int pos, elemType x)
{
if(L->size==0||pos<1||pos>L->maxSize)
return 0;
*(L->list+pos-1)=x;
return 1;
}
/* 9.向线性表L的表头插入元素x */
void inserFirstList(struct List *L, elemType x)
{
int i;
if(L->size==L->maxSize)
againMalloc(L);
if(L->size<L->maxSize)
{
for(i=L->size;i>0;i--)
{
L->list[i]=L->list[i-1];
}
L->list[0]=x;
L->size++;
}
}
/* 10.向线性表L的表尾插入元素x */
void insertLastList(struct List *L, elemType x)
{
if(L->size==L->maxSize)
againMalloc(L);
if(L->size<L->maxSize)
{
L->list[L->size]=x;
L->size++;
}
}
/* 11.向线性表L中第pos个元素位置插入元素x,若插入成功返回1,否则返回0 */
int insertPosList(struct List *L, int pos, elemType x)
{
int i;
if(pos<1||pos>L->maxSize+1)
return 0;
if(L->size==L->maxSize)
againMalloc(L);
if(L->size<L->maxSize)
{
for(i=L->size;i>=pos;i--)
L->list[i]=L->list[i-1];
L->list[pos-1]=x;
L->size++;
return 1;
}
}
/* 12.向有序线性表L中插入元素x, 使得插入后仍然有序(假定升序)*/
void insertOrderList(struct List *L, elemType x)
{
int i,j;
if(L->size==L->maxSize)
againMalloc(L);
for(i=0;i<L->size;i++)
{
if(x<=L->list[i])
break;
}
for(j=L->size;j>i;j--)
{
L->list[j]=L->list[j-1];
}
L->list[i]=x;
L->size++;
}
/* 13.从线性表L中删除表头元素并返回它,若删除失败则停止程序运行 */
elemType deleteFirstList(struct List *L)
{
int i;
elemType temp;
if(L->size==0)
{
printf("Error----> Empty list!!\n");
return -1;
}
temp=L->list[0];
for(i=0;i<L->size-1;i++)
{
L->list[i]=L->list[i+1];
}
L->size--;
return temp;
}
/* 14.从线性表L中删除表尾元素并返回它,若删除失败则停止程序运行 */
elemType deleteLastList(struct List *L)
{
int i;
elemType temp;
if(L->size==0)
{
printf("Error----> Empty list!!\n");
return -1;
}
temp=L->list[L->size-1];
L->size--;
return temp;
}
/* 15.从线性表L中删除第pos个元素并返回它,若删除失败则停止程序运行 */
elemType deletePosList(struct List *L, int pos)
{
int i;
elemType temp;
if(L->size==0)
{
printf("Error----> Empty list!!\n");
return -1;
}
if(pos<1||pos>L->size)
{
printf("Error----> Out array!!\n");
return -1;
}
temp=L->list[pos-1];
for(i=pos-1;i<L->size-1;i++)
{
L->list[i]=L->list[i+1];
}
L->size--;
return temp;
}
/* 16.从线性表L中删除值为x的第一个元素,若成功返回1,失败返回0 */
int deleteValueList(struct List *L, elemType x)
{
int i,j;
if(L->size==0)
{
printf("Error----> Empty list!!\n");
return 0;
}
for(i=0;i<L->size;i++)
{
if(L->list[i]==x)
break;
}
if(i<L->size)
{
for(j=i;j<L->size-1;j++)
L->list[j]=L->list[j+1];
L->size--;
return 1;
}
return 0;
}
int main()
{
int a[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int i;
struct List L;
initList(&L, 5);
traverseList(&L);
for(i = 0; i < 10; i++){
insertLastList(&L, a[i]);
}
traverseList(&L);
insertPosList(&L, 11, 48);
traverseList(&L);
insertPosList(&L, 2, 3);
traverseList(&L);
printf("%d \n", getElem(&L, 1));
printf("%d \n", findList(&L, 10));
updatePosList(&L, 3, 20);
traverseList(&L);
deleteFirstList(&L);
traverseList(&L);
deleteFirstList(&L);
traverseList(&L);
deleteLastList(&L);
traverseList(&L);
deleteLastList(&L);
traverseList(&L);
deletePosList(&L, 5);
traverseList(&L);
printf("%d \n", sizeList(&L));
printf("%d \n", emptyList(&L));
clearList(&L);
traverseList(&L);
while(1);
}
数据结构C语言实现系列——线性表
最新推荐文章于 2025-08-05 20:53:10 发布