||=== 构建: Debug 在 链表 中 (编译器: GNU GCC Compiler) ===|
C:\Users\张成龙\Desktop\code\链表\main.cpp||In function 'int main()':|
C:\Users\张成龙\Desktop\code\链表\main.cpp|137|error: jump to case label [-fpermissive]|
C:\Users\张成龙\Desktop\code\链表\main.cpp|131|error:   crosses initialization of 'int k'|
||=== 构建 失败: 2 error(s), 0 warning(s) (0 分, 0 秒) ===|
 

出现这样的编译错误原因是在case中定义变量,编译器认为这会跳过变量的初始化。解决办法将case中定义的变量用{}括起来,

或者 定义在switch外。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAXN = 1000 + 7;

typedef struct {
    int num;        // 编号
    char name[50];  // 书名
    double price;   // 价格
} BOOK;
typedef struct {
    BOOK *elem;     // 定义BOOK 类型指针
    int length;
} SqList;
bool operator == (BOOK e1, BOOK e2)        // 重载等于等于号,判断两个结构体变量是否相等
{
    if(e1.num == e2.num && strcmp(e1.name, e2.name) == 0 && e1.price == e2.price)
        return true;
    return false;
}

bool InitList(SqList &L)    // 返回值是该函数的执行状态,下同
{
    L.elem = new BOOK[MAXN];    // 动态分配一个数组
    if( !L.elem)
       exit(0);
    L.length = 0;   // 初始长度为0
    return true;
}
bool GetElem(SqList &L, int i, BOOK &e)
{
    if(i < 1 || i > L.length)	// 查询位置不合法
        return false;
    e = L.elem[i-1];
    return true;
}
int FindElem(SqList &L, BOOK e)     // 查询到返回其下标,否则为-1
{
    for(int i=0; i<L.length; i++)
        //if(L.elem[i].num == e.num  && L.elem[i].name == e.name && L.elem[i].price == e.price)
        if(L.elem[i] == e)	// 用到了== 的重载,否则需要上一行的写法
            return i;
    return -1;	// 没找到返回-1
}
bool InsertList(SqList &L, int i, BOOK e)
{
    if(i < 1 || i > L.length + 1)   // 插入位置不合法
        return false;
    if(L.length == MAXN)    // 元素已满
        return false;
    for(int j = L.length - 1; j >= i-1; j--)    // 第i个元素及其以后的元素全部后移一位
        L.elem[j+1] = L.elem[j];
    L.elem[i-1] = e;       // 插入
    L.length ++;           // 插入元素后长度加一
    return true;
}
bool DeleteList(SqList &L, int i)	// 删除第i个,实际存储为 i-1,因为从0开始
{
    if(i < 1 || i > L.length)
        return false;
    for(int j=i-1; j<L.length-1; j++)
        L.elem[j] = L.elem[j+1];
    L.length --;    // 删除元素后长度减一
    return true;
}
bool PrintList(const SqList &L)
{
    for(int i=0; i<L.length; i++)
    {
        cout << L.elem[i].num << " ";
        cout << L.elem[i].name << " ";
        cout << L.elem[i].price << endl;
    }
    return true;
}

int main()
{
    SqList L;
    InitList(L);
    cout << "input n: " << endl;
    int n;
    BOOK e;
    cin >> n;
    cout << "输入初始化的n个元素" << endl;
    for(int i=0; i<n; i++)
    {
        cin >> e.num;
        cin >> e.name;
        cin >> e.price;
        if( !InsertList(L, i+1, e))
        {
            printf("Insert Error");
            break;
        }
    }

	int choice;
	printf(" 1: Insert\n 2: Delete\n 3: Get\n 4: Find\n 5: Print\n");
	while(cin >> choice)
	{
		int index;
		int k;
		switch(choice)
		{
			case 1:
				printf("input e\n");
				cin >> e.num;
				cin >> e.name;
				cin >> e.price;
				printf("input Insert index: ");
				cin >> index;
				InsertList(L, index, e);
				break;
			case 2:
				printf("input Delete index: ");
				cin >> index;
				DeleteList(L, index);
				break;
			case 3:
				printf("input Get index: ");
				cin >> index;
				GetElem(L, index, e);
				printf("%d %s %.2lf\n", e.num, e.name, e.price);
				break;
			case 4:
				printf("input e\n");
				cin >> e.num;
				cin >> e.name;
				cin >> e.price;
				k = FindElem(L, e);
				if(k != -1)
					printf("%d %s %.2lf\n", L.elem[k].num, L.elem[k].name, L.elem[k].price);
				else
					printf("Find Error\n");
				break;
			case 5:
				PrintList(L);
				break;
		}
	}


    return 0;
}