Unit 1: Linear structure implementation
Data Structures-I Laboratory notes · PTU syllabus (UGCC2507)
On this page
Unit summary
This lab implements the linear data structures from Data Structures-I in C: arrays and matrices, singly, doubly and circular linked lists, stacks, postfix evaluation, recursion, and simple and circular queues. Below are the core routines you must be able to write and explain in the viva.
After this unit you can
- Implement array insertion/deletion and matrix operations
- Implement insertion, deletion, creation and search in all three kinds of linked lists
- Implement stacks using arrays and linked lists and evaluate postfix expressions
- Implement simple and circular queues using arrays and linked lists
PTU syllabus topics
- Array insertion/deletion
- matrix addition/subtraction/multiplication
- singly linked list insertion/deletion (beginning, end, specified position)
- doubly linked list creation and search
- circular linked list creation and deletion
- stack PUSH/POP using array and linked list
- postfix expression evaluation using a stack
- recursive factorial
- simple and circular queue operations using array and linked list
- Empty structure
- Pop, dequeue or delete with nothing inside
- Single element
- Insert and delete the only node
- Full structure
- Push or enqueue when the array is full
- Boundary positions
- Insert at the beginning and end
- Invalid input
- Positions out of range
Topic 1
Arrays and matrices
- Insert at position pos: shift elements right from the end, then place the item and increase n.
- Delete at position pos: shift elements left from pos + 1 and decrease n.
- Matrix multiplication: possible only if columns of A = rows of B.
cfor (i = 0; i < m; i++)
for (j = 0; j < p; j++) {
c[i][j] = 0;
for (k = 0; k < n; k++)
c[i][j] += a[i][k] * b[k][j];
}Topic 2
Linked lists
cstruct node { int data; struct node *next; };
struct node *start = NULL;
void insert_begin(int x) {
struct node *t = malloc(sizeof(struct node));
t->data = x;
t->next = start;
start = t;
}
void delete_begin() {
if (start == NULL) { printf("Underflow"); return; }
struct node *t = start;
start = start->next;
free(t);
}- Doubly linked list nodes add a
prevpointer; when inserting, update both neighbours' links. - Circular list creation: the last node's
nextpoints tostart; traversal stops when the pointer comes back tostart.
Topic 3
Stacks and postfix evaluation
c#define MAX 50
int stack[MAX], top = -1;
void push(int x) { if (top == MAX - 1) printf("Overflow"); else stack[++top] = x; }
int pop() { if (top == -1) { printf("Underflow"); return -1; } return stack[top--]; }Postfix evaluation: for each symbol, if it is a digit push it; if it is an operator, pop b then a, compute a op b and push the result.
Topic 4
Recursion and queues
- Recursive factorial:
long fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); } - Circular queue with modulo arithmetic:
cint q[MAX], front = -1, rear = -1;
void enqueue(int x) {
if ((rear + 1) % MAX == front) { printf("Queue full"); return; }
if (front == -1) front = 0;
rear = (rear + 1) % MAX;
q[rear] = x;
}Segmentation fault
Dereferencing a NULL pointer
Check for NULL before using ->next
Memory leak
Deleted node never freed
Call free() on removed nodes
Wrong queue full
Simple queue after deletions
Use a circular queue
Key terms
- malloc
- Allocates memory for a node at run time
- free
- Releases memory of a deleted node
- Segmentation fault
- A crash from accessing invalid memory
- Modulo arithmetic
- Wrapping indexes in a circular queue with %
Quick revision
- Always check overflow and underflow.
- In linked lists, handle the empty-list and first-node cases.
- Pop b first, then a, when evaluating postfix (a op b).
- Circular queue full: (rear + 1) % MAX == front.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Why do we use malloc in linked lists?
- Q2.What happens if free() is not called?
- Q3.How do you detect the end of a circular linked list?
- Q4.Why is a circular queue better than a simple queue?
- Q5.In postfix evaluation, which operand is popped first?
Long-answer questions
- Q1.Write a menu-driven program for insertion and deletion in a singly linked list at the beginning, end and a given position.
- Q2.Implement a stack using a linked list and explain PUSH and POP.
- Q3.Write a program to evaluate a postfix expression using a stack.
- Q4.Implement a circular queue using an array and explain each operation.
Stuck on this unit?
Message SBS on WhatsApp for help with Data Structures-I Laboratory, or to ask about studying BCA at Synetic.
