顺序表实现增删查改Segmentation fault (core dumped)

顺序表实现增删查改

代码

#include <iostream>
#include <cstdio>
using namespace std;

typedef int ElemType; // Assume the elements of linear list to be of type integer
#define LIST_SIZE 1024 // Assume that the maximum length of a linear list is 1024
#define FALSE 0
#define TRUE 1
typedef struct {
    ElemType data[LIST_SIZE];
    int last; // Specify the location of the last node
} SeqList;


int Insert_SeqList(SeqList * LPtr, ElemType x, int k); // ~
int Delete_SeqList(SeqList * LPtr, int k);
int Locate_SeqList( SeqList * LPtr, ElemType key );
int Get_SeqList( SeqList * LPtr, int i,  ElemType * e);
void Print_SeqList(SeqList * LPtr);
void Input_SeqList(SeqList * LPtr, int n); // n elements


int main() {
    int n; // n elements
    cin >> n;
    SeqList int_seq;
    SeqList * LPtr =  &int_seq; // A pointer to structure SequenList
    Input_SeqList( LPtr, n );
    //int Insert_SeqList(SeqList * LPtr, ElemType x, int k);
    //Insert_SeqList(LPtr, 100, 3);
    Print_SeqList( LPtr );
    Insert_SeqList(LPtr, 100, 3);
    Print_SeqList( LPtr );
    // Delete_SeqList(LPtr, 3);
    // Print_SeqList(LPtr);
    cout << Locate_SeqList(LPtr, 100) << endl;
    int e;
    if ( Get_SeqList(LPtr, 3, &e) ) cout << e << endl;

    return 0;
}

int Insert_SeqList(SeqList * LPtr, ElemType x, int k) { // k, the index of the position waiting for action of inserting
    int j;
    if (LPtr->last >= LIST_SIZE - 1) return FALSE; // overflow
    else if ( k < 0 || k >= (LPtr->last + 1)) return FALSE; // Illegal location
    else {
        for ( j = LPtr->last; j >= k; j-- ) { // Start with the last element of the order list
            LPtr->data[j + 1] = LPtr->data[j]; // Move last - k elements back
        }
        LPtr->data[k] = x; // Put x in the postition k
        LPtr->last = LPtr->last + 1; // Motifies the value of last  of the last node pointer
    }
    return TRUE;
}

int Delete_SeqList(SeqList * LPtr, int k) {
    if ( ( k > 0 && k <= LPtr->last ) && ( LPtr->last != -1 ) ) {
        for ( int j = k; j <= LPtr->last - 1; j++ ) { // LPtr->last - 1 !!!
            LPtr->data[j] = LPtr->data[j + 1];
        }
        LPtr->last--;
        return TRUE; // Successfully deleted
    }
    return FALSE; // Abnormal findings
}

int Locate_SeqList( SeqList * LPtr, ElemType key ) {
    for ( int i = 0; i <= LPtr->last; i++ ) {
        if ( LPtr->data[i] == key ) return i;
    }
    return -1;
}

int Get_SeqList( SeqList * LPtr, int i,  ElemType * e) { // Get the value stored in the index i of data
    if ( i < 0 || i > LPtr->last ) return FALSE;
    if ( LPtr->last < 0 ) return FALSE;
    *e = LPtr->data[i];
    return TRUE;
}

void Print_SeqList(SeqList * LPtr) {
    int i;
    int j = 0;
    for ( i = 0; i <= LPtr->last - 1; i++ ) {
        j++;
        cout << LPtr->data[i] << '\t';
        if ( !( j % 10 ) ) cout << '\n';
    }
    cout << LPtr->data[i];
    if ( !( j % 10 ) ) 
        cout << '\n';
    cout << '\n';
}

void Input_SeqList(SeqList * LPtr, int n) {
    for ( int i = 0; i < n; i++ ) {
        cin >> LPtr->data[i];
    }
    LPtr->last = n - 1;
}

小结

Segmentation fault (core dumped)
该错误应该是由指针没有初始化,未指向具体地址造成的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值