You are to write three separate programs, one each for stacks, queues, and linke
ID: 3803343 • Letter: Y
Question
You are to write three separate programs, one each for stacks, queues, and linked-lists that broadly support adding, deleting and displaying data.
Each of your program should include a class definition for "chunk", a class definition for the data structure (stack/queue/LL) as well as a main() function that should give a "menu" like interface to call the various functions. This "menu" like interface can be easily acheived with a "while(1)" loop and a "switch" statement nested within it, like we did in the Stack example during lecture.
Functions required for each data structure:
Linked list:
- void insertAtHead(int value) Hint: Similar to Stacks, insert at position 1
- void insertAtPosition(int position, int value) Hint: This should insert a node with ‘value’ at any position in the list as long as that position is valid
- void removeFromPosition(int position) Hint: Similar to insert above, this should remove a node from a list as long as position is valid
- void displayContents()
Stacks:
- void push(int value)
- void pop()
- void displayContents()
Queues:
- void enqueue(int value)
- void dequeue()
- void displayContents()
Please provide me 3 code, please use C++. Thank you for your helping!!!!! Have a wonderful day.
Explanation / Answer
for stack
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int arr[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
arr[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<arr[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<arr[i] <<" ";
}
};
main()
{
int ch;
stack st;
while(1)
{
cout <<" 1.push 2.pop 3.display_contents 4.exit Enter ur choice ";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}
// for queue
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class queue
{
int arr[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
arr[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<arr[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<arr[i]<<" ";
}
};
main()
{
int ch;
queue q;
while(1)
{
cout <<" 1.enqueue 2.dequeue 3.display 4.exit Enter ur choice ";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
q.insert(ch);
break;
case 2: q.delet(); break;
case 3: q.display();break;
case 4: exit(0);
}
}
return (0);
}
// for link list
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *next;
}*start;
class single_llist
{
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void delete_pos();
void display();
single_llist()
{
start = NULL;
}
};
main()
{
int choice, nodes, element, position, i;
single_llist sl;
start = NULL;
while (1)
{
cout<<endl<<"Operations on linked list"<<endl;
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Insert node at position"<<endl;
cout<<"3.Delete a Particular Node"<<endl;
cout<<"4.Display Linked List"<<endl;
cout<<"5.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at a given position:"<<endl;
sl.insert_pos();
cout<<endl;
break;
case 3:
cout<<"Delete a particular node: "<<endl;
sl.delete_pos();
break;
case 4:
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
case 5:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
node *single_llist::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
void single_llist::insert_begin()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
void single_llist::insert_pos()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}
void single_llist::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
void single_llist::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.