Exp.1.b - Circular Singly Linked List Implementation
Exp.1.b - Circular Singly Linked List Implementation
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;
}
}
}
}
}
{
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
}
}
ptr->next = temp;
temp->next=head;
}
}
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
{
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)
flag=0;
else
17
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
{
flag=1;
i++;
if(ptr->data==item)
flag=0;
18
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
}
else
flag=1;
if(flag==1)
19
Exp.1.b. CIRCULAR SINGLY LINKED LIST IMPLEMENTATION
}
20