0% found this document useful (0 votes)
20 views20 pages

Exp.1.b - Circular Singly Linked List Implementation

Uploaded by

kannangr8523
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)
20 views20 pages

Exp.1.b - Circular Singly Linked List Implementation

Uploaded by

kannangr8523
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
You are on page 1/ 20

Exp.1.b.

CIRCULAR SINGLY LINKED LIST IMPLEMENTATION

CIRCULAR SINGLY LINKED LIST IMPLEMENTATION


AIM
To implement singly linked list operations such as create, display, insertion, deletion

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
void search();
struct node* head = NULL;

1
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
struct node
{
int data;
struct node* next;
};
int main()
{
int choice;
while(1)
{
printf("\n*****\n");
printf("Implementation of circular singly linked list \n");
printf("0. Create\n");
printf("1. display\n");
printf("2. Insert Node at beginning\n");
printf("3. Insert Node in specific position\n");
printf("4. Insert Node at end of LinkedList\n");
printf("5. Delete Node at beginning\n");
printf("6. Delete Node at end\n");
2
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
printf("7. Delete Node at position\n");
printf("8. Searching an element\n ");
printf("9. ** To exit **\n”);
1 printf("\n Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 0: create();
break;
case 1: display();
break;
case 2: insert_begin();
break;
case 3: insert_pos();
break;
case 4: insert_end();
break;
case 5: delete_begin();
break;
3
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
case 6: delete_end();
break;
case 7: delete_pos();
break;
case 8: search();
break;
case 9: exit(0);
default:printf("\n Wrong Choice");
break;
}
}
}

void create() //Creation of a node in a linkedlist


{
struct node* temp;
//creating new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
4
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL)
{
head = temp;
temp->next=temp;
}
else{
struct node* ptr = head;
while(ptr->next!=head)
{
ptr = ptr->next;
}
ptr->next = temp; //inserting at end of List
temp->next=head;
}
}

void display() // Displaying the data in the linked list


5
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
{
if(head==NULL)
{
printf("Linked List is Empty\n");
return;
}
printf("Circular Singly Linked List: \n");
struct node* ptr = head;
while(ptr->next!=head)
{
printf("%d ",ptr->data);
ptr = ptr->next;
}
printf("%d ",ptr->data);
printf("\n");
}

void insert_begin() // Insertion of node at the beginning of the linked list


{
6
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
struct node* temp;
// creating a new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL)
{
head = temp;
temp->next=head;
return;
}
else
{
struct node* ptr=head;
while(ptr->next!=head)
{
ptr = ptr->next;
}
7
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
temp->next = head; //point it to old head node
ptr->next=temp;
head = temp; //point head to new first node

}
}

void insert_pos() // Insertion of node at the given position

{
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL) // if list empty will insert & return
{

head = temp;

8
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
temp->next=head;
return;
}
else
{
struct node* prev_ptr;
struct node* ptr = head;
int i , pos;
printf("Enter position: ");
scanf("%d",&pos);
for(int i=0;i<pos;i++)
{
prev_ptr = ptr;
ptr = ptr->next;
}
//new node pointing to node in that pos
temp->next = ptr;
//prevptr pointing to new node
prev_ptr->next = temp;
9
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
}
}

void insert_end() // Insertion of node at the end of the linked list


{
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL)
{
head = temp; //if list is empty, we return
temp->next=head;
return;
}
else
{
struct node* ptr = head;
10
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
while(ptr->next!=head)
{
ptr = ptr->next;
}

ptr->next = temp;
temp->next=head;
}
}

void delete_begin() // Deletion of node at the beginning of the linked list


{
if(head==NULL) //if List is empty we return
{
printf("Linked List is empty | Nothing to delete \n");
return;
}
else
{
11
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
struct node* ptr;
struct node* prev_head = head;
head = head->next; // head node pointing to second node
ptr=head;
while(ptr->next!= prev_head)
{
ptr = ptr->next;
}
// tail node pointing to new node
ptr->next = head;
free(prev_head); // deleting prev head node
printf("Node Deleted \n");
}
}

// to delete last node of LinkedList


void delete_end() // Deletion of node at the end of the linked list
{
if(head==NULL) //if List is empty we return
12
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
{
printf("Linked List is empty | Nothing to delete \n");
return;
}
else if(head->next==head)
{
struct node* ptr = head;
head = NULL;
free(ptr);
}
else
{
struct node* ptr = head;
struct node* prev_ptr = NULL;
while(ptr->next!=head)// traverse till last but one node
{
prev_ptr = ptr;
ptr = ptr->next;
}
13
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
prev_ptr->next = head;
free(ptr); // deleting last node
}
}

void delete_pos() // deletion of node at the given position


{

if(head==NULL) //we return if List is empty


{
printf("Linked List is empty nothing to delete\n");
return;
}

else

14
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
int i,pos;
printf("Enter node position to delete: ");
scanf("%d",&pos);
struct node* ptr=head;
struct node* prev_ptr;
for(int i=0;i<pos;i++)
{
prev_ptr = ptr;
ptr = ptr->next;
}
prev_ptr->next = ptr->next; //prev node pointing to pos+1 node
free(ptr); //deleting node at pos
printf("Node Deleted");

}
}
void search()

15
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
{

struct node* ptr;

int item,i=0,flag;

ptr = head;

if(ptr == NULL)

printf("\nEmpty List\n");

else

16
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
printf("\nEnter item which you want to search?\n");

scanf("%d",&item);

while (ptr->next!=head)

if(ptr->data == item)

printf("item found at location %d ",i);

flag=0;

else

17
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
{

flag=1;

i++;

ptr = ptr -> next;

if(ptr->data==item)

printf("item found at location %d ",i);

flag=0;

18
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
}

else

flag=1;

if(flag==1)

printf("Item not found\n");

19
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
}

20

You might also like