Data Structure: Sequence List Implementation

📢 This article was translated by gemini-3-flash-preview

Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdio.h>
#include <stdlib.h> // malloc(), exit()

#define Size 5  // Define the initial allocation size of the sequence list

typedef struct Table
{
    int * head; // Pointer to the dynamic array
    int length; // Current length of the list
    int size;   // Allocated capacity of the list
}table;

// Initialization function
table initTable()
{
    table t;

    t.head = (int*)malloc(Size * sizeof(int));  // Allocate memory for the list

    if (!t.head)    // Exit if allocation fails
    {
        printf("Initialization failed");
        exit(0);
    }

    t.length = 0;   // Initialize length to 0
    t.size = Size;  // Set initial capacity

    return t;
}

// Insertion function: elem is the value to insert, add is the position (1-based index)
table addTable(table t, int elem, int add)
{
    int i;

    // Check if the position is valid
    if (add > t.length + 1 || add < 1) 
    {
        printf("Invalid insertion position");
        return t;
    }
    // If capacity is full, reallocate memory
    if (t.length == t.size) 
    {
        t.head = (int *)realloc(t.head, (t.size + 1) * sizeof(int));
        if (!t.head) 
        {
            printf("Memory reallocation failed");
            return t;
        }
        t.size += 1;
    }
    // Shift elements starting from the insertion point to the right
    for (i = t.length - 1; i >= add - 1; i--) 
    {
        t.head[i + 1] = t.head[i];
    }

    // Insert the element at the specified position
    t.head[add - 1] = elem;
    // Increment the length
    t.length++;
    
    return t;
}

// Deletion function
table delTable(table t, int add) 
{
    int i;

    if (add > t.length || add < 1) 
    {
        printf("Invalid deletion position");
        exit(0);
    }
    // Shift elements after the deleted element to the left
    for (i = add; i < t.length; i++) 
    {
        t.head[i - 1] = t.head[i];
    }

    t.length--;

    return t;
}

// Search function: returns the 1-based position of the given element value
int selectTable(table t, int elem) 
{
    int i;
    
    for (i = 0; i < t.length; i++) 
    {
        if (t.head[i] == elem) 
        {
            return i + 1;
        }
    }

    return -1;
}

// Update function: replace elem with newElem
table amendTable(table t, int elem, int newElem) 
{
    int add = selectTable(t, elem);

    t.head[add - 1] = newElem;

    return t;
}

// Print the elements of the sequence list
void displayTable(table t) 
{
    int i;

    for (i = 0; i < t.length; i++) 
    {
        printf("%d ", t.head[i]);
    }
    printf("\n");
}

int main() {
    int i, add;

    table t1 = initTable();

    // Fill the sequence list
    for (i = 1; i <= Size; i++) 
    {
        t1.head[i - 1] = i;
        t1.length++;
    }

    printf("Original List:\n");
    displayTable(t1);

    printf("Deleting element at position 1:\n");
    t1 = delTable(t1, 1);
    displayTable(t1);

    printf("Inserting 5 at position 2:\n");
    t1 = addTable(t1, 5, 2);
    displayTable(t1);

    printf("Finding position of element 3:\n");
    add = selectTable(t1, 3);
    printf("%d\n", add);
    
    printf("Changing 3 to 6:\n");
    t1 = amendTable(t1, 3, 6);
    displayTable(t1);

    return 0;
}

Results

Program output:

Original List:
1 2 3 4 5
Deleting element at position 1:
2 3 4 5
Inserting 5 at position 2:
2 5 3 4 5
Finding position of element 3:
3
Changing 3 to 6:
2 5 6 4 5

This post is licensed under CC BY-NC-SA 4.0 by the author.