Professional Documents
Culture Documents
4
Student Name: SYED ZAID ALI UID: 19BET1035
Branch: IT Section/Group: 19_IT_1_B
Semester: 5th Date of Performance: 24th Sep 2021
Subject Name: DAA Lab Subject Code: ITP-309
(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
templates2.
Algorithm(i):
C) Deletion at beginning
4. Steps for experiment/practical/Code: (i) Code to Insert and Delete an element at the
beginning and at end in Doubly and Circular Linked List.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void insertion_beginning();
void insertion_last();
void deletion_beginning();
void deletion_last();
void display();
int main ()
scanf("\n%d",&choice);
switch(choice)
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
deletion_beginning();
break;
case 4:
deletion_last();
break;
case 5:
display();
break;
case 6:
exit(0);
break;
default:
void insertion_beginning()
int item;
if(ptr == NULL)
printf("\nLISTFULL");
else
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 insertion_last()
if(ptr == NULL)
printf("\nList Full");
else
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 deletion_beginning()
if(head == NULL)
head = NULL;
free(head);
printf("\nnode deleted sucessfully\n");
else
ptr = head;
free(ptr);
printf("\nnode deleted\n");
void deletion_last()
if(head == NULL)
head = NULL;
free(head);
printf("\nnode deleted\n");
else
ptr = head;
if(ptr->next != NULL)
free(ptr);
printf("\nnode deleted\n");
void display()
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
Algorithm(ii) :
Push :
if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure
Pop :
if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure
Peek:
return stack[top]
end procedure
4. Steps for experiment/practical/Code : (ii) Code to push & pop and check Isempty,
Isfull,and Return top element in stacks using templates2.
#include <iostream>
#include <cstdlib>
#define SIZE 10
class stack
X *arr;
int top;
int capacity;
public:
void push(X);
X pop();
X peek();
int size();
bool isEmpty();
bool isFull();
~stack() {
delete[] arr;
};
stack<X>::stack(int size)
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() {
int main()
stack<string> pt(3);
pt.push("A");
pt.push("B");
pt.pop();
pt.pop();
pt.push("C");
cout << "The top element is " << pt.peek() << endl;
cout << "The stack size is " << pt.size() << endl;
pt.pop();
if (pt.isEmpty()) {
else {
cout << "The stack is not empty\n";
return 0;
(i)
(ii)
3.Learn that how to implement push pop peek operations using stack.