Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include<iostream> #include<stdlib.h> using namespace std; //STEP1: Create a con

ID: 3806167 • Letter: #

Question

#include<iostream>
#include<stdlib.h>
using namespace std;

//STEP1: Create a conceptual "idea" of a chunk. We'll build a stack of chunks
class chunk {
public:
   int value;
   chunk *next;
   //give our building blocks a default look and feel
   chunk() {
       value = 0;
       next = NULL;
   }
};

class Stack {
public:
   chunk *top; //gateway pointer to the structure

   //constructor
   Stack() {
       top = NULL; //start with an empty stack
   }
  
   //Functions, with an underlying philosophy: LIFO
   //Function 1: Add to the stack, popularly called 'push'
   void push(int x) {
       //get a new chunk, we are going to add this
       chunk *temp;
       temp = new chunk;
       temp->value = x;

       //is my stack empty?
       if(top == NULL) {
           //new element becomes the top
           top = temp;
       }
       else {
           //stack has more than one chunk in there already
           temp->next = top;
           top = temp;
       }
      
   }

   //Function 2: Delete from stack, popularly called 'pop'
   void pop() {
       if(top == NULL) {
           cout << "Empty stack, nothing to delete" << endl;
       }
       else {
           chunk *temp;
           temp = top;
           top = temp->next; //top points to the second element
           cout << "About to delete: " << temp->value << endl;
           delete temp;
       }
   }

   void display() {
       chunk *traverse = top;
       if(top == NULL) {
           cout << "Empty Stack. Nothing to display" << endl;
       }
       else {
           while(traverse != NULL) {
               int i;
               cout << traverse->value << "-->" << endl;
              
               traverse = traverse->next;

           }
       }
   }


};


int main()
{

   Stack ourStack;
   int choice = 0;

   while(1) {
       cout << "Press 1 to add to stack" << endl;
       cout << "Press 2 to delete from stack" << endl;
       cout << "Press 3 to display" << endl;
       cout << "Anything else to quit" << endl;
       cin >> choice;

       switch(choice) {
           case 1: cout << "Add what?" << endl;
                   int value;
                   cin >> value;
                   ourStack.push(value);
                   break;

           case 2: ourStack.pop();
                   break;

           case 3: ourStack.display();
                   break;

           default: exit(1);
       }
   }


}

Please base on this stack code, wirte a Linked list code. Thank you!

Explanation / Answer

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *next;
}*start;

/*
* Class Declaration
*/
class single_llist
{
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void display();
single_llist()
{
start = NULL;
}
};

/*
* Main :contains menu
*/
main()
{
int choice, nodes, element, position, i;
single_llist sl;
start = NULL;
while (1)
{
  
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Insert node at last"<<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 Last: "<<endl;
sl.insert_last();
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;
}
}
}

/*
* Creating Node
*/
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;
}
}

/*
* Inserting element in beginning
*/
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;
}

/*
* Inserting Node at last
*/
void single_llist::insert_last()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{   
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last"<<endl;
}


/*
* Delete element at a given position
*/
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;
}
}

* Display Elements of a link list
*/
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;
}