linked-list

how to find middle node in singly linked list without traversal?

狂风中的少年 提交于 2019-12-07 01:26:11
问题 how to find middle node in singly linked list without traversal ? is it possible in first place ? In One traversal I Use the traditional method of using 2 pointers one which jump's 2 positions and other which jump's one position ..is there any other approach to find middle node in one traversal 回答1: No, it's not possible. The addresses of the nodes are arbitrary, so there's no way of knowing them without traversing them. 回答2: public void findMiddleNode() { Node n1 = headNode; Node n2 =

Expression Must have Class Type?

孤人 提交于 2019-12-07 00:47:27
So I've been playing around with Nodes and keep running into this error when I try to test it. If I use Parentheses I get this Error on list. - "Expression must have class type!" If I don't use Parentheses I get this Error on list , insert and display - "this is inaccessible." This happens when Declaring my LList in Main(). What's going on and why is this? My Driver #include "LList.h" #include <iostream> using namespace std; int main() { LList<int> list; bool test = list.insert(5); list.display(); return 0; } Class LList #include "Nodes.h" #ifndef LLIST_H #define LLIST_H template<typename TYPE

What is the comparable interface called in golang?

混江龙づ霸主 提交于 2019-12-06 19:58:34
问题 I'm working on a simple linked list implementation in golang for learning purposes. The definition of an element is below: type Element struct { next, prev *Element Value interface{} } As you can see, the Value can be anything that satisfies the empty interface. Now, as a new feature, I would like to make it so that when you insert a new element into the list, it inserts it in a sorted manner - each element will be <= the next. In order to do this, I wrote the following method: func (l

What is the point of PHP's SplDoublyLinkedList class, and more importantly, Linked Lists in general?

ぃ、小莉子 提交于 2019-12-06 18:44:31
问题 On a quest to expand my programming prowess, I've delved ever-so-slightly into The Standard PHP Library. This led to my discovery of the SplDoublyLinkedList class. From there I read the descriptions of Linked Lists and Doubly Linked Lists on Wikipedia. I understand how they work... But I cannot conceive of a reason WHY we need it—or better yet a practical example of SplDoublyLinkedList since we have indexed and associative arrays in PHP. How are Linked Lists normally used in-and-out of PHP?

Sorting a linked list in Java

社会主义新天地 提交于 2019-12-06 18:28:12
问题 I have written a bubble sort algorithm to sort a linked list. I am a Java beginner and trying to learn data structures. I am confused why my second element is not sorted properly. EDIT class SListNode { Object item; SListNode next; SListNode(Object obj) { item = obj; next = null; } SListNode(Object obj, SListNode next) { item = obj; this.next = next; } } public class SList { private SListNode head; private SListNode temp; public void sortList() { SListNode node = head,i,j; head = node; i =

Whose address shall the node of a linked list store: other node, or data structure having a node as a field?

独自空忆成欢 提交于 2019-12-06 16:41:17
From Understanding the Linux Kernel : the Linux kernel defines the list_head data structure, whose only fields next and prev represent the forward and back pointers of a generic doubly linked list element, respectively. It is important to note, however, that the pointers in a list_head field store the addresses of other list_head fields rather than the addresses of the whole data structures in which the list_head structure is included ; see Figure 3-3 (a). Why do the pointers in a list_head field store the addresses of other list_head fields rather than the addresses of the whole data

Solving Josephus with linked lists

社会主义新天地 提交于 2019-12-06 16:25:26
问题 I've been tryin for a while but i cant figure out how to make the program below take N as input and generate an M in order that the last soldier that dies is the 13th(N>13); int main() { int N, M; struct node { int player_id; struct node *next; }; struct node *p, *q; int i, count; printf("Enter N (number of players): "); scanf("%d", &N); printf("Enter M (every M-th payer gets eliminated): "); scanf("%d", &M); // Create circular linked list containing all the players: p = q = malloc(sizeof

Recursive Stack in C

一笑奈何 提交于 2019-12-06 16:21:23
during recursion a stack is created what does that stack contains, does it contains the values or it stores the addresses of the operands void recursiveReverse(struct node** head_ref) { struct node* first; struct node* rest; /* empty list */ if (*head_ref == NULL) return; /* suppose first = {1, 2, 3}, rest = {2, 3} */ first = *head_ref; rest = first->next; /* List has only one node */ if (rest == NULL) return; /* reverse the rest list and put the first element at the end */ recursiveReverse(&rest); first->next->next = first; /* tricky step -- see the diagram */ first->next = NULL; /* fix the

Assign txt file data to struct node in linked list

≯℡__Kan透↙ 提交于 2019-12-06 15:47:42
Ok so I have never worked with fstream before or opened and read and files in a program. My instructor just gave a few lines of code that open, read, and close a text file. I'm supposed to take the data out of the text file and put it into separate nodes in a linked list and then go on to do other things with it which is not important because I know how to do it. My problem is that I don't know how to a assign these values to the struct values. The txt file looks like this: Clark Kent 55000 2500 0.07 Lois Lane 56000 1500 0.06 Tony Stark 34000 2000 0.05 … I have created a structure called

Algorithm to Find the pointer to the Node M Steps to Tail in a Singly Linked List

♀尐吖头ヾ 提交于 2019-12-06 15:39:53
Suppose there is a singly linked list whose length is unknown. We want to find the node with M steps to the tail. For example, the singly list is like this: (A)->(B)->(C)->(X)->(Y) and M = 2. Then the output should be pointer to (C). When confronting this quiz, my first reaction is to traverse the singly linked list to get the length N. Then traverse the singly the second time, but only forward N-M-1 steps. The time complexity is O(n) and space complexity is O(1). Then, I'm challenged to find a solution to do it in one-traverse way. The solution is to have two pointers. The second pointer is M