Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented in cpp for 3 exercises #2054

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions Exercise_1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <bits/stdc++.h>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

#define MAX 1000
Expand All @@ -12,7 +12,9 @@ class Stack {
public:
int a[MAX]; // Maximum size of Stack

Stack() { //Constructor here }
Stack() {
top = -1;
}
bool push(int x);
int pop();
int peek();
Expand All @@ -21,24 +23,36 @@ class Stack {

bool Stack::push(int x)
{
//Your code here
//Check Stack overflow as well
top++;
a[top]=x;
return true;
}

int Stack::pop()
{
//Your code here
//Check Stack Underflow as well
if(top==-1){
std::cout<<"Stack Underflow";
return 0;
}
int topElement=a[top];
top--;
return topElement;
}
int Stack::peek()
{
//Your code here
//Check empty condition too
if(top==-1){
cout<<"Stack is empty";
return 0;
}
return a[top];
}

bool Stack::isEmpty()
{
//Your code here
return top==-1;
}

// Driver program to test above functions
Expand Down
17 changes: 17 additions & 0 deletions Exercise_2.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

// A structure to represent a stack
Expand All @@ -19,21 +20,37 @@ StackNode* newNode(int data)
int isEmpty(StackNode* root)
{
//Your code here
return root==NULL;
}

void push(StackNode** root, int data)
{
//Your code here
StackNode* node = newNode(data);
node->next=*root;
*root=node;
}

int pop(StackNode** root)
{
//Your code here
if(isEmpty(*root)){
cout<<"Stack is empty";
return 0;
}
int val = (*root)->data;
*root=(*root)->next;
return val;
}

int peek(StackNode* root)
{
//Your code here
if(isEmpty(root)){
cout<<"Stack is empty";
return 0;
}
return root->data;
}

int main()
Expand Down
36 changes: 35 additions & 1 deletion Exercise_3.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

// A linked list node (changes)
Expand All @@ -21,6 +22,12 @@ void push(Node** head_ref, int new_data)
/* 3. Make next of new node as head */

/* 4. move the head to point to the new node */

Node* newNode = new Node();
newNode->data = new_data;
newNode->next = (*head_ref);
(*head_ref) = newNode;

}

/* Given a node prev_node, insert a new node after the given
Expand All @@ -35,7 +42,16 @@ void insertAfter(Node* prev_node, int new_data)

/* 4. Make next of new node as next of prev_node */

/* 5. move the next of prev_node as new_node */
/* 5. move the next of prev_node as new_node */
if(prev_node==NULL){
cout<<"Previous node cannot be null";
return;
}
Node* newNode = new Node();
newNode->data = new_data;

newNode->next = prev_node->next;
prev_node->next = newNode;
}

/* Given a reference (pointer to pointer) to the head
Expand All @@ -56,13 +72,31 @@ void append(Node** head_ref, int new_data)
/* 5. Else traverse till the last node */

/* 6. Change the next of last node */
Node* newNode = new Node();
newNode->data = new_data;
newNode->next = NULL;
if(*head_ref==NULL){
(*head_ref) = newNode;
return;
}
Node* ptr = *head_ref;
while(ptr->next){
ptr = ptr->next;
}
ptr->next = newNode;

}

// This function prints contents of
// linked list starting from head
void printList(Node *node)
{
//Your code here
Node* ptr = node;
while(ptr){
cout<<ptr->data<<" ";
ptr = ptr->next;
}
}

/* Driver code*/
Expand Down
Binary file added test_program
Binary file not shown.
Binary file added test_program_2
Binary file not shown.
Binary file added test_program_3
Binary file not shown.