This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Chapter 2 | |
* 2.3 Implement an algotithm to delete a node in the middle of a singly linked list. | |
* Given only get access to that node. | |
* EXAMPLE | |
* Input: the node c from the linked list a->b->c->d->e | |
* Result: nothing is returned, but the new linked list looks like a->b->d->e | |
*/ | |
#include <iostream> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Chapter 2 | |
* | |
* 2.4 Write a code to partition a linked list around a value x, | |
* such that all nodes less than x come before all nodes greater than or equal to x | |
*/ | |
#include <iostream> | |
using namespace std; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Chapter 2 | |
* | |
* 2.5 You have two numbers represented by a linked list, where each node contains | |
* a single digit. The digits are stored in reverse order,such that the 1's digit | |
* is at the head of the list. Write a function that adds the two numbers and returns | |
* the sum as a linked list. | |
* FOLLOW UP: | |
* Suppose the digits are stored in forward order. Repeat the above problem. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Chapter 2 | |
* | |
* 2.6 Given a circular linked list, implement an algorithm which returns | |
* the node at the beginning of the loop. | |
* Input: A -> B -> C -> D -> E -> C (The same C as before, not a new node) | |
* Output: C | |
*/ | |
#include <iostream> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Chapter 2 | |
* | |
* 2.7 Implement a function to check if a linked list is a palindrome. | |
* | |
*/ | |
#include <iostream> | |
#include <stack> | |
using namespace std; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Chapter 3 | |
* | |
* 3.1 Describe hwo you could use a single array to implement three stacks. | |
* | |
*/ | |
#include <iostream> | |
using namespace std; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Chapter 3 | |
* | |
* 3.2 How could you design a stack which, in addition to push and pop, also | |
* has a function min which returns the minimum element? Push, pop and min | |
* should all operation in O(1) time. | |
* | |
*/ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Chapter 3 | |
* | |
* 3.3 Implement a data structure SetOfStacks. | |
* SetOfStacks should be composed of several stacks and should | |
* creat new stack once that previous one exeeds capacity. | |
* SetOfStack.push() and SetOfStack.pop() should behave identically to a single stack. | |
* FOLLOW UP | |
* Implement a function popAt(int index) which performs a pop operation on a specific sub-stack. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Chapter 3 | |
* | |
* 3.6 Write a program to sort a stack in ascending order(with biggest on top) | |
* You may use at most one additional stack to hold items, but you may not | |
* copy the elements into any other data structure. The stack supports the | |
* following operation: push, pop, peek, isEmpty | |
*/ | |
/* This sort algorithm supports duplicated elements */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Chapter 3 | |
* | |
* 3.5 Implement a MyQueue class with implements a queue | |
* using two stacks. | |
* | |
*/ | |
#include <iostream> | |
#include <stack> |