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

Given a doubly linked list (code provided to you in the lecture note) implement

ID: 3694612 • Letter: G

Question

Given a doubly linked list (code provided to you in the lecture note) implement a Stack and a Queue. You must use the doubly link list class and utilize it as an object in your implementation of the stack and queue. You may edit it and you may also modify it but you must include it as part of your implementation.


Implement a Stack

A Stack is a Last in First Out (LiFO) structure which translates to the processes of outputting the last element which was inputted in the collection. The Stack is based on the process of putting things on top of one another and taking the item on top (think of a stack of papers).

Operations to implement:

push (E): Add an element to the start of the sequence

pop: Remove an element from the start of the sequence

Peek: Return the first element of the sequence without removing it

atIndex(x): Return the element at the given index (x) Or throw an exception if it is out of bound (if you can control the user input then do that instead)

Size: Return the size of the Stack

isEmpty: Boolean, returns true if the Stack is empty

Empty: Empty the Stack


Implement a Queue

A Queue is a First in First Out (FiFO) structure which translates to the processes of outputting the first item inputted into a collection. The Queue is based on the process of waiting in line to get serviced (bank or six flags), where those who arrive first get serviced first.

Operations to implement:

Enqueue/push (E): Add an element to the end of the sequence

Dequeue/pop: Remove an element from the start of the sequence

Front/Peek: Return the first element of the sequence without removing it (what the head is pointing to)

atIndex(x): Return the element at the given index (x) Or throw an exception if it is out of bound (if you can control the user input then do that instead)

Size: Return the size of the Queue

isEmpty: Boolean, returns true if the Queue is empty

Empty: Empty the Queue

Back: Return what the tail is pointing to


DNode.java
public class DNode
{
  
   private Object e; // place holder for the type of object the
             //collection is storing
   private DNode next; // reference to the next node in the sequence
              // of nodes making up the linked list. Remember
             // that even though Java passes Objects by value
            // an object consisting of reference will behave
           // the same as an object passed by reference unless
          // a deep copy was made.
   private DNode prev;
    public DNode (Object e) // Constructor for implementing a single node
    {
       this.e = e; // The value of the element to be assigned to
                   // this particular instance of node
       this.next = null; // An empty reference since there is no node
                         // to follow.
       this.prev = null;
    }
    public DNode (Object e, DNode next, DNode prev) // Constructor for implementing a
                                     // node that comes after another
                                    // node
    {
        this.e = e; // The value of the element to be assigned to
                   // this particular instance of node
        this.next = next; // reference to the subsequent node to follow
        this.prev = prev;
    }
    public void changeNext (DNode next) // Changes the link to the next node
    {
        this.next = next;
    }
    public void changePrev (DNode prev) // Changes the link to the next node
    {
        this.prev = prev;
    }
    public DNode getNext() // Returns the node next in the sequence
    {
        return this.next;
    }
    public Object getValue() // Returns the value a node is holding
    {
        return this.e;
    }
    public Boolean hasNext() // Returns a boolean determining regarding
                            // the status of subsequent nodes after
                           // the current node
    {
        return !(this.next.getValue() == null || this.next == null);
    }
        public Boolean hasprev() // Returns a boolean determining regarding
                            // the status of subsequent nodes after
                           // the current node
    {
        return !(this.prev.getValue() == null || this.prev == null);
    }
      
    public void insertAfterNode( Object input)
    {
      this.changeNext(new DNode (input, this.getNext(), this));
        
    }
    public void insertAfterNode (DNode input)
    {
        this.changeNext(input);
        input.changeNext(this.next);
        input.changePrev(this);
      
    }
    public void insertbeforeNode (DNode input)
    {
        this.changeNext(input);
    }
    public void insertBetweenNodes(DNode before, DNode after)
    {
        this.changeNext(after);
        this.changePrev(before);
        before.changeNext(this);
        after.changePrev(this);
    }

    public DNode getPrev()
    {
        return this.prev;
    }
}

DoublyLinkedList.java


public class DoublyLinkedList
{
    private DNode head;
    private DNode tail;
    private int size;
  
    public DoublyLinkedList () // construct an empty list
    {
        this.tail = new DNode (null, null, this.head);
        this.head = new DNode (null, this.tail, null);
      
        this.size = 0;
      
    }
  
    public DoublyLinkedList (DNode next) // constructs a list
                                        // out of a single node
    {
        this.tail = new DNode (null, null, next);
        this.head = new DNode (null, next, null);
        next.changeNext(this.tail);
        next.changePrev(this.head);
      
        this.size = 1;
      
    }
    public DoublyLinkedList(Object [] objectArray) // construct a list out of
                                                  // an array
    {
        this.tail = new DNode (null, null, this.head);
        this.head = new DNode (null, this.tail, null);
        DNode temp = this.head;
        for (Object e : objectArray)
        {
            //Anonomus function
            new DNode (e, temp.getNext(),temp).insertBetweenNodes(temp, temp.getNext());
            temp = temp.getNext();
            this.size += 1;
        }
    }
  
    public void addToFrontofList (Object toAdd) // Appends the begining
                                               // of the list
    {
        DNode temp = new DNode (toAdd, this.head.getNext(), this.head);
        this.head.getNext().changePrev(temp);
        this.head.changeNext(temp);
        this.size += 1;

    }
    public void addToendofList (Object toAdd) // appends the end of the list
                                             // with a node
    {
        DNode temp = new DNode (toAdd, this.tail, this.tail.getPrev());
        this.tail.getPrev().changeNext(temp);
        this.tail.changePrev(temp);
        this.size += 1;
      
    }
  
