Creation of a circular linked list, Data Structure & Algorithms

Assignment Help:

Program: Creation of a Circular linked list

ALGORITHM (Insertion of an element into a Circular Linked List)

Step 1        Begin

Step 2      if the list is empty or new element comes before the start (head) element, then insert the new element as start element.

Step 3          else, if the new element comes after the last element, then insert the new element at the end element and adjust the pointer of last element to the start element.

Step 4      else, insert the new element in the list by using the find function. find function returns  to the address of the found element to the insert_list function.

Step 5                End.

If new item is to be added after an existing element, then, call the find function recursively to trace the 'key' element. The new element is added before the 'key' element using above algorithm.

Figure demonstrated the Circular linked list with a new element that is to be inserted. Figure depicts a Circular linked list along with the new element added between first & second nodes of Figure.

804_Creation of a Circular linked list.png

Figure: A Circular Linked List  after  insertion of the new element between first and second nodes

(Dotted lines depict the links prior to insertion)

Program demonstrated the code for insertion of a node into a Circular linked list.

#include

#include

#define NULL 0 structlinked_list

{

int data;

structlinked_list *next;

};

typedefstructlinked_listclist;

clist *head, *s;

/* prototype of find and insert functions */

clist * find(clist *, int);

clist * insert_clist(clist *);

/*definition of insert_clist function */

clist * insert_clist(clist *start)   

{

clist *n, *n1;

int key, x;

printf("Insert new value for the new element");

scanf("%d", &x);

printf("eneter key element");

scanf("%d",&key);

if(start->data ==key)

}

else

{

 n=(clist *)malloc(sizeof(clist));

n->data=x;

n->next = start;

start=n;

n1 = find(start, key);

if(n1 == NULL)

printf("\n not found \n");

else

{

n=(clist*)malloc(sizeof(clist));

n->data=x;

n->next=n1->next;

n1->next=n;

}

}return(start);

 

/*description of find function */

clist * find(clist *start, int key)

{

if(start->next->data == key)

return(start);

if(start->next->next == NULL)

return(NULL);

else

find(start->next, key);

}

void main()

{

voidcreate_clist(clist *);

int count(clist *);

void traverse(clist *);

head=(clist *)malloc(sizeof(clist));

s=head;

create_clist(head);

printf(" \n traversing the created clist and the starting address is %u \n",traverse(head);

printf("\n number of elements in the clist   %d \n", count(head));

head=insert_clist(head);

printf("\n traversing the clist after insert_clist& starting address is %u \n",head);

traverse(head);

}

voidcreate_clist(clist *start)

 

 

{

printf("input element -1111 for coming out of loop\n");

scanf("%d", &start->data);

if(start->data == -1111)

start->next=s;

else

{

start->next=(clist*)malloc(sizeof(clist));

create_clist(start->next);

}                                                                                                                                                     }

void traverse(clist *start)

{

if(start->next!=s)

{

printf("data is %d \t next element address is %u\n", start->data, start- traverse(start->next);

}

}

if(start->next == s)

printf("data is %d \t next element address is %u\n",start->data, start->next);

int count(clist *start)

{

if(start->next == s)

return 0;

else

return(1+count(start->next));

}


Related Discussions:- Creation of a circular linked list

Importance of object-oriented over java, Importance of Object-Oriented over...

Importance of Object-Oriented over java Java is basically based on OOP notions of classes and objects. Java uses a formal OOP type system that should be obeyed at compile-t

Perform depth -first search, You are given two jugs, a 4-gallon one and a 3...

You are given two jugs, a 4-gallon one and a 3-gallon one. Neither has any measuring marker on it. There is a tap that can be used to fill the jugs with water. How can you get exac

Explain how the shuttle sort algorithm works, Question 1 Explain how th...

Question 1 Explain how the shuttle sort algorithm works by making use of the following list of integers:11, 4, 2, 8, 5, 33, 7, 3, 1, 6. Show all the steps. Question 2

Determine in brief about the boolean, Determine in brief about the Boolean ...

Determine in brief about the Boolean Carrier set of the Boolean ADT is the set {true, false}. Operations on these values are negation, conjunction, disjunction, conditional,

Example of binary search, Let us assume a file of 5 records that means n = ...

Let us assume a file of 5 records that means n = 5 And k is a sorted array of keys of those 5 records. Let key = 55, low = 0, high = 4 Iteration 1: mid = (0+4)/2 = 2

Accept a file and form a binary tree - huffman encoding, Huffman Encoding i...

Huffman Encoding is one of the very simple algorithms to compress data. Even though it is very old and simple , it is still widely used (eg : in few stages of JPEG, MPEG etc). In t

Implement stack using two queues, How To implement stack using two queues ,...

How To implement stack using two queues , analyze the running time of the stack operations ?

Explain linked list and its types, Data Structure and Algorithm 1. Exp...

Data Structure and Algorithm 1. Explain linked list and its types. How do you represent linked list in memory? 2. List and elucidate the types of binary tree. 3. Descr

Homework, Write a recursive function the computes the number of digits in a...

Write a recursive function the computes the number of digits in a positive integer n. For example if n = 6598, the function should return 4. Find a variant expression and a thresho

Determine the types of java, Determine the types of JAVA Java has two p...

Determine the types of JAVA Java has two parts... 1. Core language -- variables, arrays, objects o Java Virtual Machine (JVM) runs the core language o Core language is

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd