linked-list

Why exactly do we need a “Circular Linked List” (singly or doubly) data structure?

我们两清 提交于 2019-12-02 15:30:00
Why exactly do we need a "Circular Linked List" (singly or doubly) data structure? What problem does it solve that is evident with simple Linked Lists (singly or doubly)? A simple example is keeping track of whose turn it is in a multi-player board game. Put all the players in a circular linked list. After a player takes his turn, advance to the next player in the list. This will cause the program to cycle indefinitely among the players. To traverse a circular linked list, store a pointer to the first element you see. When you see that element again, you have traversed the entire list. void

NullPointerException Error using linked lists

守給你的承諾、 提交于 2019-12-02 14:49:40
问题 I just finished up working on this program and got it to compile but it breaks after user input and gives me this: Please input 0 or more values at keyboard 12 4 3 2 1 Exception in thread "main" java.lang.NullPointerException at Search.buildList(Search.java:41) at Search.main(Search.java:10) Here is the code: import java.io.*; import java.util.*; public class Search { public static void main(String argv[]) throws IOException { Scanner stdin = new Scanner(System.in); System.out.println("Please

Odd problem with pointer while implementing a linked list

我怕爱的太早我们不能终老 提交于 2019-12-02 14:40:00
问题 I'm trying to implement a linked list in C, and I want to store the head node in a separate struct. However, it seems like the head node is being reassigned somehow whenever I add another node. #include <stdio.h> #include <stdlib.h> struct BC_node { struct BC_node *next; void *data; }; struct BC_list { struct BC_node *head; struct BC_node *tail; }; void BC_list_push(struct BC_list *list, void *data) { struct BC_node *node = calloc(1, sizeof(struct BC_node)); if (list->head != NULL) printf(

Difference between LinkedList<?> and LinkedList<Object> [duplicate]

↘锁芯ラ 提交于 2019-12-02 13:36:46
This question already has an answer here: What is the difference between ? and Object in Java generics? 6 answers What does the question mark in Java generics' type parameter mean? 6 answers Is there any difference between LinkedList< ? > and LinkedList< Object > in Java? This passes compilation: LinkedList<?> list1 = new LinkedList<String> (); This doesn't: LinkedList<Object> list2 = new LinkedList<String> (); i.e. a LinkedList<?> variable can be assigned any LinkedList<SomeType> . A LinkedList<Object> variable can only be assigned a LinkedList<Object> (or a raw LinkedList , which is not

Flatten a list using common lisp

僤鯓⒐⒋嵵緔 提交于 2019-12-02 13:10:16
问题 I was reading the book On Lisp by Paul Graham. In Chapter 4, Utility Functions, he gives examples of small functions that operate on lists, which would be helpful while writing a larger program. One of them is flatten . Given a nested list at any arbitrary level as argument, flatten will remove all the nested elements and put them on the top level. Below is my attempt at implementing flatten: (defun flatten (lst) (labels ((rflatten (lst1 acc) (dolist (el lst1) (if (listp el) (rflatten el acc)

Moving head of linked list to tail

扶醉桌前 提交于 2019-12-02 12:43:25
问题 I need to write a method in Java that moves the first element in a linked list to the last position. To accomplish this, I believe I'd have to set a node to reference the first element after the head, then set the next node to null. I tried to do this with my method, but when running the method, the output is incorrect. The rest of the class I have is most likely too large to post here, but I think I only need help on conceptualizing how to move the first element to the end of the list. The

Re-ordering a Linked List in Python

拈花ヽ惹草 提交于 2019-12-02 12:39:38
I realize this sort of data structure is better done with built in list type, but I'm trying to understand this more for academic reasons. Given that I have a linked list like this: a -> b -> c -> d -> e -> f I would like to change the references to b -> a -> d -> c -> f -> e In other words every pair gets switched. I am using these two classes to create a linked list. class Node: def __init__(self): self.cargo = None self.next = None class LinkedList: def __init__(self): self.cur_node = None def add_node(self, cargo): new_node = Node() new_node.cargo = cargo new_node.next = self.cur_node self

Using Pointers and strtok()

女生的网名这么多〃 提交于 2019-12-02 12:31:06
问题 I'm building a linked list and need your assistance please as I'm new to C. I need to input a string that looks like this: (word)_#_(year)_#_(DEFINITION(UPPER CASE)) Ex: Enter a string Input: invest_#_1945_#_TRADE Basically I'm looking to build a function that scans the DEFINITION and give's me back the word it relates to. Enter a word to search in the dictionary Input: TRADE Output: Found "TREADE" in the word "invest" So far I managed to come up using the strtok() function but right now I'm

JAVA - Items not being entered into the list properly?

北城余情 提交于 2019-12-02 12:26:16
This is sort of a continuation from this question . I managed to figure out the whole JTextField not letting me enter anything problem. Now I have a separate problem that the item isn't being entered properly in the list. I still think you guys should look at the GitHub repository since I'm only posting a small portion of the code here, but here's at least the relevant portions. Here's the GUI program (or at least the relevant portions): import java.awt.event.*; import javax.swing.*; public class GUI extends JPanel implements ActionListener { LinkedList<FoodItem> foodItemList = new LinkedList

NullPointerException Error using linked lists

☆樱花仙子☆ 提交于 2019-12-02 12:24:46
I just finished up working on this program and got it to compile but it breaks after user input and gives me this: Please input 0 or more values at keyboard 12 4 3 2 1 Exception in thread "main" java.lang.NullPointerException at Search.buildList(Search.java:41) at Search.main(Search.java:10) Here is the code: import java.io.*; import java.util.*; public class Search { public static void main(String argv[]) throws IOException { Scanner stdin = new Scanner(System.in); System.out.println("Please input 0 or more values at keyboard"); Node head = buildList(); System.out.println("Now printing list")