You are on page 1of 10

Bachelor in Information Technology with Honours

January 2021

CBDS2103

DATA STRUCTURES

MATRICULATION NO : 970426146216001
IDENTITY CARD NO. : 970426146216
TELEPHONE NO. : 017-2018248
E-MAIL : yamunah97@oum.edu.my
LEARNING CENTRE : OUM Bangi Learning Centre
TABLE CONTENT Page

Introduction .............................................................................. 2
Part I coding ............................................................................... 3-7
Part II Online class participation ............................................................................... 8-9

1
Introdution:

A common data structure in computer science and programming is the linked list. It is
made up of a series of nodes, each of which has a data element and a link to the node
after it in the sequence. The head of the list is the first node, while the last node usually
includes a reference to null, indicating that the list has ended.

Unlike arrays, linked lists do not store elements in contiguous memory locations. Instead,
each node holds the data and a pointer to the next node, allowing for dynamic allocation
of memory. This flexibility makes linked lists useful in situations where the size of the
data is unknown or can change dynamically.

In this assignment I would like to develop the linked list node structure and the employee
structure are first defined by the software. Next, functions are declared for establishing an
empty linked list, adding a new node to the list's beginning, checking to see if the list is
empty, and traversing the linked list.

The createLinkedList() function in the main() function is used to build an empty linked
list. The insertNode() function is used to initialize and add employee records to the linked
list. The linked list is checked to see if it is empty using the is Empty() method. Finally,
the linked list of employee records is traversed and printed using the traverseLinkedList()
function.

2
Coding:

// Online C compiler to run C program online

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// Employee structure

struct employee {

int id;

char name[25];

float salary;

};

// Linked list node

struct Node {

struct employee emp;

struct Node* next;

};

// Function to create an empty linked list

struct Node* createLinkedList() {

return NULL;

3
// Function to insert a new node at the beginning of the linked list

struct Node* insertNode(struct Node* head, struct employee emp) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->emp = emp;

newNode->next = head;

head = newNode;

return head;

// Function to test whether a linked list is empty or not

int isEmpty(struct Node* head) {

return head == NULL;

// Function to traverse and print the linked list

void traverseLinkedList(struct Node* head) {

struct Node* currentNode = head;

while (currentNode != NULL) {

printf("ID: %d, Name: %s, Salary: %.2f\n", currentNode->emp.id, currentNode-

>emp.name, currentNode->emp.salary);

currentNode = currentNode->next;

4
}

int main() {

struct Node* head = createLinkedList();

// Inserting employee records

struct employee records[5];

records[0].id = 1;

strcpy(records[0].name, "Ahmad");

records[0].salary = 2000.00;

records[1].id = 2;

strcpy(records[1].name, "Siti");

records[1].salary = 2500.00;

records[2].id = 3;

strcpy(records[2].name, "Raju");

records[2].salary = 3000.00;

records[3].id = 4;

strcpy(records[3].name, "Rani");

records[3].salary = 3500.00;

5
records[4].id = 5;

strcpy(records[4].name, "Patrick");

records[4].salary = 4000.00;

// Inserting employee records into the linked list

for (int i = 0; i < 5; i++) {

head = insertNode(head, records[i]);

// Testing whether the linked list is empty

if (isEmpty(head)) {

printf("Linked list is empty.\n");

} else {

printf("Linked list is not empty.\n");

// Traversing and printing the linked list

printf("Employee Records:\n");

traverseLinkedList(head);

return 0;

6
The screen shot of the Output:

7
PART II ONLINE CLASS PARTSIPATION:

8
9

You might also like