0% found this document useful (0 votes)
16 views23 pages

A69 DS Ass10

Data structures assignment

Uploaded by

Shruti Punekar
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)
16 views23 pages

A69 DS Ass10

Data structures assignment

Uploaded by

Shruti Punekar
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/ 23

Name: Vijayalaxmi Punekar

Class: SY BTech CSE


Batch: SA3
Roll No: 69

Assignment No-X
Singly Circular Linked List:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};

struct node* ins_beg(struct node*,int);


struct node* ins_end(struct node*,int);
struct node* ins_mid(struct node*,int,int);
struct node* del_beg(struct node*);
struct node* del_end(struct node*);
struct node* del_mid(struct node*,int);
void display(struct node*);

void main()
{
struct node *start=NULL;
int item,ch,k;
while(1)
{
printf("\n***************Select Option***********************\n");
printf("1.Insert at the beginning of the list.\n");
printf("2.Insert at the end of the list.\n");
printf("3.Insert at the middle of the list.\n");
printf("4.Delete from the beginning of the list.\n");
printf("5.Delete from the end of the list.\n");
printf("6.Delete from the middle of the list.\n");
printf("7.Display the list.\n");
printf("8.Exit.\n");
printf("***************************************************\n");
scanf("%d",&ch);

switch(ch)
{
case 1:
printf("Enter the element: \n");
scanf("%d",&item);
start=ins_beg(start,item);
break;

case 2:
printf("Enter the element: \n");
scanf("%d",&item);
start=ins_end(start,item);
break;

case 3:
printf("Enter element to be added: \n");
scanf("%d",&item);
printf("Enter element after which u want to add: \n");
scanf("%d",&k);
start=ins_mid(start,item,k);
break;

case 4:
start=del_beg(start);
break;

case 5:
start=del_end(start);
break;

case 6:
printf("Enter the element to be deleted: \n");
scanf("%d",&item);
start=del_mid(start,item);
break;

case 7:
display(start);
break;

case 8:
exit(1);

default:
printf("Wrong Choic:(\n");
}
}
}

struct node* ins_beg(struct node *s,int x)


{
struct node *temp,*p;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
if(s==NULL)
{
s=temp;
s->next=s;
return s;
}
p=s;
while((p->next)!=s)
{
p=p->next;
}
temp->next=s;
s=temp;
p->next=s;
return s;
}

struct node* ins_end(struct node *s,int x)


{
struct node *temp,*p;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
if(s==NULL)
{
s=temp;
s->next=s;
return s;
}
p=s;
while(p->next!=s)
{
p=p->next;
}
p->next=temp;
temp->next=s;
return s;
}

struct node* ins_mid(struct node *s,int x,int k)


{
struct node *temp,*p;
p=s;
if(p==NULL)
{
printf("List is empty....\n");
return s;
}
while(p->next!=s)
{
if(p->data==k)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->next=p->next;
p->next=temp;
return s;
}
p=p->next;
printf("Not Present:( \n");
return s;
}
}

struct node* del_beg(struct node *s)


{
struct node *temp,*p;
if(s==NULL)
{
printf("List is empty:( \n");
return s;
}
p=s;
if(s->next==s)
{
temp=s;
s=NULL;
free(temp);
return s;
}
p=s;
while((p->next)!=s)
{
p=p->next;
}
temp=s;
s=s->next;
p->next=s;
free(temp);
return s;
}

struct node *del_end(struct node *s)


{
struct node *temp,*ptemp;
if(s==NULL)
{
printf("List is empty:( \n");
return s;
}
if(s->next==s)
{
temp=s;
s=NULL;
free(temp);
return s;
}
temp=s;
ptemp=temp;
temp=temp->next;
while((temp->next)!=s)
{
ptemp=temp;
temp=temp->next;
}
ptemp->next=temp->next;
free(temp);
return s;
}

/*struct node* del_mid(struct node *s,int x)


{
struct node *temp,*ptemp;
if(s==NULL)
{
printf("List is empty:( \n");
return s;
}
if((s->next)==s)
{
if(s->data==x)
{
temp=s;
s->next=s;;
free(temp);
return s;
}
printf("Not Present:( \n");
return s;
}
temp=s;
while((temp->data)!=x && (temp->next!=s))
{
ptemp=temp;
temp=temp->next;
}
if(temp->data==x)
{
ptemp->next=temp->next;
free(temp);
return s;
}
printf("Not Present:( \n");
return s;
}*/
struct node* del_mid(struct node *s, int x)
{
struct node *temp, *ptemp;

if (s == NULL) // List empty


{
printf("List is empty:( \n");
return s;
}

// Deleting the head node


if (s->data == x)
{
if (s->next == s) // Only one node
{
free(s);
return NULL;
}

// Find last node and update head


temp = s;
while (temp->next != s)
temp = temp->next;
temp->next = s->next;
temp = s;
s = s->next;
free(temp);
return s;
}

// Deleting middle or end node


ptemp = s;
temp = s->next;
while (temp != s && temp->data != x)
{
ptemp = temp;
temp = temp->next;
}

if (temp->data == x) // Node found


{
ptemp->next = temp->next;
free(temp);
}
else
printf("Not Present:( \n");

return s;
}

