B - CSE Assignment
B - CSE Assignment
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 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");
#include <stdio.h>
#include <stdlib.h>
struct node
{
int val;
struct node *next;
struct node *prev;
};
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();
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");
}
}
}
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;
}
}
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;
}
}
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;
}
}
}
}
}
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);
}
}
}