DS Assignment 2 (Class).docx

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

• Homework Help
• 17

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

5/7/2016 Data- Structures & Algorithms Class Assignment Faisal Ali-Section C

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

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()
{ 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())

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

{ 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;
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; }

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

Q: 2 Implement the following functions of queue using linked list.
This is the end of the preview. Sign up to access the rest of the document.
• Fall '17
• Beenish

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern