Stacks
Java
Node for Stack
Node.java
public class Node {
int data;
Node next = null;
public Node(int d) {
data = d;
}
void appendToTail(int d) {
System.out.println("Adding " + d + "...");
Node n = this;
while (n.next != null) {
n = n.next;
}
n.next = new Node(d);
this.printList();
}
Node removeNode(int d) {
System.out.println("Removing " + d + "...");
Node n = this;
if (n.data == d) {
this.printList();
return this.next;
}
while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next;
this.printList();
return this;
}
n = n.next;
}
this.printList();
return null;
}
void printList() {
Node n = this;
while (n.next != null) {
System.out.print(n.data + " ");
n = n.next;
}
System.out.print(n.data);
System.out.println();
System.out.println();
}
}
Structure of Stack
public class Stack {
Node top;
public Stack(int d) {
top = new Node(d);
}
void push(int d) {
System.out.println("Pushing " + d + "...");
if (top == null) {
top.data = d;
}
else {
Node t = new Node(d);
t.next = top;
top = t;
}
printStack();
}
Node pop() {
System.out.println("Popping...");
if (top == null) {
printStack();
return null;
}
int d = top.data;
top = top.next;
printStack();
return new Node(d);
}
void printStack() {
Node n = top;
while (n.next != null) {
System.out.print(n.data + " ");
n = n.next;
}
System.out.print(n.data);
System.out.println();
System.out.println();
}
}
Implementation of Stack
ImplementStack.java
public class ImplementStack {
public static void main(String[] args) {
System.out.println("Initializing Stack with " + 1);
Stack stack = new Stack(1);
stack.printStack();
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.pop();
stack.push(6);
stack.push(7);
stack.pop();
}
}
C
Header File for Node
Node_c.h
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *next;
} Node;
void appendToTail(Node *head, int d);
Node* removeNode(Node *head, int d);
void printList(Node *head);
Node for Stack
Node.c
#include "Node_c.h"
void appendToTail(Node *head, int d) {
printf("Adding %d...\n", d);
Node *n = head;
while (n->next != NULL) {
n = n-> next;
}
Node *newNode = malloc(sizeof(Node));
newNode->data = d;
n->next = newNode;
printList(head);
}
Node* removeNode(Node *head, int d) {
printf("Removing %d...\n", d);
Node *n = head;
if (n->data == d) {
printList(head);
return head->next;
}
while (n->next != NULL) {
if (n->next->data == d) {
n->next = n->next->next;
printList(head);
return head;
}
n = n->next;
}
printList(head);
return NULL;
}
void printList(Node *head) {
Node *n = head;
while (n->next != NULL) {
printf("%d ", n->data);
n = n->next;
}
printf("%d", n->data);
printf("\n\n");
}
Header File for Stack
Stack_c.h
#include "Node_c.h"
void push(Node **top, int d);
Node* pop(Node **top);
void printStack(Node *top);
Structure of Stack
Stack.c
#include "Stack_c.h"
void push(Node **top, int d) {
printf("Pushing %d...\n", d);
if (*top == NULL) {
(*top)->data = d;
}
else {
Node *t = malloc(sizeof(Node));
t->data = d;
t->next = *top;
*top = t;
}
printStack(*top);
}
Node* pop(Node **top) {
printf("Popping...\n");
if (*top == NULL) {
printStack(*top);
return NULL;
}
int d = (*top)->data;
*top = (*top)->next;
printStack(*top);
Node *newNode = malloc(sizeof(Node));
newNode->data = d;
return newNode;
}
void printStack(Node *top) {
Node *n = top;
while (n->next != NULL) {
printf("%d ", n->data);
n = n->next;
}
printf("%d", n->data);
printf("\n\n");
}
Implementation of Stack
ImplementStack.c
#include "Stack_c.h"
int main() {
printf("Initializing Stack with %d\n", 1);
Node *stack = malloc(sizeof(Node));
stack->data = 1;
printStack(stack);
push(&stack, 2);
push(&stack, 3);
push(&stack, 4);
push(&stack, 5);
pop(&stack);
push(&stack, 6);
push(&stack, 7);
pop(&stack);
return 0;
}
C++
Header File for Node
Node.h
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next = NULL;
Node(int d) {
data = d;
}
~Node() {};
void appendToTail(int d);
Node* removeNode(int d);
void printList();
};
Node for Stack
Node.cpp
#include "Node.h"
void Node::appendToTail(int d) {
cout << "Adding " << d << "...\n";
Node *n = this;
while (n->next != NULL) {
n = n->next;
}
n->next = new Node(d);
this->printList();
}
Node* Node::removeNode(int d) {
cout << "Removing " << d << "...\n";
Node *n = this;
if (n->data == d) {
this->printList();
return this->next;
}
while (n->next != NULL) {
if (n->next->data == d) {
n->next = n->next->next;
this->printList();
return this;
}
n = n->next;
}
this->printList();
return NULL;
}
void Node::printList() {
Node *n = this;
while (n->next != NULL) {
cout << n->data << " ";
n = n->next;
}
cout << n->data;
cout << "\n\n";
}
Header File for Stack
Stack.h
#include "Node.h"
#include <iostream>
using namespace std;
class Stack {
private:
Node *top = NULL;
public:
Stack(int d) {
top = new Node(d);
}
~Stack() {};
void push(int d);
Node* pop();
void printStack();
};
Structure of Stack
Stack.cpp
#include "Stack.h"
void Stack::push(int d) {
cout << "Pushing " << d << "...\n";
if (this->top == NULL) {
this->top->data = d;
}
else {
Node *t = new Node(d);
t->next = this->top;
this->top = t;
}
this->printStack();
}
Node* Stack::pop() {
cout << "Popping...\n";
if (this->top == NULL) {
this->printStack();
return NULL;
}
int d = this->top->data;
this->top = this->top->next;
this->printStack();
return new Node(d);
}
void Stack::printStack() {
Node *n = this->top;
while (n->next != NULL) {
cout << n->data << " ";
n = n->next;
}
cout << n->data;
cout << "\n\n";
}
Implementation of Stack
ImplementStack.cpp
#include "Stack.h"
int main() {
cout << "Initializing Stack with " << 1 << "\n";
Stack *stack = new Stack(1);
stack->printStack();
stack->push(2);
stack->push(3);
stack->push(4);
stack->push(5);
stack->pop();
stack->push(6);
stack->push(7);
stack->pop();
return 0;
}
C#
Node for Stack
Node.cs
using System;
public class Node
{
public int data;
public Node next = null;
public Node(int d)
{
data = d;
}
public void appendToTail(int d)
{
Console.WriteLine("Adding " + d + "...");
Node n = this;
while (n.next != null) { }
{
n = n.next;
}
n.next = new Node(d);
this.printList();
}
public Node removeNode(int d)
{
Console.WriteLine("Removing " + d + "...");
Node n = this;
if (n.data == d)
{
this.printList();
return this.next;
}
while (n.next != null)
{
if (n.next.data == d)
{
n.next = n.next.next;
this.printList();
return this;
}
n = n.next;
}
this.printList();
return null;
}
public void printList()
{
Node n = this;
while (n.next != null)
{
Console.WriteLine(n.data + " ");
n = n.next;
}
Console.WriteLine(n.data + "\n\n");
}
}
Structure of Stack
Stack.cs
using System;
public class Stack
{
public Node top;
public Stack(int d)
{
top = new Node(d);
}
public void push(int d)
{
Console.WriteLine("Pushing " + d + "...");
if (top == null)
{
top.data = d;
}
else
{
Node t = new Node(d);
t.next = top;
top = t;
}
printStack();
}
public Node pop()
{
Console.WriteLine("Popping...");
if (top == null)
{
printStack();
return null;
}
int d = top.data;
top = top.next;
printStack();
return new Node(d);
}
public void printStack()
{
Node n = top;
while (n.next != null)
{
Console.WriteLine(n.data + " ");
n = n.next;
}
Console.WriteLine(n.data + "\n\n");
}
}
Implementation of Stack
ImplementStack.cs
using System;
public class ImplementStack
{
public static int Main()
{
Console.WriteLine("Initializing Stack with " + 1);
Stack stack = new Stack(1);
stack.printStack();
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.pop();
stack.push(6);
stack.push(7);
stack.pop();
return 0;
}
}
Python
Node for Stack
Node.py
import sys
class Node:
data = None
next = None
def __init__(self, d):
self.data = d;
def appendToTail(self, d):
print 'Adding ' + str(d) + '...'
n = self
while (n.next != None):
n = n.next
n.next = Node(d)
self.printList()
def removeNode(self, d):
print 'Removing ' + str(d) + '...'
n = self
if (n.data == d):
self.printList()
return self.next
while (n.next != None):
if (n.next.data == d):
n.next = n.next.next
self.printList()
return self
n = n.next
self.printList()
return None
def printList(self):
n = self
while (n.next != None):
sys.stdout.write(str(n.data) + ' ')
n = n.next
sys.stdout.write(str(n.data))
print '\n\n'
Structure of Stack
Stack.py
import Node
import sys
class Stack:
top = None
def __init__(self, d):
self.top = Node.Node(d)
def push(self, d):
print 'Pushing ' + str(d) + '...'
if (self.top == None):
self.top.data = d
else:
t = Node.Node(d)
t.next = self.top
self.top = t
self.printStack()
def pop(self):
print 'Popping...'
if (self.top == None):
printStack()
return None
d = self.top.data
self.top = self.top.next
self.printStack()
return Node.Node(d)
def printStack(self):
n = self.top
while (n.next != None):
sys.stdout.write(str(n.data) + ' ')
n = n.next
sys.stdout.write(str(n.data) + ' ')
print '\n\n'
Implementation of Stack
ImplementStack.py
import Stack
print 'Initializing Stack with ' + str(1)
stack = Stack.Stack(1)
stack.printStack()
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
stack.pop()
stack.push(6)
stack.push(7)
stack.pop()
Console Output
ImplementList.cpp
Initializing Stack with 1
1
Pushing 2...
2 1
Pushing 3...
3 2 1
Pushing 4...
4 3 2 1
Pushing 5...
5 4 3 2 1
Popping...
4 3 2 1
Pushing 6...
6 4 3 2 1
Pushing 7...
7 6 4 3 2 1
Popping...
6 4 3 2 1