1.顺序表功能的.h文件
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int typedata;
typedef struct Seqlist{
typedata* node;
int size;
int capacity;
}seqlist;
void menu();
void seqlist_init(seqlist * list);
void seqlistpush_back(seqlist* list);
void seqlistpush_front(seqlist* list);
void seqlistpop_back(seqlist* list);
void seqlistpop_front(seqlist* list);
void seqlistpush_onin(seqlist* list, int n);
void seqlistpop_onin(seqlist* list, int n);
2.顺序表包含的功能的.c文件
#include "seqlist.h"
void menu() {
printf("******************************\n");
printf("***1.尾插 2.头插***\n");
printf("***3.尾删 4.头删***\n");
printf("***5.内扩 6.内删***\n");
printf("******************************\n");
printf("******************************\n");
}
void seqlist_init(seqlist* list) {
list->size = 0;
list->capacity = 4;
list->node = (typedata*)malloc(list->capacity * sizeof(typedata));
memset(list->node, 0, sizeof(typedata) * list->capacity);
}
void seqlist_print(seqlist* list) {
int i=0;
printf("接下来将对顺序表内容进行展示\n");
for (i = 0; i < list->size; i++) {
printf("%d ", list->node[i]);
}
printf("\n");
printf("-----------------------------\n");
}
void seqlist_full(seqlist* list) {
if (list->capacity == list->size) {
seqlist* tmp = (seqlist*)realloc(list->node, list->capacity * 2 * sizeof(typedata));
if (tmp == NULL) {
perror("扩容出错\n");
exit(1);
}
list = tmp;
list->capacity = 2 * list->capacity;
}
}
void seqlistpush_back(seqlist* list) {
printf("请输入你要插入的数:\n");
typedata x = 0;
scanf("%d", &x);
seqlist_full(list);
list->node[list->size] = x;
list->size++;
printf("插入成功\n");
}
void seqlistpush_front(seqlist* list) {
printf("请输入你要插入的数:\n");
typedata x = 0;
scanf("%d", &x);
seqlist_full(list);
int i = 0;
for (i = list->size; i > 0; i--) {
list->node[i] = list->node[i - 1];
}
list->node[0] = x;
list->size++;
printf("插入成功\n");
}
void seqlistpop_back(seqlist* list) {
if (list->size > 0) {
list->node[list->size - 1] = 0;
list->size--;
printf("尾删完成\n");
}
else {
printf("内部暂时还没有内容\n");
}
}
void seqlistpop_front(seqlist* list) {
int i = 0;
for (i = 0; i < list->size; i++) {
list->node[i-1] = list->node[i];
}
list->node[list->size - 1] = 0;
list->size--;
printf("头删成功\n");
}
int len_cmp(seqlist* list,int n) {
if (list->size >= n && n>0) {
return 1;
}
return 0;
}
void seqlistpush_onin(seqlist* list,int n) {//n为要增加的位置;
seqlist_full(list);
printf("请输入你要插入的数\n");
typedata x = 0;
scanf("%d", &x);
if (len_cmp(list,n) == 1) {
printf("正在进行插入\n");
int i = 0;
for (i = list->size; i>n-1; i--) {
list->node[i] = list->node[i - 1];
}
list->node[n- 1] = x;
list->size++;
printf("插入完毕\n");
}
else {
printf("你要插入的位置存在问题\n");
}
}
void seqlistpop_onin(seqlist* list, int n) {//n为要增加的位置;
if (len_cmp(list, n) == 1) {
printf("正在进行内部删除\n");
int i = 0;
for (i = n; i <list->size; i++) {
list->node[i-1] = list->node[i];
}
list->node[list->size-1] =0;
list->size--;
printf("删除完毕\n");
}
else {
printf("你要删除的位置存在问题\n");
}
}
3.顺序表实现的主函数
#include "seqlist.h"
int main() {
int input = 0;
int n = 0;//内部删除或增加的位置
seqlist list1;
seqlist_init(&list1);
do {
menu();
printf("请你选择您要进行的操作\n");
scanf("%d", &input);
switch (input) {
case 1:
printf("正要进行尾插\n");
seqlistpush_back(&list1);
seqlist_print(&list1);
break;
case 2:
printf("正要进行头插\n");
seqlistpush_front(&list1);
seqlist_print(&list1);
break;
case 3:
printf("正在进行尾删\n");
seqlistpop_back(&list1);
seqlist_print(&list1);
break;
case 4:
printf("正在进行头删\n");
seqlistpop_front(&list1);
seqlist_print(&list1);
break;
case 5:
printf("正在进行内部插入\n");
printf("请输入你要插入的位置\n");
scanf("%d", &n);
seqlistpush_onin(&list1,n);
seqlist_print(&list1);
break;
case 6:
printf("正在进行内部删除\n");
printf("请输入你要删除的位置\n");
scanf("%d", &n);
seqlistpop_onin(&list1, n);
seqlist_print(&list1);
break;
case 0:
printf("正在退出程序\n");
break;
default:
printf("您的输入有误,请重新输入\n");
}
} while (input);
free(list1.node);
return 0;
}
4.代码的效果展示
最后这里跳出程序是一开始在主函数文件设break了(已解决)
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » 顺序表的实现
发表评论 取消回复