0% found this document useful (0 votes)
17 views27 pages

19BCS2191 DAA Exp4

The document describes an experiment conducted using C++ to implement operations on doubly linked lists and circular linked lists. It includes the code to insert and delete nodes at the beginning and end of both types of linked lists. It also includes code to implement push and pop operations on a stack using templates.

Uploaded by

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

19BCS2191 DAA Exp4

The document describes an experiment conducted using C++ to implement operations on doubly linked lists and circular linked lists. It includes the code to insert and delete nodes at the beginning and end of both types of linked lists. It also includes code to implement push and pop operations on a stack using templates.

Uploaded by

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

Experiment No: 4

Student Name: Shivani Singh UID: 19BCS2191


Branch: CSE Section/Group: CSE-8/B
Semester: 5 Date of Performance: 10/9/21
Subject Name: DAA Lab Subject Code: CSP-309

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

2. Task to be done/ Which logistics used:


C++ compiler

3. Algorithm/Flowchart :

Insertion in doubly linked list at beginning

Step 1: IF ptr = NULL

Write OVERFLOW Go to Step 9

[END OF IF]

Step 2: SET NEW_NODE = ptr


Step 3: SET ptr = ptr -> NEXT

Step 4: SET NEW_NODE -> DATA = VAL

Step 5: SET NEW_NODE -> PREV = NULL

Step 6: SET NEW_NODE -> NEXT = START

Step 7: SET head -> PREV = NEW_NODE

Step 8: SET head = NEW_NODE

Step 9: EXIT

INSERT AT THE END:

Step 1: IF PTR = NULL

Write OVERFLOW Go to Step 11

[END OF IF]

tep 2: SET NEW_NODE = PTR

Step 3: SET PTR = PTR -> NEXT

Step 4: SET NEW_NODE -> DATA = VAL

Step 5: SET NEW_NODE -> NEXT = NULL

Step 6: SET TEMP = START

Step 7: Repeat Step 8 while TEMP -> NEXT != NULL

Step 8: SET TEMP = TEMP -> NEXT

[END OF LOOP]

Step 9: SET TEMP -> NEXT = NEW_NODE

Step 10: SET NEW Algorithm for PUSH Operation


A simple algorithm for Push operation can be derived as follows − begin procedure push: stack, data

if stack is full return null

endif

top ← top + 1 stack[top] ← data

end procedure_NODE -> PREV = TEMP

Step 11: EXIT

4. CODE FOR DOUBLY LINKED LIST:

#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>

using namespace std;

struct node
{

int data;

struct node *next;

};

struct node *head;

void begin_insert()

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("node inserted\n");

void last_insert()

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 begin_delete()

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 last_delete()

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 display()

struct node *ptr;

ptr=head;

if(head == NULL)

printf("\nnothing to print");

else

printf("\n NODES are: \n");

while(ptr -> next != head)

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

ptr = ptr -> next;


}

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

int main ()

int choice =0;

while(choice != 6)

printf("CIRCULAR LINKED LIST OPERATION ...\n");

printf("\n===============================================\n");

printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from


last\n5.Show\n6.Exit\n");

printf("\nEnter your choice?\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:

printf("Please enter valid choice..");

}
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

if stack is full return null

endif

top ← top + 1 stack[top] ← data

end procedure

A Pop operation may involve the following steps −

• Step 1 − Checks if the stack is empty.


• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.

CODE:
#include <iostream>
#include <cstdlib>
using namespace std;
#define SIZE 10
template <class X>
class stack

X *arr;
int top;

int capacity;

public:

stack(int size = SIZE); void push(X);

X pop();

X peek();

int size();

bool isEmpty(); bool isFull();

~stack(){

delete[] arr; }

// constructor

};

template <class X> stack<X>::stack(int size)

arr = new X[size];

capacity = size;

top = -1; }

template <class X>

void stack<X>::push(X x)

{
if (isFull()) {

cout << "OverFlow\nProgram Terminated\n";

exit(EXIT_FAILURE); }

cout << "Inserting " << x << endl;

arr[++top] = x; }

template <class X>

X stack<X>::pop()

{
if (isEmpty())

cout << "UnderFlow\nProgram Terminated\n";

exit(EXIT_FAILURE); }

cout << "Removing " << peek() << endl;

return arr[top--];

template <class X>

X stack<X>::peek()

if (!isEmpty())

return arr[top];

else
exit(EXIT_FAILURE);

template <class X>

int stack<X>::size()

return top + 1; }

template <class X>

bool stack<X>::isEmpty() {

return top == -1; // or return size() == 0;


}

template <class X>

bool stack<X>::isFull()

return top == capacity - 1;

int main() {

stack<string> pt(2); pt.push("A"); pt.push("B"); pt.pop();

// or return size() == capacity;

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();

// check if stack is empty or not

if (pt.isEmpty())

cout << "Stack Is Empty\n";

else

cout << "Stack Is Not Empty\n";

return 0; }

OBSERVATION/ OUTPUT:
5 Observations/Discussions/ Complexity Analysis:
Learning outcomes (What I have learnt):
a. Concept of Linked list has been revised.

b. Concept of Stacks was put to use.


c. Learned about Double and circular linked list.

Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like