Lists
This is an implementation of a singly-linked list. A doubly-linked list would follow the same format except that it would incorporate a previous node in addition to the next node.
Java
Node for List
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();
}
}
Implementation of List
ImplementList.java
public class ImplementList {
public static void main(String[] args) {
System.out.println("Initializing Node with " + 1);
Node list = new Node(1);
list.printList();
list.appendToTail(2);
list.appendToTail(3);
list.appendToTail(4);
list.appendToTail(5);
list.removeNode(3);
list.appendToTail(6);
list.removeNode(4);
list.appendToTail(7);
list.appendToTail(8);
list.appendToTail(6);
}
}
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 List
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");
}
Implementation of List
ImplementList.c
#include "Node_c.h"
int main() {
printf("Initializing Node with %d\n", 1);
Node *list = malloc(sizeof(Node));
list->data = 1;
printList(list);
appendToTail(list, 2);
appendToTail(list, 3);
appendToTail(list, 4);
appendToTail(list, 5);
removeNode(list, 3);
appendToTail(list, 6);
removeNode(list, 4);
appendToTail(list, 7);
appendToTail(list, 8);
appendToTail(list, 6);
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 List
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";
}
Implementation of List
ImplementList.cpp
#include "Node.h"
int main() {
cout << "Initializing Node with " << 1 << "\n";
Node *list = new Node(1);
list->printList();
list->appendToTail(2);
list->appendToTail(3);
list->appendToTail(4);
list->appendToTail(5);
list->removeNode(3);
list->appendToTail(6);
list->removeNode(4);
list->appendToTail(7);
list->appendToTail(8);
list->appendToTail(6);
return 0;
}
C#
Node for List
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");
}
}
Implementation of List
ImplementList.cs
using System;
public class ImplementList
{
public static int Main() {
Console.WriteLine("Initializing Node with " + 1);
Node list = new Node(1);
list.printList();
list.appendToTail(2);
list.appendToTail(3);
list.appendToTail(4);
list.appendToTail(5);
list.removeNode(3);
list.appendToTail(6);
list.removeNode(4);
list.appendToTail(7);
list.appendToTail(8);
list.appendToTail(6);
return 0;
}
}
Python
Node for List
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'
Implementation of List
ImplementList.py
import Node
print 'Initializing Node with ' + str(1)
list = Node.Node(1)
list.printList()
list.appendToTail(2)
list.appendToTail(3)
list.appendToTail(4)
list.appendToTail(5)
list.removeNode(3)
list.appendToTail(6)
list.removeNode(4)
list.appendToTail(7)
list.appendToTail(8)
list.appendToTail(6)
Console Output
ImplementList.cpp
Initializing Node with 1
1
Adding 2...
1 2
Adding 3...
1 2 3
Adding 4...
1 2 3 4
Adding 5...
1 2 3 4 5
Removing 3...
1 2 4 5
Adding 6...
1 2 4 5 6
Removing 4...
1 2 5 6
Adding 7...
1 2 5 6 7
Adding 8...
1 2 5 6 7 8
Adding 6...
1 2 5 6 7 8 6