Last active
June 19, 2016 11:14
-
-
Save closer27/5e9d16e1a23861776c4e to your computer and use it in GitHub Desktop.
Basic LinkedList
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
public class LinkedList { | |
private Node head; | |
public LinkedList () { | |
} | |
public void addNode (Node newNode) { | |
if (head == null) { | |
head = newNode; | |
return; | |
} else { | |
Node node = head; | |
while (node.getNext() != null) { | |
node = node.getNext(); | |
} | |
node.setNext(newNode); | |
} | |
} | |
public void removeNode (int orderNum) { | |
if (head == null) { | |
System.out.println("the list is empty"); | |
return; | |
} | |
if (orderNum == 0) { | |
Node deleteNode = head; | |
head = deleteNode.getNext(); | |
System.out.println("deleteNode : " + deleteNode.getValue()); | |
deleteNode = null; | |
return; | |
} else if (orderNum == lengthOfList()-1) { | |
Node node = head; | |
while (node.getNext() != null) { | |
node = node.getNext(); | |
} | |
System.out.println("deleteNode : " + node.getValue()); | |
node = null; | |
return; | |
} else { | |
Node node = head; | |
int count = 0; | |
Node previousNode = null; | |
while (count < orderNum) { | |
if (count == orderNum -1) { | |
previousNode = node; | |
} | |
node = node.getNext(); | |
count++; | |
} | |
previousNode.setNext(node.getNext()); | |
System.out.println("deleteNode : " + node.getValue()); | |
node = null; | |
return; | |
} | |
} | |
public void printAllValue () { | |
Node node = head; | |
while (node != null) { | |
System.out.println(node.getValue()); | |
node = node.getNext(); | |
} | |
} | |
public int lengthOfList () { | |
int length = 0; | |
Node node = head; | |
while (node != null) { | |
length++; | |
node = node.getNext(); | |
} | |
return length; | |
} | |
public static void main (String args[]) { | |
LinkedList linkedList = new LinkedList(); | |
linkedList.addNode(new Node(1)); | |
linkedList.addNode(new Node(4)); | |
linkedList.addNode(new Node(3)); | |
linkedList.addNode(new Node(6)); | |
linkedList.addNode(new Node(23)); | |
linkedList.addNode(new Node(11)); | |
linkedList.printAllValue(); | |
System.out.println("length : " + linkedList.lengthOfList()); | |
linkedList.removeNode(3); | |
System.out.println("length : " + linkedList.lengthOfList()); | |
linkedList.removeNode(0); | |
System.out.println("length : " + linkedList.lengthOfList()); | |
linkedList.removeNode(linkedList.lengthOfList()-1); | |
} | |
} | |
class Node { | |
private int value; | |
private Node next; | |
public Node (int value) { | |
this.value = value; | |
this.next = null; | |
} | |
public void setValue (int value) { | |
this.value = value; | |
} | |
public int getValue () { | |
return this.value; | |
} | |
public void setNext (Node node) { | |
this.next = node; | |
} | |
public Node getNext () { | |
return this.next; | |
} | |
} |
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
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct BinaryTreeNode { | |
int data; | |
struct BinaryTreeNode *leftChild; | |
struct BinaryTreeNode *rightChild; | |
} BinaryTreeNode; | |
void PreOrder(struct BinaryTreeNode *root) { | |
if (root) { | |
printf("%d\n", root->data); | |
PreOrder(root->leftChild); | |
PreOrder(root->rightChild); | |
} | |
} | |
void PostOrder(struct BinaryTreeNode *root) { | |
if (root) { | |
PreOrder(root->leftChild); | |
PreOrder(root->rightChild); | |
printf("%d\n", root->data); | |
} | |
} | |
void insertNode(struct BinaryTreeNode *root, struct BinaryTreeNode *node) { | |
} | |
int findMax(struct BinaryTreeNode *root) { | |
int temp = root->data; | |
while(1) { | |
} | |
return temp; | |
} | |
int main() { | |
struct BinaryTreeNode *root = malloc(sizeof(struct BinaryTreeNode)); | |
root->data = 1; | |
root->leftChild = malloc(sizeof(struct BinaryTreeNode)); | |
root->leftChild->data = 2; | |
root->leftChild->leftChild = malloc(sizeof(struct BinaryTreeNode)); | |
root->leftChild->leftChild->data = 4; | |
root->leftChild->leftChild->leftChild = NULL; | |
root->leftChild->leftChild->rightChild = NULL; | |
root->leftChild->rightChild = malloc(sizeof(struct BinaryTreeNode)); | |
root->leftChild->rightChild->data = 5; | |
root->leftChild->rightChild->leftChild = NULL; | |
root->leftChild->rightChild->rightChild = NULL; | |
root->rightChild = malloc(sizeof(struct BinaryTreeNode)); | |
root->rightChild->data = 3; | |
root->rightChild->leftChild = malloc(sizeof(struct BinaryTreeNode)); | |
root->rightChild->leftChild->data = 6; | |
root->rightChild->leftChild->leftChild = NULL; | |
root->rightChild->leftChild->rightChild = NULL; | |
root->rightChild->rightChild = malloc(sizeof(struct BinaryTreeNode)); | |
root->rightChild->rightChild->data = 7; | |
root->rightChild->rightChild->leftChild = NULL; | |
root->rightChild->rightChild->rightChild = NULL; | |
if (root == NULL) | |
printf("error\n"); | |
else { | |
printf("success\n"); | |
PostOrder(root); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment