You are on page 1of 9

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.10
CIRCULARLY LINK LIST OPERATIONS
Task#1
Write a function to create a circularly linked list.
Code:
void createList(int n)
{
int i, data;
struct node *prevNode, *newNode;

if(n >= 1) {
head = (struct node *)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");


scanf("%d", &data);

head->data = data;
head->next = NULL;

prevNode = head;

for(i=2; i<=n; i++) {


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

printf("Enter data of %d node: ", i);


scanf("%d", &data);

newNode->data = data;
newNode->next = NULL;
prevNode->next = newNode;
prevNode = newNode;
}
prevNode->next = head;

printf("\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n");


}
}

Task#2
Write a function to search an element in circularly linked list.
C Function:
int search(struct node *head, int key)
{
int index = 0;
struct node *current = head;

do {
if (current == NULL) {
return;
}
if (current->data == key) {
return index;
}
current = current->next;

index++;
}
while (current != head);

return -1;
}
Task#3
Write a function to insert an element in beginning of circularly linked list.
C Function:
void insertAtBeginning(int data)
{
struct node *newNode, *current;

if(head == NULL) {
printf("List is empty.\n");
}
else {
newNode = (struct node *)malloc(sizeof(struct node));

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

current = head;

while(current->next != head) {
current = current->next;
}
current->next = newNode;

head = newNode;

printf("NODE INSERTED SUCCESSFULLY\n");


}
}
Task#4
Write a function to insert an element in last of circularly linked list.

C Function:

void insertAtLast(int data, int n)


{
struct node *newNode, *current;
int i;

if(head == NULL) {
printf("List is empty.\n");
}
else {
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;

current = head;

for(i=2; i<=n-1; i++) {


current = current->next;
}

newNode->next = current->next;

current->next = newNode;

printf("NODE INSERTED SUCCESSFULLY.\n");


}
Task#5
Write a function to insert an element in mid of circularly linked list.
C Function:
void insertAtLast(int data, int n)
{
struct node *newNode, *current;
int i, mid;

mid = n/2;

if(head == NULL) {
printf("List is empty.\n");
}
else {
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;

current = head;

for(i=2; i<=mid; i++) {


current = current->next;
}

newNode->next = current->next;

current->next = newNode;

printf("NODE INSERTED SUCCESSFULLY.\n");


}

Task#6
Write a function to delete an element in circularly linked list.
C Function:
void deleteAll(struct node **head, int key)

int i, count;

struct node *prev, *cur;

if (*head == NULL) {

printf("List is empty.\n");

return;

count = 0;

cur = *head;

prev = cur;

while (prev->next != *head) {

prev = prev->next;

count++;

i = 0;
while (i <= count) {

if (cur->data == key) {

if (cur->next != cur){

prev->next = cur->next;

else {

prev->next = NULL;

free(cur);

if (prev != NULL) {

cur = prev->next;

else {

cur = NULL;

else {

prev = cur;

cur = cur->next;

i++;

}
Task#7
Write a function to display circularly linked list.
C Function:
void displayList()
{
struct node *current;
int n = 1;

if(head == NULL) {
printf("List is empty.\n");
}
else {
current = head;
printf("DATA IN THE LIST:\n");

do {
printf("Data %d = %d\n", n, current->data);

current = current->next;
n++;
}
while(current != head);
}
}

Conclusion:
In this lab, I came to know the basic steps needed to use the linked
list as Circular Linked List. I concluded the working principle of the circular
linked list and how to use the different algorithmic operation on the circular
linked list. I understood the step by step implementation of the function
programs.

You might also like