Implementation of Sequential List Operations

This problem requires implementing six core functions for an integer sequantial list that supports input, output, retrieval, search, insertion, and deletion operations. The sequential list structure manages integer data elements with fixed-size array storage.

Function Interface Definitions:

The sequential list structure is defined as:

typedef struct {
    DataType *data; // base address of storage space
    int size;       // current length
} SeqList;

Required fnuction implementations:

  • int RetrieveElement(SeqList list, int position, DataType &element) - Retrieevs element at specified position. Returns 0 for invalid position, 1 for success.
  • int SearchElement(SeqList list, double target) - Searches for element value. Returns position (1-indexed) if found, 0 otherwise.
  • int InsertElement(SeqList &list, int position, DataType element) - Inserts element at given position. Validates position and available space, shifts elements right, returns 1 for success.
  • int RemoveElement(SeqList &list, int position) - Removes element at given position. Shifts subsequent elements left, returns 1 for success.
  • void InputData(SeqList &list) - Reads list length and elements from input.
  • void DisplayData(SeqList list) - Outputs all elements in the list.

Sample Judge Program:

#include<iostream>
#include<string>
#include<iomanip>
#include <cstdlib>
using namespace std;

#define OVERFLOW -2
typedef int DataType;

#define MAX_CAPACITY 100

typedef struct {
    DataType *data;
    int size;
} SeqList;

void InitializeList(SeqList &list) {
    list.data = new DataType[MAX_CAPACITY];
    if (!list.data)
        exit(OVERFLOW);
    list.size = 0;
}

/* Implementation goes here */

int main() {
    SeqList container;
    int index = 0, temp_result, insert_pos, delete_pos;
    double search_value;
    DataType element;

    InitializeList(container);
    InputData(container);
    DisplayData(container);

    cin >> index;
    temp_result = RetrieveElement(container, index, element);
    if (temp_result != 0) {
        cout << "Element at position: " << element << endl;
    } else
        cout << "Retrieval failed! Position out of bounds\n";

    cin >> search_value;
    temp_result = SearchElement(container, search_value);
    if (temp_result != 0) {
        cout << "Found at position " << temp_result << endl;
    } else
        cout << "Search failed!\n";

    cin >> insert_pos;
    cin >> element;
    if (InsertElement(container, insert_pos, element))
        DisplayData(container);
    else
        cout << "Insertion failed\n";

    cin >> delete_pos;
    if (RemoveElement(container, delete_pos))
        DisplayData(container);
    else
        cout << "Deletion failed\n";

    return 0;
}

Sample Input:

5
5 4 3 2 1
2
2
2 6
6

Sample Output:

5 4 3 2 1 
Element at position: 4
Found at position 4
5 6 4 3 2 1 
5 6 4 3 2 

Implementation:

int RetrieveElement(SeqList list, int position, DataType &element) {
    if(position < 1 || position > list.size)
        return 0;
    element = list.data[position - 1];
    return 1;
}

int SearchElement(SeqList list, double target) {
    for(int idx = 0; idx < list.size; idx++)
        if(list.data[idx] == target) 
            return idx + 1;
    return 0;
}

int InsertElement(SeqList &list, int position, DataType element) {
    if(position < 1 || position > list.size + 1) 
        return 0;
    if(list.size == MAX_CAPACITY) 
        return 0;
    
    for(int idx = list.size - 1; idx >= position - 1; idx--) {
        list.data[idx + 1] = list.data[idx];
    }
    list.data[position - 1] = element;
    list.size++;
    return 1;
}

int RemoveElement(SeqList &list, int position) {
    if(position < 1 || position > list.size) 
        return 0;
    
    for(int idx = position; idx < list.size; idx++) {
        list.data[idx - 1] = list.data[idx];
    }
    list.size--;
    return 1;
}

void InputData(SeqList &list) {
    int count;
    cin >> count;
    for(int idx = 0; idx < count; idx++) {
        scanf("%d", &list.data[idx]);
        list.size++;
    }
}

void DisplayData(SeqList list) {
    for(int idx = 0; idx < list.size; idx++) {
        cout << list.data[idx] << " ";
    }
    cout << endl;
}

Tags: C++ data-structures sequential-list Arrays algorithms

Posted on Sat, 16 May 2026 23:32:52 +0000 by sriusa