0% found this document useful (0 votes)
14 views11 pages

B - CSE Assignment

The document provides C code for implementing circular linked lists, both singly and doubly linked. It includes functions for inserting, deleting, searching, and displaying nodes in the list. Additionally, it covers operations like sorting and updating node values.

Uploaded by

roniach019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

B - CSE Assignment

The document provides C code for implementing circular linked lists, both singly and doubly linked. It includes functions for inserting, deleting, searching, and displaying nodes in the list. Additionally, it covers operations like sorting and updating node values.

Uploaded by

roniach019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

/*Circular linked list as Single Linked List

1. insertAtBegininng(int val)
2. insertAtEnd(int val)
3. insertAtASpecificPostion(int position, int val)
4. searchWithPosition(int val)
5. deleteHead()
6. deleteTail()
7. deleteASpecificPosition(int position)
8. printMiddleItem()
9. reverseList() */

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void insertAtBegininng ();


void insertAtEnd ();
void searchWithPosition();
void deleteHead();
void deleteTail();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 7)
{
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from
Beginning\n4.Delete from last\n5.Search for an element\n6.Show\n7.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
insertAtBegininng();
break;
case 2:
insertAtEnd();
break;
case 3:
deleteHead();
break;
case 4:
deleteTail();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertAtBegininng()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
}

}
void insertAtEnd()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}

printf("\nnode inserted\n");
}

void deleteHead()
{
struct node *ptr;
if(head == NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}

else
{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");

}
}
void deleteTail()
{
struct node *ptr, *preptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if (head ->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");

}
else
{
ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
free(ptr);
printf("\nnode deleted\n");

}
}

void search()
{
struct node *ptr;
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
if(head ->data == item)
{
printf("item found at location %d",i+1);
flag=0;
}
else
{
while (ptr->next != head)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
}
if(flag != 0)
{
printf("Item not found\n");
}
}

