0% found this document useful (0 votes)
4 views7 pages

Linked List Circular Operation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Linked List Circular Operation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Write a program that uses functions to perform the

following operations on circular


linked List i) Creation ii) Insertion iii) Deletion iv)
Traversal.

// Online C compiler to run C program online

#include <stdio.h>

#include <stdlib.h>

struct node{

int data;

struct node *nextAddr;

struct node *prevAddr;

};

//helper function

int size_list(struct node **head)

struct node *cursor = *head;

int size = 0;

if(cursor -> nextAddr == NULL && cursor -> prevAddr == NULL)

size++;

return size;

while (cursor -> nextAddr != *head)

{
size++;

cursor = cursor -> nextAddr;

size++;

return size;

void create (struct node **headPointer , int data)

struct node *node;

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

node -> data = data;

node -> nextAddr = NULL;

node -> prevAddr = NULL;

//nothing in linked list

if (*headPointer == NULL)

*headPointer = node;

return;

//if list is not empty insert at last

struct node *cursor;

cursor = *headPointer;

if(cursor -> nextAddr == NULL && cursor -> prevAddr == NULL)

node -> prevAddr = cursor;

node -> nextAddr = cursor;


cursor -> nextAddr = node;

cursor -> prevAddr = node;

return;

cursor -> prevAddr -> nextAddr = node;

node -> prevAddr = cursor -> prevAddr;

cursor -> prevAddr = node;

node -> nextAddr = cursor;

//insertion

void insert (struct node **headPointer , int index , int data)

struct node *node;

struct node *cursor;

cursor = *headPointer;

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

node -> data = data;

node -> nextAddr = NULL;

node -> prevAddr = NULL;

int size = size_list(headPointer) - 1;

if (index > size)

printf("crossed index");

return;

}
if (index == size && index != 0) //last element

node -> prevAddr = cursor -> prevAddr;

cursor -> prevAddr -> nextAddr = node;

node -> nextAddr = cursor;

cursor -> prevAddr = node;

return;

for (int i = 0; i < index; i++)

cursor = cursor -> nextAddr;

//put at specified index and push the other one one step below

cursor -> prevAddr -> nextAddr = node;

node -> prevAddr = cursor -> prevAddr;

node -> nextAddr = cursor;

cursor -> prevAddr = node;

//deletion

void delete_list(struct node **headPointer , int index)

struct node *node;

struct node *cursor;

struct node *to_null;

cursor = *headPointer;
int size = size_list(headPointer) - 1;

if(index > size)

printf("Crosssed Index Value");

return;

if(index == size && index != 0)

to_null = (*headPointer) -> prevAddr;

cursor -> prevAddr = cursor -> prevAddr -> prevAddr;

cursor -> prevAddr -> nextAddr = cursor;

free(to_null);

return;

for (int i = 0; i < index; i++)

cursor = cursor -> nextAddr;

cursor -> prevAddr -> nextAddr = cursor -> nextAddr;

cursor -> nextAddr -> prevAddr = cursor -> prevAddr;

free(cursor);

//traversal

void printLinkList(struct node **headPointer)

{
struct node *cursor = *headPointer;

while (cursor -> nextAddr != *headPointer)

printf("%d " , cursor -> data);

cursor = cursor -> nextAddr;

cursor -> nextAddr;

printf("%d\n" , cursor -> data);

int main() {

struct node *head;

create(&head , 10);

create(&head , 20);

create(&head , 50);

create(&head , 30);

create(&head , 1000);

create(&head , 59580);

create(&head , 3230);

printf("List after creation\n");

printLinkList(&head);

insert(&head , 3 , 1);

printf("After Insertion \n");

printLinkList(&head);
delete_list(&head , 3);

printf("After Deletion \n");

printLinkList(&head);

return 0;

You might also like