Question

In: Computer Science

Hi I would like to see an example in c++ of Stack As Linked List I...

Hi I would like to see an example in c++
of Stack As Linked List 
I need a seek() function which receives a double number X 
and which returns in which position in the stack is X.
If the value X is not in the stack, the function should return -1

Solutions

Expert Solution

Code for the function seek() only:

int seek(double x){
    struct Node *ptr;
    if(top == NULL)
        return -1;//if stack is empty return -1
    ptr = top;
    int position = 1;//we consider the top position to be the first position
    while(ptr != NULL){
        if(ptr->data == x)
            return position;
        else{ 
            ptr = ptr->next;
            position++;//update position as we move forward in the list
        }
    }
    return -1;
}

Full working code:

#include <iostream>
using namespace std;
struct Node {//node definition
   double data;
   struct Node *next;
};
struct Node* top = NULL;
void push(double val) {//pushes the new node
   struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
   newnode->data = val;
   newnode->next = top;
   top = newnode;
}
void pop() {//pops the top element from the stack
   if(top==NULL)
   cout<<"Stack Underflow"<<endl;
   else {
      cout<<"The popped element is "<< top->data <<endl;
      top = top->next;
   }
}

int seek(double x){
    struct Node *ptr;
    if(top == NULL)
        return -1;//if stack is empty return -1
    ptr = top;
    int position = 1;//we consider the top position to be the first position
    while(ptr != NULL){
        if(ptr->data == x)
            return position;
        else{ 
            ptr = ptr->next;
            position++;//update position as we move forward in the list
        }
    }
    return -1;
}

void display() {
   struct Node* ptr;
   if(top==NULL)
   cout<<"stack is empty";
   else {
      ptr = top;
      cout<<"Stack elements are: ";
      while (ptr != NULL) {
         cout<< ptr->data <<" ";
         ptr = ptr->next;
      }
   }
   cout<<endl;
}
int main() {
   int ch;
   double val;
   cout<<"1) Push in stack"<<endl;
   cout<<"2) Pop from stack"<<endl;
   cout<<"3) Display stack"<<endl;
   cout<<"4) Seek function"<<endl;
   cout<<"5) Exit"<<endl;
   do {
      cout<<"Enter choice: "<<endl;
      cin>>ch;
      switch(ch) {
         case 1: {
            cout<<"Enter value to be pushed:"<<endl;
            cin>>val;
            push(val);
            break;
         }
         case 2: {
            pop();
            break;
         }
         case 3: {
            display();
            break;
         }
         case 4: {
             cout<<"Enter value : ";
             double x;
             cin>>x;
             int position = seek(x);
             cout<<"The postion is "<<position<<endl;
             break;
         }
         case 5: {
            cout<<"Exit"<<endl;
            break;
         }
         default: {
            cout<<"Invalid Choice"<<endl;
         }
      }
   }while(ch!=5);
   return 0;
}

output:

code screenshot:


Related Solutions

Hi, I would like to test a java program. I am learning linked list and going...
Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes. For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents and add 15 to the list again and display the list contents and delete 13 from the list and display the list contents and lastly delete 12 from the list and display the list...
c++ example of a double linked list of chars I need to create a double linked...
c++ example of a double linked list of chars I need to create a double linked list of chars. this should be a class that allows you to input a single character at a time, list the resulting characters, find any character and delete the first example of a character.
I want to know 'Linked link + queue' and 'Linked link Stack' code in C++ But...
I want to know 'Linked link + queue' and 'Linked link Stack' code in C++ But there is a condition. There shall be the following 'menu': *****Menu***** 1. Insert into Stack 2. Insert into Queue 3. Delete from Stack 4. Delete from Queue 5. View Content in Stack 6. View Content in Queue ********************************************** Let me give you a example. >> 1 30 // Insert 30 into Stack >>1 58// Insert 58 into Stack >>1 26 // Insert 26 into...
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list...
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list for employees, the data stored is ·An employee number (a positive integer) ·A yearly salary (a float). ·Number of dependents (a short positive integer) The employer would like you as the programmer to design and implement a linked list using classes. For each class two files are needed, one to define the class, the other to implement the methods. In addition, the client uses...
Hi! I 'm writing a code for a doubly linked list and this is the header...
Hi! I 'm writing a code for a doubly linked list and this is the header file #include<iostream> #include <string> using namespace std; struct node { int data; node *next,*prev; node(int d,node *p=0,node *n=0) { data=d; prev=p; next=n; } }; class list { node *head,*tail; public: list(); bool is_empty(); int size(); void print(); void search(); int search2(int el); void add_last(int el); void add_first(int el); bool add_pos(); bool delete_first(); bool delete_last(); void delete_pos(int pos); void delete_el(); void add_sorted(); }; i want...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT