Data Structures La2b
Data Structures La2b
Data Structures La2b
WEEK 5
PROBLEM STATEMENT 1:
Write a C Program to implement Circular linked list and perform all the operations
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int key;
struct node{
int data;
}*head=NULL,*last=NULL,*n,*temp,*p1;
void create(){
scanf("%d",&n->data);
n->next=NULL;
if(head==NULL){
head=n;
last=n;
else{
last->next=n;
last=n;
last->next=head;
void insert_begin(){
scanf("%d",&n->data);
n->next=head;
head=n;
last->next=head;
void insert_end(){
scanf("%d",&n->data);
n->next=NULL;
last->next=n;
last=n;
last->next=head;
void insert_after(){
scanf("%d",&n->data);
n->next=NULL;
printf("Enter key");
scanf("%d",&key);
temp=head;
do{
if(temp->data==key)
break;
temp=temp->next;
}while(temp!=head);
n->next=temp->next;
temp->next=n;
void insert_before(){
scanf("%d",&n->data);
n->next=NULL;
printf("Enter key");
scanf("%d",&key);
temp=head;
do{
if(temp->data==key)
break;
else{
p1=temp;
temp=temp->next;
}while(temp!=head);
n->next=p1->next;
p1->next=n;
void del_begin(){
temp=head;
head=head->next;
last->next=head;
free(temp);
void del_end(){
temp=head;
while(temp!=NULL){
p1=temp;
temp=temp->next;
p1->next=NULL;
last=p1;
last->next=head;
free(temp);
void del(){
printf("Enter a key\n");
scanf("%d",&key);
temp=head;
do{
if(temp->data==key)
break;
else{
p1=temp;
temp=temp->next;
}while(temp!=head);
p1->next=temp->next;
free(temp);
void display(){
temp=head;
do{
printf("%d\t",temp->data);
temp=temp->next;
}while(temp!=head);
int main(){
create();
int ch;
printf("\nInsertion:-\n\n1.Insert a node at beginning\n2.Insert a node at end\n3.Insert a node after a given
element\n4.Insert a node before a given element\n\nDeletion:-\n\n5.Delete a node at beginning\n6.Delete a node at end\
n7.Delete node of given key\n\n8.Display");
do{
scanf("%d",&ch);
switch(ch){
case 1:insert_begin();break;
case 2:insert_end();break;
case 3:insert_after();break;
case 4:insert_before();break;
case 5: del_begin();break;
case 6: del_end();break;
case 7:del();break;
case 8:display();break;
default:printf("Invalid choice");break;
}while(ch<=8);
OUTPUT:
PROBLEM STATEMENT 2:
Write a C Program to implement Circular linked list and perform all the operations
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int key;
struct node {
int data;
void create() {
scanf("%d", &n->data);
n->next = NULL;
n->prev = NULL;
if (head == NULL) {
head = n;
last = n;
} else {
last->next = n;
n->prev = last;
last = n;
void insert_begin() {
scanf("%d", &n->data);
n->next = head;
n->prev = NULL;
if (head != NULL) {
head->prev = n;
head = n;
if (last == NULL) {
last = n;
void insert_end() {
scanf("%d", &n->data);
n->next = NULL;
n->prev = last;
if (last != NULL) {
last->next = n;
last = n;
if (head == NULL) {
head = n;
void insert_after() {
scanf("%d", &n->data);
scanf("%d", &key);
temp = head;
if (temp->data == key)
break;
temp = temp->next;
if (temp == NULL) {
} else {
n->next = temp->next;
n->prev = temp;
if (temp->next != NULL) {
temp->next->prev = n;
} else {
last = n;
temp->next = n;
void insert_before() {
scanf("%d", &n->data);
scanf("%d", &key);
if (head->data == key) {
insert_begin();
return;
temp = head;
if (temp->data == key)
break;
temp = temp->next;
if (temp == NULL) {
} else {
n->next = temp;
n->prev = temp->prev;
temp->prev->next = n;
temp->prev = n;
void del_begin() {
if (head == NULL) {
printf("List is empty\n");
return;
temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
} else {
last = NULL;
free(temp);
void del_end() {
if (last == NULL) {
printf("List is empty\n");
return;
}
temp = last;
last = last->prev;
if (last != NULL) {
last->next = NULL;
} else {
head = NULL;
free(temp);
void del() {
if (head == NULL) {
printf("List is empty\n");
return;
scanf("%d", &key);
temp = head;
if (temp->data == key)
break;
temp = temp->next;
if (temp == NULL) {
} else {
if (temp == head) {
del_begin();
} else if (temp == last) {
del_end();
} else {
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
free(temp);
void display() {
if (head == NULL) {
printf("List is empty\n");
return;
temp = head;
printf("%d\t", temp->data);
temp = temp->next;
printf("\n");
int main() {
int ch;
create();
do {
scanf("%d", &ch);
switch (ch) {
return 0;
OUTPUT: