0% found this document useful (0 votes)
2 views

csll

The document contains a C program that implements a circular linked list with functionalities to insert, delete, peek, and display elements. It defines a structure for nodes and provides a menu-driven interface for user interaction. The program utilizes dynamic memory allocation for node creation and manages the list operations accordingly.

Uploaded by

nandanhs14
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)
2 views

csll

The document contains a C program that implements a circular linked list with functionalities to insert, delete, peek, and display elements. It defines a structure for nodes and provides a menu-driven interface for user interaction. The program utilizes dynamic memory allocation for node creation and manages the list operations accordingly.

Uploaded by

nandanhs14
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/ 3

7th

14 December 2024 08:22

#include<stdio.h>
#include<malloc.h> // for malloc and free()
#include<stdlib.h>
struct node // structure of a node
{
int data; // data part
struct node *next; // address(points to next) part
} typedef (node); // node is a user defined data type(here)
//func prototypes
node* getnode();
void menu();
void insert_beg();
void delete_beg();
void peek();
void display();
node* start = NULL; // start points to first node
int main() // main function
{
int op;
do{
menu();
scanf("%d", &op);
switch(op)
{
case 0: exit(0);
case 1: insert_beg(); break;
case 2: delete_beg(); break;
case 3: peek(); break;
case 4: display(); break;
default: printf("Invalid Choice!");
break;
}
}while(1);
return 0;
}
node * getnode() // create a new node
{
node * temp; // temp is a pointer to node
temp = (node *)malloc(sizeof(node)); // allocate memory for a node
return temp; // return address of the node
}
void insert_beg() // insert at beginning
{
node *t, *p; // t and p are pointers to node
t = getnode(); // create a new node t
int x;
printf("enter element to insert\n");
scanf("%d", &x);
t->data = x; // store data in t
if (start == NULL) // empty list
{
start = t;
t->next = start; // point to itself to make it circular
printf("%d Inserted\n", start->data);
}
else // more than one node
{
p = start;
while (p->next != start) {
p = p->next;

New Section 2 Page 1


p = p->next;
}
t->next = start;
p->next = t;
start = t;
printf("%d Inserted\n", start->data);
}
}
void delete_beg() // delete from beginning
{
node *t, *p;
if (start == NULL) // empty list
{
printf("List is empty\n");
return;
}
if (start->next == start) // only one node
{
t = start;
start = NULL;
printf("%d deleted\n", t->data);
free(t);
}
else // more than one node
{
p = start;
while (p->next != start) {
p = p->next;
}
t = start;
printf("%d deleted\n", t->data);
start = start->next;
p->next = start;
free(t);
}
}
void peek() // display first element
{
if(start==NULL) // empty list
{
printf("Empty\n");
}
else // more than one node
{
printf("%d\n", start->data); //print first element
}
}
void display() // display all elements
{
if(start==NULL) // empty list
{
printf("Empty\n");
}
else // more than one node
{
node *p;
for(p=start; p->next!=start; p=p->next) // traverse(visit) all nodes
{
printf("%d -> ", p->data);
}
printf("%d -> NULL\n", p->data); // end of list
}
}
void menu() // display menu
{
printf("0 -> Exit\n1-> Insert\n2-> Delete\n3-> Peek\n4-> Display\nEnter your
Choice\n");
}

New Section 2 Page 2


}

New Section 2 Page 3

You might also like