Javascript  

 

 

 

 

 

Linked List

 

this code is created first by chatGPT on Jan 18 2023 (meaning using chatGPT 3.5) and then modified a little bit my me. The initial request that I put into chatGPT is as follows :

 

Write a javascript code meeting following requirement.

 

1. Put an html table with three rows at the center of the page

2. In the first row, put a textbox and 'Prepend','Append,'Insert','Delete' buttons

3. In the second row, put a listbox with 'item1','item2','item3'

4. In the third row, put a textfield named 'LinkedlistOutput'

5. The data in the listbox is stored in a Linkedlist and print the linkedlist structure in 'LinkedlistOutput' in the format of 'A -> B -> C'

6. Implement the routines for append, prepend, insert, remove operation for the linkedlist

7. When clicking an item in the listbox, the selected item is shown in the text box. Reflect this change to the linkedlist and print the output

8. When clicking 'Prepend' button, the text in the textbox is added on top of the listbox. Reflect this change to the linkedlist and print the output

9. When clicking 'Append' button, the text in the textbox is added on bottom of the listbox. Reflect this change to the linkedlist and print the output

10. When clicking 'Insert' button, the text in the textbox is added at the selected position in the list box. If there is no selected position, show a popup saying 'you have to select a position in the list box' . Reflect this change to the linkedlist and print the output

11. When clicking 'Delete' button, the text in the textbox is removed from the list box. If there is no selected position, show a popup saying 'you have to select a position in the list box' . Reflect this change to the linkedlist and print the output

12. Write the html, css, javascript into separate files .

NOTE : It is not guaranteed that you would have the same code as I got since chatGPT produce the answers differently depending on the context. And it may produce the different answers everytime you ask even with the exact the same question.

NOTE : If you don't have any of your own idea for the request, copy my request and paste it into the chatGPT and put additional requests based on the output for the previous request. I would suggest to create a new thread in the chatGPT and put my request and then continue to add your own request.

 

 

LinkedList.html

<!DOCTYPE html>

<html>

  <head>

    <link rel="stylesheet" type="text/css" href="LinkedList.css" />

  </head>

  <body>

    <table id="table" align="center">

      <tr>

        <td>

          <input type="text" id="textbox" />

          <button id="prepend">Prepend</button>

          <button id="append">Append</button>

          <button id="insert">Insert</button>

          <button id="delete">Delete</button>

        </td>

      </tr>

      <tr>

        <td>

          <select id="listbox" multiple>

            <option value="item1">item1</option>

            <option value="item2">item2</option>

            <option value="item3">item3</option>

          </select>

        </td>

      </tr>

      <tr>

        <td>

          <textarea id="linkedlistoutput"></textarea>

        </td>

      </tr>

    </table>

    <script src="LinkedList.js"></script>

  </body>

</html>

 

 

LinkedList.css

#table {

  width: 600px;

}

 

#listbox {

  width: 100%;

  height: 100px;

}

 

#linkedlistoutput {

  width: 100%;

  height: 50px;

}

 

 

LinkedList.js

class Node {

  constructor(value) {

    this.value = value;

    this.next = null;

  }

}

 

class LinkedList {

  constructor() {

    this.head = null;

    this.tail = null;

    this.length = 0;

  }

 

  // Add value to the start of the list

  prepend(value) {

    const newNode = new Node(value);

    if (!this.head) {

      this.head = newNode;

      this.tail = this.head;

    } else {

      newNode.next = this.head;

      this.head = newNode;

    }

    this.length++;

  }

 

  // Add value to the end of the list

  append(value) {

    const newNode = new Node(value);

    if (!this.head) {

      this.head = newNode;

      this.tail = this.head;

    } else {

      this.tail.next = newNode;

      this.tail = newNode;

    }

    this.length++;

  }

 

  // Insert value at a specific index in the list

  insert(value, index) {

    if (index < 0 || index > this.length) {

      return false;

    }

    if (index === 0) {

      this.prepend(value);

      return true;

    }

    if (index === this.length) {

      this.append(value);

      return true;

    }

    const newNode = new Node(value);

    let current = this.head;

    let currentIndex = 0;

    while (currentIndex !== index - 1) {

      current = current.next;

      currentIndex++;

    }

    newNode.next = current.next;

    current.next = newNode;

    this.length++;

    return true;

  }

 

  // Remove the value at a specific index from the list

  remove(index) {

    if (index < 0 || index >= this.length) {

      return undefined;

    }

    if (index === 0) {

      const removed = this.head;

      this.head = this.head.next;

      this.length--;

      return removed.value;

    }

    let current = this.head;

    let currentIndex = 0;

    while (currentIndex !== index - 1) {

      current = current.next;

      currentIndex++;

    }

    const removed = current.next;

    current.next = current.next.next;

    this.length--;

    return removed.value;

  }

 

  // Return a string representation of the list

  toString() {

    let current = this.head;

    let result = "";

    while (current) {

      result += current.value + " -> ";

      current = current.next;

    }

    return result.slice(0, -4);

  }

}

 

const listbox = document.getElementById("listbox");

const textbox = document.getElementById("textbox");

const linkedlistoutput = document.getElementById("linkedlistoutput");

const prependButton = document.getElementById("prepend");

const appendButton = document.getElementById("append");

const insertButton = document.getElementById("insert");

const deleteButton = document.getElementById("delete");

 

const linkedList = new LinkedList();

 

// Add initial values from list box to linked list

for (let i = 0; i < listbox.options.length; i++) {

  linkedList.append(listbox.options[i].value);

}

linkedlistoutput.value = linkedList.toString();

 

// Function to update the list box with the current values from the linked list

function updateListbox() {

  // Clear the options in the list box

  listbox.options.length = 0;

 

  // Add the values from the linked list to the list box

  let current = linkedList.head;

  while (current) {

    listbox.options[listbox.options.length] = new Option(current.value);

    current = current.next;

  }

}

updateListbox();

 

listbox.addEventListener("change", function() {

  textbox.value = listbox.value;

});

 

prependButton.addEventListener("click", function() {

  linkedList.prepend(textbox.value);

  linkedlistoutput.value = linkedList.toString();

  updateListbox();

});

 

appendButton.addEventListener("click", function() {

  linkedList.append(textbox.value);

  linkedlistoutput.value = linkedList.toString();

  updateListbox();

});

 

insertButton.addEventListener("click", function() {

  const selectedIndex = listbox.selectedIndex;

  if (selectedIndex === -1) {

    alert("you have to select a position in the list box");

  } else {

    linkedList.insert(textbox.value, selectedIndex);

    linkedlistoutput.value = linkedList.toString();

    updateListbox();

  }

});

 

deleteButton.addEventListener("click", function() {

  const selectedIndex = listbox.selectedIndex;

  if (selectedIndex === -1) {

    alert("you have to select a position in the list box");

  } else {

    linkedList.remove(selectedIndex);

    linkedlistoutput.value = linkedList.toString();

    updateListbox();

  }

});