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

complete Precourse-1 #2006

Open
wants to merge 1 commit 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
35 changes: 33 additions & 2 deletions Exercise_1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ using namespace std;

class Stack {
//Please read sample.java file before starting.
// firstly we need to decide which data structure to go with
// if they have allowed us to use the stack our first choice is stackl
// if not allowed our second choice is arraylist

// Time complexity of each operation in this
// push , pop,peek,isempty as we are using single operation to add value to stack time complexity is O(1)
// and the maximum capacity to store the is already define 1000
//Kindly include Time and Space complexity at top of each file
int top;

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

Stack() { //Constructor here }
Stack() { //Constructor here
// we need to start with index -1
top = -1;
}
bool push(int x);
int pop();
int peek();
Expand All @@ -23,22 +33,43 @@ bool Stack::push(int x)
{
//Your code here
//Check Stack overflow as well
if(top >= MAX-1)
{
cout << "stack overflow" << endl;
return false;
}

// now we need to store each element using the array
a[++top] = x;

return true;

}

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

return a[top];
}

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

// Driver program to test above functions
Expand Down
43 changes: 34 additions & 9 deletions Exercise_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,74 @@
using namespace std;

// A structure to represent a stack

// if we want to design the stack using the linked list we need to keep track of previous node for the tracking
// again as we are doing all the operation in one go the time complexity is O(1) for all the operations

class StackNode {
public:
int data;
StackNode* next;
};

StackNode* newNode(int data)
{
StackNode* stackNode = new StackNode();
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}
StackNode *head;
int isEmpty(StackNode* root)
{
//Your code here
return head==NULL;
}

void push(StackNode** root, int data)
void push(StackNode* root, int data)
{
//Your code here
// base case would first node we need to check
if(head == NULL)
{
head = newNode(data);
}
else
{
root->next = head;
head = root;
}
}

int pop(StackNode** root)
int pop(StackNode* root)
{
//Your code here
if(head == NULL)
return -1;

int x = head->data;
head = head->next;

return x;
}

int peek(StackNode* root)
{
//Your code here
if(head==NULL)
return -1;
//Your code here
return head->data;
}

int main()
{
StackNode* root = NULL;

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

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

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

Expand Down
47 changes: 33 additions & 14 deletions Exercise_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,81 @@ class Node
public:
int data;
Node *next;

Node(int new_data){
data = new_data;
next = NULL;
}
};
Node *head;
/* Given a reference (pointer to pointer)
to the head of a list and an int, inserts
a new node on the front of the list. */
void push(Node** head_ref, int new_data)
{
/* 1. allocate node */

/* 2. put in the data */

Node *new_node = new Node(new_data);
/* 3. Make next of new node as head */

new_node->next = *head_ref;
/* 4. move the head to point to the new node */
*head_ref = new_node;
}

/* Given a node prev_node, insert a new node after the given
prev_node */
void insertAfter(Node* prev_node, int new_data)
{
/*1. check if the given prev_node is NULL */

if(prev_node == NULL)
{
return;
}
/* 2. allocate new node */

/* 3. put in the data */

Node *new_node = new Node(new_data);
/* 4. Make next of new node as next of prev_node */

/* 5. move the next of prev_node as new_node */
new_node->next = prev_node->next;
/* 5. move the next of prev_node as new_node */
prev_node->next = new_node;
}

/* Given a reference (pointer to pointer) to the head
of a list and an int, appends a new node at the end */
void append(Node** head_ref, int new_data)
{
/* 1. allocate node */

/* 2. put in the data */

/* 3. This new node is going to be
the last node, so make next of
it as NULL*/

Node *new_node = new Node(new_data);
/* 4. If the Linked List is empty,
then make the new node as head */

if(*head_ref==NULL)
{
*head_ref = new_node;
return;
}

/* 5. Else traverse till the last node */

Node *curr = *head_ref;
while(curr->next != NULL)
curr= curr->next;
/* 6. Change the next of last node */
curr->next = new_node;
}

// This function prints contents of
// linked list starting from head
void printList(Node *node)
{
//Your code here
while(node != NULL)
{
printf("%d ",node->data);
node = node->next;
}
}

/* Driver code*/
Expand Down