如下图,完成顺序表的初始化,尾插,打印函数后,在进行测试中发现CB中报错,而在VS中可以完美运行。百思不得其解,请各位大佬更正可能会出错的地方
CB中运行![](https://i-blog.csdnimg.cn/blog_migrate/0e759812af23d0d29950035110bdd669.png)
vs中运行
运行代码
test.c
#include "SeqList.h"
void TestSeqList1() {
SL sl;
SeqListInit(&sl);
SeqListPushBack(&sl, 1);
SeqListPushBack(&sl, 2);
SeqListPushBack(&sl, 3);
SeqListPushBack(&sl, 4);
SeqListPushBack(&sl, 5);
SeqListPrint(&sl);
}
int main() {
printf("aaa");
TestSeqList1();
return 0;
}
SeqList.c
#include "SeqList.h"
//!函数实现
void SeqListPrint(SL* ps) {
int i;
for (i = 0; i < ps->size; i++) {
printf("%d", ps->a[i]);
}
printf("\n");
}
//!初始化创建
void SeqListInit(SL* ps) {
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
//!尾插
void SeqListPushBack(SL* ps, SLDataType x) {
//!如果没有空间或者空间不足 则扩容
if (ps->size == ps->capacity) {
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
if (tmp == NULL) {
printf(("realloc fail\n"));
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->size] = x;
ps->size++;
}
//!尾删
void SeqListPopBack(SL* ps);
//!头插
void SeqListPushFront(SL* ps, SLDataType x);
//!头删
void SeqListPopFront(SL* ps);
SeqList.h
#include <stdio.h>
#include <stdlib.h>
typedef int SLDataType;
//!动态顺序表
typedef struct Seqlist {
SLDataType* a;//动态开辟的数
int size; //表示数组中储存了多少个数据
int capacity;//数组实际能存数据的空间容量是多大
}SL;
///打印
void SeqListPrint(SL* ps);
//!接口函数
//!创建初始化
void SeqListInit(SL* ps);
//!尾插
void SeqListPushBack(SL* ps, SLDataType x);
//!尾删
void SeqListPopBack(SL* ps);
//!头插
void SeqListPushFront(SL* ps, SLDataType x);
//!头删
void SeqListPopFront(SL* ps);
//....
求各位大神告知原因!!!!!
---------------------------------------------------------分割线---------------------------------------------------------------
【10.2更新已解决】
在浏览SeqList.文件中,意外发现Debug和Ralease没有选中,导致无法运行。
选中后,程序成功运行。