Data Structures La2b

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

DATA STRUCTURES LAB

WEEK 5

NAME :Sukrithi Bhattacharya DATE:24/09/2024


HTNO : 23R21A0560
BRANCH : CSE
SECTION : A

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;

struct node *next;

}*head=NULL,*last=NULL,*n,*temp,*p1;

void create(){

n=(struct node*) malloc(sizeof(struct node));

printf("Enter node data");

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(){

n=(struct node*) malloc(sizeof(struct node));

printf("Enter node data");

scanf("%d",&n->data);

n->next=head;

head=n;

last->next=head;

void insert_end(){

n=(struct node*)malloc(sizeof(struct node));

printf("Enter node data");

scanf("%d",&n->data);

n->next=NULL;

last->next=n;

last=n;

last->next=head;

void insert_after(){

n=(struct node*)malloc(sizeof(struct node));

printf("Enter node data");

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(){

n=(struct node*)malloc(sizeof(struct node));

printf("Enter node data");

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{

printf("\nEnter your choice:-");

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;

struct node *prev, *next;

} *head = NULL, *last = NULL, *n, *temp, *p1;

void create() {

n = (struct node*) malloc(sizeof(struct node));

printf("Enter node data: ");

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() {

n = (struct node*) malloc(sizeof(struct node));

printf("Enter node data: ");

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() {

n = (struct node*) malloc(sizeof(struct node));

printf("Enter node data: ");

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() {

n = (struct node*) malloc(sizeof(struct node));

printf("Enter node data: ");

scanf("%d", &n->data);

printf("Enter key: ");

scanf("%d", &key);

temp = head;

while (temp != NULL) {

if (temp->data == key)
break;

temp = temp->next;

if (temp == NULL) {

printf("Key not found\n");

} 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() {

n = (struct node*) malloc(sizeof(struct node));

printf("Enter node data: ");

scanf("%d", &n->data);

printf("Enter key: ");

scanf("%d", &key);

if (head->data == key) {

insert_begin();

return;

temp = head;

while (temp != NULL) {

if (temp->data == key)

break;
temp = temp->next;

if (temp == NULL) {

printf("Key not found\n");

} 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;

printf("Enter key: ");

scanf("%d", &key);

temp = head;

while (temp != NULL) {

if (temp->data == key)

break;

temp = temp->next;

if (temp == NULL) {

printf("Key not found\n");

} 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;

while (temp != NULL) {

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

temp = temp->next;

printf("\n");

int main() {

int ch;

create();

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\n");

do {

printf("\nEnter your choice:- ");

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\n"); break;

} while (ch <= 8);

return 0;

OUTPUT:

You might also like