You are on page 1of 5

NFC Institute of Engineering&Fertilizer

Research Faisalabad

Department of Electrical Engineering


Fourth Semester
Course Title:
Data Structure & Algorithms (EE-232)
Topic:
Lab Assignments
Submitted To:
Dr. Salman Arain
Submitted By:
Hafeez Ali
Roll.No:
18-ELE-43
Reg.#:
2018-UET-NFC-FD-ELECT-43
Lab.No.9
LINKED LIST OPERATIONS
Task#1
Write a function to insert in the beginning of linked list.
C Function:
void insert(struct node* head, new_data)
{
struct node *newNode;

newNode = malloc(sizeof(struct node));

newNode->data = new_data;
newNode->next = head;

head = newNode;
}

Task#2
Write a function to insert in the middle of linked list.
C Function:
void insert(struct node* head, new_data)
{
struct node *newNode;

newNode = malloc(sizeof(struct node));

newNode->data = new_data;

struct node *temp = head;


for(int i=2; i < position; i++) {
if(temp->next != NULL) {
temp = temp->next;
}
}
newNode->next = temp->next;

temp->next = newNode;
}

Task#3
Write a function to insert in the last of linked list.
C Function:
void insert(struct node* head, int new_data)
{
struct node *newNode;

newNode = malloc(sizeof(struct node));

newNode->data = new_data;
newNode->next = NULL;

struct node *temp = head;

while(temp->next != NULL){
temp = temp->next;
}
temp->next = newNode
}
Task#4
Write a function to search an element in linked list.
C Function:
bool search(struct Node* head, int x)
{
struct Node* current = head;

while (current != NULL) {


if (current->key == x) {
return true;
}
current = current->next;
}
return false;
}

Task#5
Write a function to delete an element in linked list.
C Function:
del (struct node *before_del)
{
struct node *temp;

temp = before_del->next;

before_del->next = temp->next;

free(temp);
}
Task#6
Write a function to replace an element in linked list.
C Function:
node *modifyNode(struct node* Node, int old, int new)
{
while (Node != NULL){
if(Node->key == old) {
Node->key = new;
}
Node = Node->next;
}
}

Task#7
Write a function to display linked list.
C Function:
void printList(struct Node *node)
{
while (node != NULL) {
printf(" %d ", node->data);

node = node->next;
}
}

Conclusion:
In this lab, I came to know the implementation of different operations on
linked list such as Insert, Delete, Search & Replace. I concluded the concept of algorithms
used in the linked list.

You might also like