In C, we have to implement the Abstract Data Types (ADTs) ourselves. Implementations of these ADTs would be very limited if we had to base the underlying memory off of arrays in C. This is because C arrays, as we know, are of fixed length.
The solution to this issue is to use dynamic memory allocation in order to request chunks of memory during runtime as needed. We ask the computer for these chunks (called nodes) and connect/link them using pointers. This is the idea of a linked list.
Modelling a bank line using a linked list
Here is how we can use a linked list to model a queue in a bank line where:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Person Person;
typedef struct BankLine BankLine;
// struct to represent person
struct Person {
char name[100];
int age;
Person *next;
};
// struct to represent bank line
struct BankLine {
char name[50];
Person *head;
Person *tail;
};
// function to create new bank line in memory
BankLine create_bank_line(char *name) {
BankLine new_line;
strcpy(new_line.name, name);
new_line.head = NULL;
new_line.tail = NULL;
return new_line;
}
// function to add a new person to add to the bank line
Person *create_person(char *name, int age) {
// allocate memory for Person struct
Person *new_person = malloc(sizeof(Person));
// set person name
strcpy(new_person->name, name);
// set person age
new_person->age = age;
// initialize the reference to the next person as NULL
new_person->next = NULL;
// tell user about this change
printf("😁 Someone appeared: %s (%d years old)\n", new_person->name,
new_person->age);
// return reference to new person
return new_person;
}
void add_person_to_line(char *name, int age, BankLine *bank_line) {
// get reference to new person
Person *new_person = create_person(name, age);
// if line has no person, set the head as the new person
if (bank_line->head == NULL) {
bank_line->head = new_person;
printf(" 👆 %s is the first person in the line\n", new_person->name);
}
// if line has no tail, set the tail as the new person
if (bank_line->tail == NULL) {
bank_line->tail = new_person;
printf(" 👇 %s is the last person in the line\n", new_person->name);
} else {
// set the current tail's next as the new person
bank_line->tail->next = new_person;
// set the line's tail as the new person
bank_line->tail = new_person;
printf(" 👇 %s went to the back of the line\n", new_person->name);
}
}
// function to print the information of a person
void info(Person *person) {
printf(" 😃 \"My name is %s. I am %d years old. ", person->name, person->age);
if (person->next != NULL) {
printf("The person after me is %s\"\n", person->next->name);
} else {
printf("No one comes after me in the line\"\n");
}
}
// function to print the information of all persons in the line
void bank_line_info(BankLine *bank_line) {
printf("👀 This bank line is %s\n", bank_line->name);
printf("👀 The person at the head of the line is %s (age %d years)\n",
bank_line->head->name, bank_line->head->age);
// set current person as the head of the line
Person *current_person = bank_line->head;
// as long as the current person is not NULL, print the information
// of the current person and increment the current person to the next person
while (current_person != NULL) {
info(current_person);
current_person = current_person->next;
}
}
// function to clean up bank line from memory
void free_bank_line(BankLine *bank_line) {
printf("👀 Freeing the line \"%s\"\n", bank_line->name);
Person *current_person = bank_line->head;
while (current_person != NULL) {
Person *next = current_person->next;
free(current_person);
current_person = next;
}
}
int main() {
BankLine rb_line = create_bank_line("Reepooblic Bank Line");
// add some persons to the line
add_person_to_line("David", 17, &rb_line);
add_person_to_line("Joash", 26, &rb_line);
add_person_to_line("Athaliah", 18, &rb_line);
add_person_to_line("Anita", 36, &rb_line);
// print info on the line
bank_line_info(&rb_line);
// clear line from memory
free_bank_line(&rb_line);
return 0;
}
bank-line.c Copy
Assignment
Create a modified version of the code above with the following variations:
- Use Node instead of Person
- Use linked list instead of bank line
- Each node should store an integer called value instead of the member variables name and age
- Add a function that can swap two nodes in the linked list by changing their next member variables
Created using natural intelligence