19BCS2191 DAA Exp4
19BCS2191 DAA Exp4
1. Aim/Overview of the practical: (i) Code to Insert and Delete an element at the
beginning and at end in Doubly and Circular Linked List
(ii) Code to push & pop and check Isempty, Isfull,and Return top element in stacks using
templates
3. Algorithm/Flowchart :
[END OF IF]
Step 9: EXIT
[END OF IF]
[END OF LOOP]
endif
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insert_first()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value: ");
scanf("%d",&item);
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insert_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value: ");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
}
void delete_first()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void delete_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
int display()
{
struct node *ptr;
printf("\n NODES ARE: ");
ptr = head;
while(ptr != NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
int main ()
{
int choice =0;
while(choice != 6)
{
printf("\nOPERATION ON LIST\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from last \n5. Display
\n6.Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert_first();
break;
case 2: insert_last();
break;
case 3: delete_first();
break;
case 4: delete_last();
break;
case 5: display();
break;
case 6:exit(0);
break;
default:printf("Please enter valid choice..");
}
}
}
4 Result/Output/Writing Summary:
CODE FOR CIRCULAR LINKED LIST:
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
struct node
{
int data;
};
void begin_insert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
{
scanf("%d",&item);
if(head == NULL)
head = ptr;
else
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
printf("node inserted\n");
void last_insert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW\n");
}
else
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
head = ptr;
else
temp = head;
{
temp = temp -> next;
printf("\nnode inserted\n");
void begin_delete()
if(head == NULL)
printf("\nUNDERFLOW");
}
else if(head->next == head)
head = NULL;
free(head);
printf("\nnode deleted\n");
else
{ ptr = head;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
}
void last_delete()
if(head==NULL)
printf("\nUNDERFLOW");
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
ptr = head;
preptr=ptr;
ptr = ptr->next;
free(ptr);
printf("\nnode deleted\n");
}
void display()
ptr=head;
if(head == NULL)
printf("\nnothing to print");
else
int main ()
while(choice != 6)
printf("\n===============================================\n");
scanf("\n%d",&choice);
switch(choice)
case 1:
begin_insert();
break;
case 2:
last_insert();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
display();
break;
case 6:
exit(0);
break;
default:
}
OBSERVATION FOR CIRCULAR LINKED LIST:
II)Code to push & pop and check Isempty, Isfull,and Return top element in
stacks using templates
ALGORITHM/PROCEDURE:
Algorithm for PUSH Operation:-
A simple algorithm for Push operation can be derived as follows − begin procedure push: stack, data
endif
end procedure
CODE:
#include <iostream>
#include <cstdlib>
using namespace std;
#define SIZE 10
template <class X>
class stack
X *arr;
int top;
int capacity;
public:
X pop();
X peek();
int size();
~stack(){
delete[] arr; }
// constructor
};
capacity = size;
top = -1; }
void stack<X>::push(X x)
{
if (isFull()) {
exit(EXIT_FAILURE); }
arr[++top] = x; }
X stack<X>::pop()
{
if (isEmpty())
exit(EXIT_FAILURE); }
return arr[top--];
X stack<X>::peek()
if (!isEmpty())
return arr[top];
else
exit(EXIT_FAILURE);
int stack<X>::size()
return top + 1; }
bool stack<X>::isEmpty() {
bool stack<X>::isFull()
int main() {
pt.pop();
pt.push("C");
cout << "Top element is: " << pt.peek() << endl;
// Returns the number of elements present in the stack cout << "Stack size is " << pt.size() << endl; pt.pop();
if (pt.isEmpty())
else
return 0; }
OBSERVATION/ OUTPUT:
5 Observations/Discussions/ Complexity Analysis:
Learning outcomes (What I have learnt):
a. Concept of Linked list has been revised.
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):