DS Assignment 2 (Class).docx - DataStructures Algorithms...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
5/7/2016 Data- Structures & Algorithms Class Assignment Faisal Ali-Section C
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Q: 1 Implement the following functions of stack using linked list . #include <iostream> using namespace std; class Node {public: int data; Node *next; Node() { data = 0; next = NULL;} }; class Stack {private: Node *top; public: Stack() { top = NULL; } void push(int d) { Node *newNode = new Node(); newNode->data = d; newNode->next = top; top = newNode; }//check either list is empty or not i.e. (head->NULL) bool isEmpty()
Image of page 2
{ return (top == NULL); } int pop() { int data = -1; if (isEmpty()) { cout << "Stack is emprty" << endl; return data; } else { Node *temp = top; top = top->next; data = temp->data; delete temp; } return data; } void display() { if (isEmpty()) { cout << "Stack is emprty" << endl; } if (!isEmpty()) { Node *temp = top; while (temp != NULL) { cout << temp->data << "\n"; temp = temp->next; }}} void peek() { if (isEmpty())
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
{ cout << "Stack is emprty" << endl; } if (!isEmpty()) { Node *temp = top; cout << temp->data << endl; }}}; int main() { Stack *s = new Stack(); int c; int n = 0; do { cout << "\n\n\t\t\t*** STACK ***" << endl; cout << "\n\n\n\t\t1) Push " << endl; cout << "\t\t2) Pop " << endl; cout << "\t\t3) Display " << endl; cout << "\t\t4) Dispaly only Top " << endl; cout << "\t\t5) Exit " << endl; cout << "\n\t Enter your choice : "; cin >> c; switch (c) {case 1: cout << "Insert data :"; cin >> n; s->push(n); break; case 2: s->pop(); break;
Image of page 4
case 3: s->display(); system("pause"); break; case 4: s->peek(); system("pause"); break; case 5: break; default: break; } system("cls"); } while (c < 5); return 0; }
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Q: 2 Implement the following functions of queue using linked list.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern