0% found this document useful (0 votes)
23 views15 pages

Create and Display Circular Singly Linked List

The document provides a C implementation of a Circular Singly Linked List, including functions for creating the list, inserting and deleting nodes at various positions, displaying the list, counting nodes, searching for a key, and sorting the list. It includes a menu-driven interface for user interaction to perform these operations. Each function is detailed with its algorithmic steps for clarity.

Uploaded by

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

Create and Display Circular Singly Linked List

The document provides a C implementation of a Circular Singly Linked List, including functions for creating the list, inserting and deleting nodes at various positions, displaying the list, counting nodes, searching for a key, and sorting the list. It includes a menu-driven interface for user interaction to perform these operations. Each function is detailed with its algorithmic steps for clarity.

Uploaded by

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

####################### Circular Singly linked List###########################

#include <stdio.h>

#include <stdlib.h>

// Node structure

typedef struct node {

int data;

struct node *next;

} Node;

Node *last = NULL; // pointer to last node

// Function prototypes

void create(int n);

void finsert(int data);

void linsert(int data);

void ainsert(int pos, int data);

void fdelete();

void ldelete();

void adelete(int pos);

void display();

int count();

void search(int key);

void sort();

int main() {

int choice, n, data, pos, key;

while (1) {

printf("\n=== Circular Singly Linked List Menu ===\n");

printf("1. Create List\n");


printf("2. Insert at First\n");

printf("3. Insert at Last\n");

printf("4. Insert at Position\n");

printf("5. Delete First\n");

printf("6. Delete Last\n");

printf("7. Delete at Position\n");

printf("8. Display\n");

printf("9. Count Nodes\n");

printf("10. Search\n");

printf("11. Sort\n");

printf("12. Exit\n");

printf("Enter choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter number of nodes: ");

scanf("%d", &n);

create(n);

break;

case 2:

printf("Enter data: ");

scanf("%d", &data);

finsert(data);

break;

case 3:

printf("Enter data: ");

scanf("%d", &data);
linsert(data);

break;

case 4:

printf("Enter position: ");

scanf("%d", &pos);

printf("Enter data: ");

scanf("%d", &data);

ainsert(pos, data);

break;

case 5:

fdelete();

break;

case 6:

ldelete();

break;

case 7:

printf("Enter position: ");

scanf("%d", &pos);

adelete(pos);

break;

case 8:

display();

break;

case 9:

printf("Total nodes = %d\n", count());

break;

case 10:

printf("Enter key to search: ");


scanf("%d", &key);

search(key);

break;

case 11:

sort();

break;

case 12:

exit(0);

default:

printf("Invalid choice!\n");

return 0;

// Create list

void create(int n) {

int data, i;

last = NULL;

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

printf("Enter data for node %d: ", i+1);

scanf("%d", &data);

linsert(data);

// Insert at first

void finsert(int data) {


Node *newNode = (Node*)malloc(sizeof(Node));

newNode->data = data;

if (last == NULL) {

last = newNode;

last->next = last;

} else {

newNode->next = last->next;

last->next = newNode;

// Insert at last

void linsert(int data) {

Node *newNode = (Node*)malloc(sizeof(Node));

newNode->data = data;

if (last == NULL) {

last = newNode;

last->next = last;

} else {

newNode->next = last->next;

last->next = newNode;

last = newNode;

// Insert at position
void ainsert(int pos, int data) {

if (pos < 1) {

printf("Invalid position!\n");

return;

if (pos == 1) {

finsert(data);

return;

Node *newNode = (Node*)malloc(sizeof(Node));

newNode->data = data;

Node *temp = last->next;

int i;

for (i = 1; i < pos-1 && temp != last; i++) {

temp = temp->next;

newNode->next = temp->next;

temp->next = newNode;

if (temp == last) {

last = newNode;

}
// Delete first

void fdelete() {

if (last == NULL) {

printf("List empty!\n");

return;

Node *temp = last->next;

if (last == last->next) {

last = NULL;

} else {

last->next = temp->next;

free(temp);

printf("First node deleted.\n");

// Delete last

void ldelete() {

if (last == NULL) {

printf("List empty!\n");

return;

Node *temp = last->next;

if (last == last->next) {

free(last);
last = NULL;

} else {

while (temp->next != last) {

temp = temp->next;

temp->next = last->next;

free(last);

last = temp;

printf("Last node deleted.\n");

// Delete at position

void adelete(int pos) {

if (last == NULL) {

printf("List empty!\n");

return;

Node *temp = last->next, *prev;

int i;

if (pos == 1) {

fdelete();

return;

for (i = 1; i < pos && temp != last; i++) {


prev = temp;

temp = temp->next;

if (i != pos) {

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

return;

prev->next = temp->next;

if (temp == last) {

last = prev;

free(temp);

printf("Node at position %d deleted.\n", pos);

// Display list

void display() {

if (last == NULL) {

printf("List empty!\n");

return;

Node *temp = last->next;

printf("List: ");

do {

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


temp = temp->next;

} while (temp != last->next);

printf("(circular)\n");

// Count nodes

int count() {

if (last == NULL) return 0;

int cnt = 0;

Node *temp = last->next;

do {

cnt++;

temp = temp->next;

} while (temp != last->next);

return cnt;

// Search key

void search(int key) {

if (last == NULL) {

printf("List empty!\n");

return;

Node *temp = last->next;

int pos = 1;

do {
if (temp->data == key) {

printf("Key %d found at position %d.\n", key, pos);

return;

temp = temp->next;

pos++;

} while (temp != last->next);

printf("Key %d not found.\n", key);

// Sort list

void sort() {

if (last == NULL || last->next == last) {

printf("List too small to sort.\n");

return;

Node *i, *j;

int temp;

for (i = last->next; i->next != last->next; i = i->next) {

for (j = i->next; j != last->next; j = j->next) {

if (i->data > j->data) {

temp = i->data;

i->data = j->data;

j->data = temp;

}
}

printf("List sorted.\n");

Algorithms for Circular Singly Linked List

1. Create List (create())

1. Start

2. Input number of nodes n

3. Repeat for i = 1 to n:

o Allocate memory for new node

o Input data

o If list is empty → set last = newNode and last->next = last

o Else → newNode->next = last->next, last->next = newNode, last = newNode

4. Stop

2. Insert at First (finsert())

1. Start

2. Allocate memory for new node

3. Input data

4. If list empty → set last = newNode and last->next = last

5. Else → newNode->next = last->next, last->next = newNode

6. Stop

3. Insert at Last (linsert())

1. Start

2. Allocate memory for new node

3. Input data

4. If list empty → set last = newNode and last->next = last

5. Else → newNode->next = last->next, last->next = newNode, last = newNode

6. Stop

4. Insert at Any Position (ainsert())


1. Start

2. Input pos and data

3. If list empty and pos != 1 → print error

4. Allocate memory for new node

5. If pos == 1 → perform same as finsert()

6. Else → traverse till pos-1

o If reached last and still not at position → error

o Else adjust:

 newNode->next = temp->next

 temp->next = newNode

 If temp == last → update last = newNode

7. Stop

5. Delete from First (fdelete())

1. Start

2. If list empty → print message and stop

3. If only one node (last->next == last) → free node and set last = NULL

4. Else → set temp = last->next, last->next = temp->next, free temp

5. Stop

6. Delete from Last (ldelete())

1. Start

2. If list empty → print message and stop

3. If only one node → free node and set last = NULL

4. Else → traverse till node before last (temp)

o Set temp->next = last->next

o Free last node

o Update last = temp

5. Stop

7. Delete from Any Position (adelete())


1. Start

2. If list empty → print message and stop

3. Input pos

4. If only one node and pos == 1 → delete and set last = NULL

5. Else if pos == 1 → same as fdelete()

6. Else → traverse till pos-1

o If node not found → error

o Else adjust links: temp->next = node->next

o If node == last → update last = temp

o Free node

7. Stop

8. Display (display())

1. Start

2. If list empty → print message and stop

3. Set temp = last->next

4. Repeat until temp == last:

o Print temp->data

o Move temp = temp->next

5. Print last->data

6. Stop

9. Count Nodes (count())

1. Start

2. If list empty → return 0

3. Initialize cnt = 0

4. Traverse from last->next to last

o Increment cnt for each node

5. Return cnt

6. Stop
10. Search (search())

1. Start

2. Input key

3. If list empty → print not found

4. Initialize pos = 1, temp = last->next

5. Repeat until temp == last:

o If temp->data == key → print "Found at pos" and stop

o Move temp = temp->next, pos++

6. Check last node → if match print "Found" else "Not Found"

7. Stop

11. Sort (sort())

1. Start

2. If list empty or one node → stop

3. Use pointer i from last->next to last

4. For each i, use j = i->next and compare till loop ends

5. If i->data > j->data → swap values

6. Print "List sorted"

7. Stop

You might also like