void display()
{
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n");

while(ptr -> next != head)


{

printf("%d\n", ptr -> data);


ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}

/*Circular Linked List as Doubly Linked List


10. insertAtBegininng(int val)
11. insertAtEnd(int val)
12. insertAtASpecificPostion(int position, int val)
13. searchWithPosition(int val)
14. deleteHead()
15. deleteTail()
16. deleteASpecificPosition(int position)
17. printMiddleItem()
18. reverseList() */

/* C Program to Implement Circular Doubly Linked List */

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

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

typedef struct node n;

n* create_node(int);

void add_node();
void insertAtBegininng();
void insertAtEnd();
void insertAtASpecificPostion();
void delete_node_position();
void searchWithPosition();
void display_from_beg();
void reverseList();

n *new, *ptr, *prev;


n *first = NULL, *last = NULL;
int number = 0;
void main()
{
printf("\n linked list\n");

printf("1.insert at beginning \n 2.insert at end\n 3.insert at position\


n4.sort linked list\n 5.delete node at position\n 6.updatenodevalue\n7.search
element \n8.displaylist from beginning\n9.display list from end\n10.exit ");

int ch;

while (1)
{
printf("\n enter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1 :
insertAtBegininng();
break;
case 2 :
insertAtEnd();
break;
case 3 :
insertAtASpecificPostion();
break;
case 4 :
sort_list();
break;
case 5 :
delete_node_position();
break;
case 6 :
update();
break;
case 7 :
searchWithPosition();
break;
case 8 :
display_from_beg();
break;
case 9 :
reverseList();
break;
case 10 :
exit(0);
case 11 :
add_node();
break;
default:
printf("\ninvalid choice");
}
}
}

/* MEMORY ALLOCATED FOR NODE DYNAMICALLY */


n* create_node(int info)
{
number++;
new = (n *)malloc(sizeof(n));
new->val = info;
new->next = NULL;
new->prev = NULL;
return new;
}

/* ADDS NEW NODE */

void add_node()
{
int info;
printf("\nenter the value you would like to add:");
scanf("%d", &info);
new = create_node(info);
if (first == last && first == NULL)
{
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}

else
{
last->next = new;
new->prev = last;
last = new;
last->next = first;
first->prev = last;
}
}

/* INSERTS ELEMENT AT FIRST */

void insertAtBegininng()
{
int info;
printf("\nenter the value to be inserted at first:");
scanf("%d",&info);
new = create_node(info);
if (first == last && first == NULL)
{
printf("\ninitially it is empty linked list later insertion is done");
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}

else
{
new->next = first;
first->prev = new;
first = new;
first->prev = last;
last->next = first;
printf("\n the value is inserted at begining");
}
}
/*INSERTS ELEMNET AT END*/
void insertAtEnd()
{
int info;
printf("\nenter the value that has to be inserted at last:");
scanf("%d", &info);
new = create_node(info);
if (first == last && first == NULL)
{
printf("\ninitially the list is empty and now new node is inserted but
at first");
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}

else
{
last->next = new;
new->prev = last;
last = new;
first->prev = last;
last->next = first;
}
}

/*INSERTS THE ELEMENT AT GIVEN POSITION*/


void insertAtASpecificPostion()
{
int info, pos, len = 0, i;
n *prevnode;
printf("\n enter the value that you would like to insert:");
scanf("%d", &info);
printf("\n enter the position where you have to enter:");
scanf("%d", &pos);
new = create_node(info);
if (first == last && first == NULL)
{
if (pos == 1)
{
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}

else
printf("\n empty linked list you cant insert at that particular
position");
}

else
{
if (number < pos)
printf("\n node cant be inserted as position is exceeding the
linkedlist length");

else
{
for (ptr = first, i = 1;i <= number;i++)
{
prevnode = ptr;
ptr = ptr->next;
if (i == pos-1)
{
prevnode->next = new;
new->prev = prevnode;
new->next = ptr;
ptr->prev = new;
printf("\ninserted at position %d succesfully", pos);
break;
}
}
}
}
}

/*SORTING IS DONE OF ONLY NUMBERS NOT LINKS*/


void sort_list()
{
n *temp;
int tempval, i, j;
if (first == last && first == NULL)
printf("\nlinked list is empty no elements to sort");

else
{
for (ptr = first,i = 0;i < number;ptr = ptr->next,i++)
{
for (temp = ptr->next,j=i;j<number;j++)
{
if (ptr->val > temp->val)
{
tempval = ptr->val;
ptr->val = temp->val;
temp->val = tempval;
}
}
}
for (ptr = first, i = 0;i < number;ptr = ptr->next,i++)
printf("\n%d", ptr->val);
}
}
/*DELETION IS DONE*/
void delete_node_position()
{
int pos, count = 0, i;
n *temp, *prevnode;
printf("\n enter the position which u wanted to delete:");
scanf("%d", &pos);
if (first == last && first == NULL)
printf("\n empty linked list you cant delete");

else
{
if (number < pos)
printf("\n node cant be deleted at position as it is exceeding the
linkedlist length");
else
{
for (ptr = first,i = 1;i <= number;i++)
{
prevnode = ptr;
ptr = ptr->next;
if (pos == 1)
{
number--;
last->next = prevnode->next;
ptr->prev = prevnode->prev;
first = ptr;
printf("%d is deleted", prevnode->val);
free(prevnode);
break;
}

else if (i == pos - 1)
{
number--;
prevnode->next = ptr->next;
ptr->next->prev = prevnode;
printf("%d is deleted", ptr->val);
free(ptr);
break;
}
}
}
}
}
/*UPDATION IS DONE FRO GIVEN OLD VAL*/
void update()
{
int oldval, newval, i, f = 0;
printf("\n enter the value old value:");
scanf("%d", &oldval);
printf("\n enter the value new value:");
scanf("%d", &newval);
if (first == last && first == NULL)
printf("\n list is empty no elemnts for updation");
else
{
for (ptr = first, i = 0;i < number;ptr = ptr->next,i++)
{
if (ptr->val == oldval)
{
ptr->val = newval;
printf("value is updated to %d", ptr->val);
f = 1;
}
}
if (f == 0)
printf("\n no such old value to be get updated");
}
}
/*SEARCHING USING SINGLE KEY*/
void searchWithPosition()
{
int count = 0, key, i, f = 0;
printf("\nenter the value to be searched:");
scanf("%d", &key);
if (first == last && first == NULL)
printf("\nlist is empty no elemnets in list to search");
else
{
for (ptr = first,i = 0;i < number;i++,ptr = ptr->next)
{
count++;
if (ptr->val == key)
{
printf("\n the value is found at position at %d", count);
f = 1;
}
}
if (f == 0)
printf("\n the value is not found in linkedlist");
}
}
/*DISPLAYING IN BEGINNING*/
void display_from_beg()
{
int i;
if (first == last && first == NULL)
printf("\nlist is empty no elemnts to print");
else
{
printf("\n%d number of nodes are there", number);
for (ptr = first, i = 0;i < number;i++,ptr = ptr->next)
printf("\n %d", ptr->val);
}
}
/* DISPLAYING IN REVERSE */
void reverseList()
{
int i;
if (first == last && first == NULL)
printf("\nlist is empty there are no elments");
else
{
for (ptr = last, i = 0;i < number;i++,ptr = ptr->prev)
{
printf("\n%d", ptr->val);
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy