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

Completed PreCourse-1 #1988

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
155 changes: 101 additions & 54 deletions Exercise_1.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,101 @@
#include <bits/stdc++.h>

using namespace std;

#define MAX 1000

class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
int top;

public:
int a[MAX]; // Maximum size of Stack

Stack() { //Constructor here }
bool push(int x);
int pop();
int peek();
bool isEmpty();
};

bool Stack::push(int x)
{
//Your code here
//Check Stack overflow as well
}

int Stack::pop()
{
//Your code here
//Check Stack Underflow as well
}
int Stack::peek()
{
//Your code here
//Check empty condition too
}

bool Stack::isEmpty()
{
//Your code here
}

// Driver program to test above functions
int main()
{
class Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " Popped from stack\n";

return 0;
}
#include <bits/stdc++.h>

using namespace std;

#define MAX 1000

/*
Time Complexity :
push(): O(1)
pop(): O(1)
peek(): O(1)
isEmpty(): O(1)

Space Complexity :
stack: O(n), n is number of elements in stack

Did this code successfully run on Leetcode : Couldn't find the leetcode problem

Any problem you faced while coding this : None
*/
class Stack
{
int top;

public:
int a[MAX]; // Maximum size of Stack

Stack()
{
top = -1;
}
bool push(int x);
int pop();
int peek();
bool isEmpty();
};

// Push data onto the stack
bool Stack::push(int x)
{
if (top >= MAX)
{
return false;
}

a[++top] = x;

return true;
}

// Pop data from the stack
int Stack::pop()
{
if (isEmpty())
{
return -1;
}
else
{
return a[top--];
}
}

// Peek the top element of the stack
int Stack::peek()
{
if (isEmpty())
{
return -1;
}

return a[top];
}

// Check if the stack is empty
bool Stack::isEmpty()
{
if (top < 0)
{
return true;
}

return false;
}

// Driver program to test above functions
int main()
{
class Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.peek() << " Peeked from stack\n";
cout << s.pop() << " Popped from stack\n";
cout << s.peek() << " Peeked from stack\n";
cout << s.pop() << " Popped from stack\n";
cout << s.peek() << " Peeked from stack\n";
cout << s.pop() << " Popped from stack\n";

return 0;
}
152 changes: 100 additions & 52 deletions Exercise_2.cpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,100 @@
#include <bits/stdc++.h>
using namespace std;

// A structure to represent a stack
class StackNode {
public:
int data;
StackNode* next;
};

StackNode* newNode(int data)
{
StackNode* stackNode = new StackNode();
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}

int isEmpty(StackNode* root)
{
//Your code here
}

void push(StackNode** root, int data)
{
//Your code here
}

int pop(StackNode** root)
{
//Your code here
}

int peek(StackNode* root)
{
//Your code here
}

int main()
{
StackNode* root = NULL;

push(&root, 10);
push(&root, 20);
push(&root, 30);

cout << pop(&root) << " popped from stack\n";

cout << "Top element is " << peek(root) << endl;

return 0;
}
#include <bits/stdc++.h>
using namespace std;

/*
Time Complexity :
push(): O(1)
pop(): O(1)
peek(): O(1)
isEmpty(): O(1)

Space Complexity :
stack: O(n), n is number of elements in the stack

Did this code successfully run on Leetcode : Couldn't find the leetcode problem

Any problem you faced while coding this : None
*/

// A structure to represent a stack
class StackNode
{
public:
int data;
StackNode *next;
};

StackNode *newNode(int data)
{
StackNode *stackNode = new StackNode();
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}

// Check if the stack is empty
int isEmpty(StackNode *root)
{
return root == NULL;
}

// Push data onto the stack
void push(StackNode **root, int data)
{
StackNode *node = newNode(data);

node->next = *root;

*root = node;
}

// Pop data from the stack
int pop(StackNode **root)
{
if (isEmpty(*root))
{
return -1;
}

StackNode *node = *root;

*root = (*root)->next;

int popped = node->data;

delete node; // Free memory to prevent memory leaks

return popped;
}

// Peek the top element of the stack
int peek(StackNode *root)
{
if (isEmpty(root))
{
return -1;
}

return root->data;
}

int main()
{
StackNode *root = NULL;

push(&root, 10);
push(&root, 20);
push(&root, 30);

cout << pop(&root) << " popped from stack\n";

cout << "Top element is " << peek(root) << endl;

cout << pop(&root) << " popped from stack\n";

cout << "Top element is " << peek(root) << endl;

cout << pop(&root) << " popped from stack\n";

return 0;
}
Loading