void display(struct node *s)


{
struct node *p;
p=s;
if(p==NULL)
{
printf("List is empty:( \n");
return;
}
printf("Elements in the list are: ");
while((p->next)!=s)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d\t",p->data);
}
Output:
2.

Program:
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node in a circular doubly linked list


struct node {
int data;
struct node* next;
struct node* prev;
};

// Function prototypes
struct node* insert_head(struct node* head, int data);
struct node* insert_end(struct node* head, int data);
struct node* insert_after(struct node* head, int target_data, int data);
struct node* delete_head(struct node* head);
struct node* delete_end(struct node* head);
struct node* delete_node(struct node* head, int data);
void display(struct node* head);

int main() {
struct node* head = NULL;
int choice, data, target_data;

while (1) {
printf("\nCircular Doubly Linked List Menu:\n");
printf("1. Insert at head\n");
printf("2. Insert at end\n");
printf("3. Insert after a specific node\n");
printf("4. Delete head node\n");
printf("5. Delete end node\n");
printf("6. Delete a specific node\n");
printf("7. Display the list\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter data to insert at head: ");
scanf("%d", &data);
head = insert_head(head, data);
break;

case 2:
printf("Enter data to insert at end: ");
scanf("%d", &data);
head = insert_end(head, data);
break;

case 3:
printf("Enter target data to insert after: ");
scanf("%d", &target_data);
printf("Enter data to insert: ");
scanf("%d", &data);
head = insert_after(head, target_data, data);
break;
case 4:
head = delete_head(head);
break;

case 5:
head = delete_end(head);
break;

case 6:
printf("Enter data to delete: ");
scanf("%d", &data);
head = delete_node(head, data);
break;

case 7:
printf("List contents:\n");
display(head);
break;

case 8:
printf("Exiting...\n");
// Free all nodes before exiting
while (head != NULL) {
head = delete_head(head);
}
exit(0);

default:
printf("Invalid choice, please try again.\n");
break;
}
}

return 0;
}

// Insert a node at the head of the list


struct node* insert_head(struct node* head, int data) {
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = data;
if (head == NULL) {
new_node->next = new_node;
new_node->prev = new_node;
return new_node;
}
struct node* tail = head->prev;
new_node->next = head;
new_node->prev = tail;
head->prev = new_node;
tail->next = new_node;
return new_node;
}

// Insert a node at the end of the list


struct node* insert_end(struct node* head, int data) {
if (head == NULL) {
return insert_head(head, data);
}
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = data;
struct node* tail = head->prev;
new_node->next = head;
new_node->prev = tail;
tail->next = new_node;
head->prev = new_node;
return head;
}

// Insert a node after a specific node


struct node* insert_after(struct node* head, int target_data, int data) {
if (head == NULL) return NULL;
struct node* current = head;
do {
if (current->data == target_data) {
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = data;
new_node->next = current->next;
new_node->prev = current;
current->next->prev = new_node;
current->next = new_node;
return head;
}
current = current->next;
} while (current != head);
printf("Node with data %d not found.\n", target_data);
return head;
}

// Delete the head node


struct node* delete_head(struct node* head) {
if (head == NULL) return NULL;
if (head->next == head) { // Only one node
free(head);
return NULL;
}
struct node* new_head = head->next;
struct node* tail = head->prev;
tail->next = new_head;
new_head->prev = tail;
free(head);
return new_head;
}

// Delete the end node


struct node* delete_end(struct node* head) {
if (head == NULL) return NULL;
if (head->next == head) { // Only one node
free(head);
return NULL;
}
struct node* tail = head->prev;
struct node* new_tail = tail->prev;
new_tail->next = head;
head->prev = new_tail;
free(tail);
return head;
}

// Delete a specific node


struct node* delete_node(struct node* head, int data) {
if (head == NULL) return NULL;
struct node* current = head;
do {
if (current->data == data) {
if (current == head) { // If the node to be deleted is the head
return delete_head(head);
}
if (current->next == head) { // If the node to be deleted is the end
return delete_end(head);
}
current->prev->next = current->next;
current->next->prev = current->prev;
free(current);
return head;
}
current = current->next;
} while (current != head);
printf("Node with data %d not found.\n", data);
return head;
}

// Display the list


void display(struct node* head) {
if (head == NULL) {
printf("List is empty.\n");
return; }
struct node* current = head;
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}

Output:

You might also like