    public void insertAfterNode(DNode current, Object input)// Inserts a new
                                                           // a new node after
                                                          // current node
    {
      current.insertAfterNode(input);
        this.size += 1;
    }
  
    public int getSize() // returns the size of the list
    {
        return this.size;
    }
  
  

       @Override
    public String toString()
    {
        String result = "";
        for (DNode temp = this.head.getNext(); temp.hasNext(); temp = temp.getNext())
        {
           result += temp.getValue();
           result += " -> ";
       }
        result += "End of list";
      
        return result;
    }
}

Explanation / Answer

Solution: Please follow these coding for stack and queue as shown in below..

Implementation of stack:

#include <stdio.h>
#include <stdlib.h>
/* A Doubly Linked List Node */
struct DNode
{
struct DNode *prev;
int data;
struct DNode *next;
};
/* stack data structure that supports findMiddle()
in O(1) time. The Stack is implemented using Doubly Linked List. It
maintains pointer to head node, pointer to middle node and count of
nodes */
struct myStack
{
struct DLLNode *head;
struct DLLNode *mid;
int count;
};

/* Function to create the stack data structure */
struct myStack *createMyStack()
{
struct myStack *ms =
(struct myStack*) malloc(sizeof(struct myStack));
ms->count = 0;
return ms;
};

/* Function to push an element to the stack */
void push(struct myStack *ms, int new_data)
{
/* allocate DNode and put in data */
struct DNode* new_DNode =
(struct DLLNode*) malloc(sizeof(struct DNode));
new_DNode->data = new_data;

/* we are adding at the begining,
prev is always NULL */
new_DNode->prev = NULL;

/* link the old list off the new DNode */
new_DNode->next = ms->head;

/* Increment count of items in stack */
ms->count += 1;

/* Change mid pointer in two cases
1) Linked List is empty
2) Number of nodes in linked list is odd */
if (ms->count == 1)
{
ms->mid = new_DNode;
}
else
{
ms->head->prev = new_DNode;

if (ms->count & 1) // Update mid if ms->count is odd
ms->mid = ms->mid->prev;
}

/* move head to point to the new DNode */
ms->head = new_DNode;
}

/* Function to pop an element from stack */
int pop(struct myStack *ms)
{
/* Stack underflow */
if (ms->count == 0)
{
printf("Stack is empty ");
return -1;
}

struct DNode *head = ms->head;
int item = head->data;
ms->head = head->next;

// If linked list doesn't become empty, update prev
// of new head as NULL
if (ms->head != NULL)
ms->head->prev = NULL;

ms->count -= 1;

// update the mid pointer when we have even number of
// elements in the stack, i,e move down the mid pointer.
if (!((ms->count) & 1 ))
ms->mid = ms->mid->next;

free(head);

return item;
}

// Function for finding middle of the stack
int findMiddle(struct myStack *ms)
{
if (ms->count == 0)
{
printf("Stack is empty now ");
return -1;
}

return ms->mid->data;
}

// Driver program to test functions of myStack
int main()
{
/* Let us create a stack using push() operation*/
struct myStack *ms = createMyStack();
push(ms, 11);
push(ms, 22);
push(ms, 33);
push(ms, 44);
push(ms, 55);
push(ms, 66);
push(ms, 77);

printf("Item popped is %d ", pop(ms));
printf("Item popped is %d ", pop(ms));
printf("Middle Element is %d ", findMiddle(ms));
return 0;
}

Implementation of Queue:

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
int data;
class node *next;
class node *prev;
};
class dqueue: public node
{
node *head,*tail;
int top1,top2;
public:
dqueue()
{
top1=0;
top2=0;
head=NULL;
tail=NULL;
}
void push(int x){
   node *temp;
   int ch;
   if(top1+top2 >=5)
   {
   cout <<"dqueue overflow";
   return ;
   }
   if( top1+top2 == 0)
   {
   head = new node;
   head->data=x;
   head->next=NULL;
   head->prev=NULL;
   tail=head;
   top1++;
   }
   else
   {
   cout <<" Add element 1.FIRST 2.LAST enter ur choice:";
   cin >> ch;
if(ch==1)
   {
   top1++;
   temp=new node;
   temp->data=x;
   temp->next=head;
   temp->prev=NULL;
   head->prev=temp;
   head=temp;
   }
   else
   {
   top2++;
   temp=new node;
   temp->data=x;
   temp->next=NULL;
   temp->prev=tail;
   tail->next=temp;
   tail=temp;
   }

   }
}
void pop()
{
int ch;
cout <<"Delete 1.First Node 2.Last Node Enter ur choice:";
cin >>ch;
if(top1 + top2 <=0)
{
cout <<" Dqueue under flow";
return;
}
if(ch==1)
{
head=head->next;
head->prev=NULL;
top1--;
}
else
{
top2--;
tail=tail->prev;
tail->next=NULL;
}
}
void display()
{
int ch;
node *temp;
cout <<"display from 1.Staring 2.Ending Enter ur choice";
cin >>ch;
if(top1+top2 <=0)
{
cout <<"under flow";
return ;
}
if (ch==1)
{
temp=head;
while(temp!=NULL)
{
cout << temp->data <<" ";
temp=temp->next;
}
}
else
{
temp=tail;
while( temp!=NULL)
{
cout <<temp->data << " ";
temp=temp->prev;
}
}
}
};
main()
{
dqueue d1;
int ch;
while (1){
   cout <<"1.INSERT 2.DELETE 3.DISPLAU 4.EXIT Enter ur choice:";
cin >>ch;
switch(ch)
{
case 1: cout <<"enter element";
       cin >> ch;
       d1.push(ch); break;
case 2: d1.pop(); break;
case 3: d1.display(); break;
case 4: exit(1);
}
}}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote