Skip to content

Commit

Permalink
uodate more notes
Browse files Browse the repository at this point in the history
  • Loading branch information
kekeandzeyu committed Feb 9, 2025
1 parent 7492215 commit 465c996
Showing 1 changed file with 75 additions and 23 deletions.
98 changes: 75 additions & 23 deletions src/pages/notes/data-structures-and-algorithms-2.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,59 +21,111 @@ Singly linked lists contain nodes which have a 'value' field as well as 'next' f
<Tabs>
<TabItem title="Java">

```java frame="terminal" collapse={1-68}
```java frame="terminal" collapse={1-120}
// SLList.java
import java.util.Iterator;

public class SLList implements Iterable<Integer> {
public static class IntNode {
public int item;
public IntNode next;
public class SLList<T> implements Iterable<T> {

public IntNode(int i, IntNode n) {
/**
* Node class for SLList
*/
public static class Node<T> {
public T item;
public Node<T> next;

public Node(T i, Node<T> n) {
item = i;
next = n;
}
}

private final IntNode sentinel;
private final Node<T> sentinel;
private int size;

/**
* Default Constructor for SLList
*/
public SLList() {
sentinel = new IntNode(63, null);
sentinel = new Node<>(null, null);
size = 0;
}

public SLList(int x) {
sentinel = new IntNode(63, null);
sentinel.next = new IntNode(x, null);
/**
* Constructor for SLList with one element
*
* @param x element to be added
*/
public SLList(T x) {
sentinel = new Node<>(null, null);
sentinel.next = new Node<>(x, null);
size = 1;
}

public void addFirst(int x) {
sentinel.next = new IntNode(x, sentinel.next);
/**
* Add element to the front of the list
*
* @param x element to be added
*/
public void addFirst(T x) {
sentinel.next = new Node<>(x, sentinel.next);
size += 1;
}

public void addLast(int x) {
/**
* Add element to the end of the list
*
* @param x element to be added
*/
public void addLast(T x) {
size += 1;
IntNode p = sentinel;
Node<T> p = sentinel;
while (p.next != null) {
p = p.next;
}
p.next = new IntNode(x, null);
p.next = new Node<>(x, null);
}


/**
* Get the first element of the list
*
* @return first element of the list
*/
public T getFirst() {
return sentinel.next.item;
}

/**
* Get the last element of the list
*
* @return last element of the list
*/
public T getLast() {
Node<T> p = sentinel;
while (p.next != null) {
p = p.next;
}
return p.item;
}

/**
* Return the size of the list
*
* @return size of the list
*/
public int size() {
return size;
}

public Iterator<Integer> iterator() {
/**
* Iterator implementation for SLList
*/
public Iterator<T> iterator() {
return new SLListIterator();
}

private class SLListIterator implements Iterator<Integer> {
private IntNode p;
private class SLListIterator implements Iterator<T> {
private Node<T> p;

public SLListIterator() {
p = sentinel.next;
Expand All @@ -83,8 +135,8 @@ public class SLList implements Iterable<Integer> {
return p != null;
}

public Integer next() {
int returnItem = p.item;
public T next() {
T returnItem = p.item;
p = p.next;
return returnItem;
}
Expand All @@ -95,7 +147,7 @@ public class SLList implements Iterable<Integer> {
</TabItem>
<TabItem title="C++">

```cpp frame="terminal"
```cpp frame="terminal" collapse={1-121}
// SLList.h
#ifndef SLLIST_H
#define SLLIST_H
Expand Down

0 comments on commit 465c996

Please sign in to comment.