标签:顺序表实现
顺序表:逻辑上连续,物理结构上也连续
实现一个具有“增、删(根据位置删除,根据元素删除)、查、初始化、输入、打印”等简单功能的顺序表:
源码:
头文件部分(#include"sequence.h"):
#include <stdio.h>
#include<stdlib.h>
#define MAXSIZE 10
typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int last;
}SeqList;
void input(SeqList *L);
void output(SeqList * L);
int GetData(SeqList *L, int x);
int ListDelete(SeqList *L, int i);
int InsertSList(SeqList *L, int i, int x);
int GetLength(SeqList *L);
int InitSList(SeqList *L);
封装函数部分(#include"sequence.c"):
void input(SeqList *L)
{
for (int n = 0; n <= L->last; n++)
scanf("%d", &(L->data[n]));
}
void output(SeqList * L)
{
for (int n = 0; n <= L->last; n++)
printf("%d ", L->data[n]);
printf("\n");
}
int GetData(SeqList *L, int x)
{
int i = 0;
if (L->last == -1)
return -1;
while (i <= L->last)
{
if (L->data[i] == x)
return i + 1;
i++;
}
return -1;
}
int ListDelete(SeqList *L, int i)
{
if ((i > L->last + 1) || (i < 1))
return -1;
if (L->last == -1)
return -1;
for (int n = i - 1; n <L->last; n++)
{
L->data[n] = L->data[n + 1];
}
L->last--;
return 1;
}
int InsertSList(SeqList *L, int i, int x)
{
if ((i<1) || i>(L->last + 2))
return -1;
if ((L->last) == (MAXSIZE - 1))
return -1;
for (int n = L->last; n >= i - 1; n--)
L->data[n + 1] = L->data[n];
L->data[i - 1] = x;
L->last++;
return 1;
}
int GetLength(SeqList *L)
{
return (L->last + 1);
}
int InitSList(SeqList *L)
{
L->last = -1;
return 1;
}
主函数部分:
#include"sequence.h"
int main()
{
int m = 0;
int n = 0;
int i = 0;
int ret=0;
ElemType x;
SeqList L;
printf("***********************************************\n");
printf("***********************************************\n");
printf("********0.exit 1.InitSList*********\n");
printf("********2.InsertSList 3.ListDelete********\n");
printf("********4.GetData 5.GetLength*********\n");
printf("********6.input 7.output************\n");
while (1)
{
while (1)
{
printf("请选择功能: ");
scanf("%d", &n);
if (n >= 0 && n <= 7)
break;
else
printf("选择无效,请重新选择\n");
}
switch (n)
{
case 0:
exit(1);
break;
case 1:
InitSList(&L);
printf("初始化成功\n");
break;
case 2:
printf("请输入要插入的位置: 和要插入的元素:\n");
scanf("%d%d", &i, &x);
ret = InsertSList(&L, i, x);
if (ret == 1)
printf("插入成功\n");
else
printf("插入失败\n");
break;
case 3:
printf("****1、位置删除 2、元素删除****\n");
printf("请选择删除方式:");
while (1)
{
scanf("%d", &m);
if (m==1||m==2)
break;
else
printf("选择无效,请重新选择\n");
}
switch (m)
{
case 1:
{
printf("请输入要删除的位置:");
scanf("%d", &i);
ret = ListDelete(&L,i);
break;
}
case 2:
{
int i = 0;
printf("输入要删除的元素:");
scanf("%d", &x);
while (1)
{
i = GetData(&L, x);
if (i ==-1)
break;
ret = ListDelete(&L, i);
}
break;
}
}
if (ret == 1)
printf("删除成功\n");
else
printf("删除失败\n");
break;
case 4:
printf("请输入要查找的元素:");
scanf("%d",&x);
ret=GetData(&L,x);
if (ret==-1)
printf("查询无果\n");
else
printf("查找成功:%d\n",ret);
break;
case 5:
ret=GetLength(&L);
printf("顺序表长度:%d\n", ret);
break;
case 6:
printf("请输入顺序表长度:");
scanf("%d", &L.last);
L.last = L.last - 1;
printf("请输入顺序表:");
input(&L);
printf("输入成功\n");
break;
case 7:
printf("打印顺序表:");
output(&L);
break;
}
ret = 0;
}
system("pause");
return 0;
}本文出自 “11132019” 博客,请务必保留此出处http://11142019.blog.51cto.com/11132019/1760947
标签:顺序表实现
原文地址:http://11142019.blog.51cto.com/11132019/1760947