Professional Documents
Culture Documents
Research Faisalabad
if(n >= 1) {
head = (struct node *)malloc(sizeof(struct node));
head->data = data;
head->next = NULL;
prevNode = head;
newNode->data = data;
newNode->next = NULL;
prevNode->next = newNode;
prevNode = newNode;
}
prevNode->next = head;
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;
C Function:
if(head == NULL) {
printf("List is empty.\n");
}
else {
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
current = head;
newNode->next = current->next;
current->next = newNode;
mid = n/2;
if(head == NULL) {
printf("List is empty.\n");
}
else {
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
current = head;
newNode->next = current->next;
current->next = newNode;
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;
if (*head == NULL) {
printf("List is empty.\n");
return;
count = 0;
cur = *head;
prev = cur;
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.