采用函数实现和声明分开,SeqList.h包含结构体声明和所有函数的声明,SeqList.c包含所有函数的实现,main.c主函数实现,代码在vs中测试运行通过,其中部分代码是假设存储的数中没有重复的数
//main.c 主函数实现
#pragma once
#include "SeqList.h"
#include <stdio.h>
#pragma warning(disable : 4996)
int main()
{
SeqList mylist;
ElemType Item;
int pos;
int select = 1;
Init(&mylist);
while (select)
{
printf("**************************************\n");
printf("* [1] push_back [2] push_front *\n");
printf("* [3] show_list [4] pop_back *\n");
printf("* [5] pop_front [6] insert_pos *\n");
printf("* [7] find [8] length *\n");
printf("* [9] delete_pos [10] delete_val *\n");
printf("* [11] sort [12] resver *\n");
printf("* [13] clear [14*] destroy *\n");
printf("* [0] quit_system *\n");
printf("**************************************\n");
printf("请输入选项:>");
scanf("%d", &select);
if (select==0)
{
break;
}
if (!(select > 0 && select <= 14))
{
printf("输入有误,请重新输入\n");
}
switch (select)
{
case 0:
break;
case 1:
printf("请输入要插入的数字,以-1结束:");
while (scanf("%d", &Item), Item != -1)
{
push_back(&mylist, Item);
}
break;
case 2:
printf("请输入要插入的数字,以-1结束:");
while (scanf("%d", &Item), Item != -1)
{
push_front(&mylist, Item);
}
break;
case 3:
show(mylist);
break;
case 4:
pop_back(&mylist);
break;
case 5:
pop_front(&mylist);
break;
case 6:
printf("请输入要插入位置的下标:");
scanf("%d", &pos);
printf("请输入要插入的数字,以-1结束输入:");
while (scanf("%d", &Item), Item != -1)
{
insert_pos(&mylist, pos, Item);
}
break;
case 7:
printf("请输入要查找的数的值,以-1结束输入:");
while (scanf("%d", &Item), Item != -1)
{
int tmp = find(mylist, Item);
if (tmp >= 0)
{
printf("要查找的数在下标为%d处\n", find(mylist, Item));
}
else
{
printf("未找到\n");
}
}
break;
case 8:
printf("线性表的长度为%d\n", length(mylist));
break;
case 9:
printf("请输入要删除数的下标,以-1结束输入:");
while (scanf("%d", &pos), pos != -1)
{
delete_pos(&mylist, pos);
}
break;
case 10:
printf("请输入要删除数据的值,以-1结束输入");
while (scanf("%d", &Item), Item != -1)
{
delete_val(&mylist, Item);
}
break;
case 11:
sort(&mylist);
break;
case 12:
resver(&mylist);
break;
case 13:
clear(&mylist);
break;
case 14:
break;
default:
break;
}
}
free(mylist.base);
mylist.base = NULL;
return 0;
}
//SeqList.h结构体声明和所有函数的声明
#pragma once
typedef int ElemType;
typedef struct SeqList
{
ElemType *base;
int capacicty;
int size;
}SeqList;
void Init(SeqList *mylist);
void show(SeqList mylist);
void push_back(SeqList *mylist, ElemType Item);
void push_front(SeqList *mylist,ElemType Item);
void pop_back(SeqList *mylist);
void pop_front(SeqList *mylist);
void insert_pos(SeqList *mylist, int pos, int Item);
int find(SeqList mylist, ElemType Item);
int length(SeqList mylist);
void delete_pos(SeqList *mylist, int pos);
void delete_val(SeqList *mylist, ElemType Item);
void sort(SeqList *mylist);
void resver(SeqList *mylist);
void clear(SeqList *mylist);
//SeqList.c所有函数的实现
#include "SeqList.h"
#include <stdio.h>
void Init(SeqList *mylist)
{
mylist->base = (ElemType *)malloc(sizeof(ElemType) * 8);
mylist->capacicty = 8;
mylist->size = 0;
}
void push_back(SeqList *mylist, ElemType Item)
{
if (mylist->size >= mylist->capacicty)
{
printf("空间已满\n");
return;
}
mylist->base[mylist->size] = Item;
mylist->size++;
}
void push_front(SeqList *mylist,ElemType Item)
{
if (mylist->size >= mylist->capacicty)
{
printf("空间已满\n");
return;
}
for (int i = mylist->size; i >= 0; i--)
{
mylist->base[i + 1] = mylist->base[i];
}
mylist->base[0] = Item;
mylist->size++;
}
void show(SeqList mylist)
{
for (int i = 0; i < mylist.size; i++)
{
printf("%d ", mylist.base[i]);
}
printf("\n");
printf("%d\n", mylist.size);
}
void pop_back(SeqList *mylist)
{
if (mylist->size == 0)
{
printf("线性表已为空\n");
return;
}
mylist->size--;
}
void pop_front(SeqList *mylist)
{
if (mylist->size == 0)
{
printf("线性表已为空\n");
return;
}
if (mylist->size == 1)
{
mylist->size--;
return;
}
for (int i = 0; i < mylist->size - 1; i++)
{
mylist->base[i] = mylist->base[i + 1];
}
mylist->size--;
}
void insert_pos(SeqList *mylist, int pos, int Item)
{
if (mylist->size >= mylist->capacicty)
{
printf("空间已满\n");
return;
}
if (!(pos >= 0 && pos <= mylist->size))
{
printf("输入的下标位置错误,请重新输入");
return;
}
for (int i = mylist->size; i >= pos; i--)
{
mylist->base[i+1] = mylist->base[i];
}
mylist->base[pos] = Item;
mylist->size++;
}
int find(SeqList mylist, ElemType Item)
{
for (int i = 0; i < mylist.size; i++)
{
if (mylist.base[i] == Item)
{
return i;
}
}
return -2;
}
int length(SeqList mylist)
{
return mylist.size;
}
void delete_pos(SeqList *mylist, int pos)
{
if (!(pos >= 0 && pos <= mylist->size))
{
printf("输入的下标位置错误,请重新输入");
return;
}
for (int i = pos; i < mylist->size - 1; i++)
{
mylist->base[i] = mylist->base[i + 1];
}
mylist->size--;
}
void delete_val(SeqList *mylist, ElemType Item)
{
int pos = find(*mylist, Item);
if (pos < 0)
{
printf("未找到该数值为%d的数,请重新输入", Item);
return;
}
delete_pos(mylist, pos);
}
void sort(SeqList *mylist)
{
if (mylist->size == 0)
{
printf("线性表已为空\n");
return;
}
int tmp;
for (int i = 0; i < mylist->size; i++)
{
for (int j = 0; j < mylist->size - i - 1; j++)
{
if (mylist->base[j] > mylist->base[j + 1])
{
tmp = mylist->base[j];
mylist->base[j] = mylist->base[j + 1];
mylist->base[j + 1] = tmp;
}
}
}
}
void resver(SeqList *mylist)
{
if (mylist->size == 0)
{
printf("线性表已为空\n");
return;
}
int tmp;
for (int i = 0, j = mylist->size - 1; i < j; i++, j--)
{
tmp = mylist->base[i];
mylist->base[i] = mylist->base[j];
mylist->base[j] = tmp;
}
}
void clear(SeqList *mylist)
{
mylist->size = 0;
}