Skip to content

Commit

Permalink
add two methods and test case
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin committed Apr 15, 2017
1 parent 36e6302 commit 4929c5b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
82 changes: 76 additions & 6 deletions LinkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export class Node<T> {
// single link list
export class List<T> {
public head: Node<T>;
public listSize: number;
constructor() {
this.head = new Node(null); // empty head
this.listSize = 0;
}

// find one Node with data elem, if not found ,return null
Expand Down Expand Up @@ -56,6 +58,7 @@ export class List<T> {
newNode.next = findNode.next;
findNode.next = newNode;
}
this.listSize++;
}

// find the last node
Expand All @@ -78,6 +81,7 @@ export class List<T> {
let node = new Node(elem);
let lastNode = this.findLastNode();
lastNode.next = node;
this.listSize++;
}

// remove an element , if success ,return true,eles false
Expand All @@ -88,6 +92,7 @@ export class List<T> {
if(compare(cur.data, elem)) { // find the node
pre.next = cur.next;
cur = null; // delete the node
this.listSize--;
return true;
}
pre = cur;
Expand All @@ -96,6 +101,33 @@ export class List<T> {
return false;
}

// advance n node from current node
// if curr is null, the from the head advance n node
// else from the current node
// if n great the distance between currnode and the tail node,
// then return the tail node ,else return Nth node after the curr.
public advance(n:number, curr?:Node<T>):Node<T> {
if(this.listSize == 0) {
return null;
}
let cnt = 0;
curr = this.head.next;
while(curr) {
cnt++;
if(curr.next == null) {
return curr;
}
curr = curr.next;
if(cnt == n) {
return curr;
}
}
return null;
}

public count():number {
return this.listSize;
}
// travse the list
public display() {
let node = this.head.next;
Expand All @@ -110,11 +142,11 @@ export class List<T> {
// double linked list
export class DLList<T> {
private head: Node<T>;
// private tail: Node<T>;
private listSize:number;

constructor() {
this.head = new Node<T>(null);
// this.tail = new Node<T>(null);
this.listSize = 0;
}

public find(elem:T):Node<T> {
Expand Down Expand Up @@ -158,6 +190,7 @@ export class DLList<T> {
newNode.prev = findNode;
}
}
this.listSize++;
}

public findLastNode(): Node<T> {
Expand All @@ -179,6 +212,7 @@ export class DLList<T> {
let lastNode = this.findLastNode();
lastNode.next = node;
node.prev = lastNode;
this.listSize++;
}

public remove(elem:T):boolean {
Expand All @@ -193,6 +227,7 @@ export class DLList<T> {
cur.next.prev = pre;
}
cur = null; // delete the node
this.listSize--;
return true;
}
pre = cur;
Expand All @@ -201,16 +236,45 @@ export class DLList<T> {
return false;
}

// back n node from current node
// if curr is null, then return the head node
// else from the current node back to head
// if n great the distance between currnode and the head node,
// then return the head node ,else return Nth node before the curr.
public back(n:number, curr?:Node<T>):Node<T> {
if(this.listSize == 0 || curr == undefined) {
return this.head;
}

let cnt = 0;
while(curr) {
cnt++;
if(curr.prev == null) {
return curr;
}
curr = curr.prev;
if(cnt == n) {
return curr;
}
}
return null;
}

public count():number {
return this.listSize;
}

public display() {
console.log("display the list: ");
let node = this.head.next;
console.log("display the list: ")
while(node) {
console.log(node.data);
node = node.next;
}
}

public dispReverse() {
console.log("reverse display the list: ");
let last = this.findLastNode();
while(last.prev != null) {
console.log(last.data);
Expand All @@ -222,10 +286,13 @@ export class DLList<T> {

// circle linked list
export class CLList<T> {
public head: Node<T>;
private head: Node<T>;
private listSize: number;

constructor() {
this.head = new Node(null); // empty head
this.head.next = this.head;
this.listSize = 0;
}

public find(elem:T):Node<T> {
Expand Down Expand Up @@ -262,6 +329,7 @@ export class CLList<T> {
newNode.next = findNode.next;
findNode.next = newNode;
}
this.listSize++;
}

public findLastNode(): Node<T> {
Expand All @@ -277,9 +345,9 @@ export class CLList<T> {
public insertTail(elem:T) {
let node = new Node(elem);
let lastNode = this.findLastNode();
console.log(lastNode);
node.next = this.head;
lastNode.next = node;
this.listSize++;
}

public remove(elem:T):boolean {
Expand All @@ -289,6 +357,7 @@ export class CLList<T> {
if(compare(cur.data, elem)) { // find the node
pre.next = cur.next;
cur = null; // delete the node
this.listSize--;
return true;
}
pre = cur;
Expand All @@ -298,8 +367,9 @@ export class CLList<T> {
}

public display() {
console.log("display the circle list: ");

let node = this.head.next;
console.log("display the list: ")
while(node != this.head) {
console.log(node.data);
node = node.next;
Expand Down
15 changes: 14 additions & 1 deletion test_LList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ sl.display();
sl.insert(d);
sl.display();

prt(" advance");
let node = sl.find(a);
prt(sl.advance(2,node));
prt(sl.advance(3));
prt(sl.count());

prt("---- remove ")
sl.remove(b);
sl.display();

sl.remove(a);
sl.display();

prt(sl.count());

prt("------ DLList Test -----");

Expand All @@ -53,6 +60,12 @@ prt("-- insert tail");
dl.insertTail(d);
dl.display(); // a c b d

prt("--- back");
let nd = dl.find(d);
// prt(nd);
prt(dl.back(2,nd));
prt(dl.back(4));

prt("--- remove");
dl.remove(c);
dl.dispReverse();
Expand Down

0 comments on commit 4929c5b

Please sign in to comment.