linked-list

How to find a middle value or node from linked list in java?

空扰寡人 提交于 2019-12-06 04:56:29
I have the 50 values in linked list.how to find a middle value or node of linked list? List list = new LinkedList(); for (int i = 0; i < 50; i++) { list.add(String.valueOf(i)); } int size = list.size(); int middle = (size / 2); System.out.println(list.get(middle).toString());... i got an answer like this.... But my team leader said to find in another way? Is there any other built in method to iterate in linked list?i tried ...but i dint get any built in method for finding middle value...And or can u any one suggest another logic to find the value of middle node in linke list? thank you.......

Why can't I push this object onto my std::list?

坚强是说给别人听的谎言 提交于 2019-12-06 04:19:46
问题 Just started programming in C++. I've created a Point class, a std::list and an iterator like so: class Point { public: int x, y; Point(int x1, int y1) { x = x1; y = y1; } }; std::list <Point> pointList; std::list <Point>::iterator iter; I then push new points onto pointList. Now, I'm needing to iterate through all the points in pointList, so I need to loop using the iterator. This is where I get screwed up. for(iter = pointList.begin(); iter != pointList.end(); iter++) { Point currentPoint =

Java:Delete all the elements from Linked List

此生再无相见时 提交于 2019-12-06 03:37:25
问题 In Java, how to remove all the elements in linked list without using already available clear() method? This exercise was inspired by a question received in a phone interview. Say I can do this in C void DeleteAllElement( ListElement **head ) { ListElement *deleteMe = *head; while( deleteMe ) { ListElement *next = deleteMe->next; delete deleteMe; deleteMe = next; } *head = NULL; } Thanks 回答1: Java has automatic garbage collection, so you just need to set the Head reference to null: myList

C program to make a second copy of a linked list

半城伤御伤魂 提交于 2019-12-06 02:47:17
问题 I was writing a C code to copy the contents of a Linked List onto another list. I want to know if there is a more efficient way of doing this. Which is better? struct node *copy(struct node *start1) { struct node *start2=NULL,*previous=NULL; while(start1!=NULL) { struct node * temp = (struct node *) malloc (sizeof(struct node)); temp->info=start1->info; temp->link=NULL; if(start2==NULL) { start2=temp; previous=temp; } else { previous->link=temp; previous=temp; } start1=start1->link; } return

Heap sort using linked lists

安稳与你 提交于 2019-12-06 02:39:15
问题 I was wondering if anyone has ever used linked lists to do heap sort and if they have could they provide the code. I have been able to do heapsort using arrays, but trying to do it in linked lists seems unpractical and just a pain in the you know where. I have to implement linked lists for a project Im doing, any help would be greatly appreciated. Also I am using C. 回答1: The answer is "you don't want to implement heap sort on a linked list." Heapsort is a good sorting algorithm because it's O

Synchronizing access to a doubly-linked list

末鹿安然 提交于 2019-12-06 02:24:02
问题 I'm trying to implement a (special kind of) doubly-linked list in C, in a pthreads environment but using only C-wrapped synchronization instructions like atomic CAS, etc. rather than pthread primitives. (The elements of the list are fixed-size chunks of memory and almost surely cannot fit pthread_mutex_t etc. inside them.) I don't actually need full arbitrary doubly-linked list methods, only: insertion at the end of the list deletion from the beginning of the list deletion at arbitrary points

Reverse doubly-link list in C++

杀马特。学长 韩版系。学妹 提交于 2019-12-06 01:53:09
I've been trying to figure out how to reverse the order of a doubly-linked list, but for some reason, in my function void reverse() runs while loop once and then crashes for some reason. To answer some questions ahead, I'm self-teaching myself with my brothers help. This isn't all of the code, but I have a display() function which prints all nodes chronologically from start_ptr and a switch which activates certain functions like case 1 : add_end(); break; case 2 : add_begin(); break; case 3 : add_index(); break; case 4 : del_end(); break; case 5 : del_begin(); break; case 6 : reverse(); break;

What is the reason for making a nested class static in HashMap or LinkedList? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-06 00:56:42
This question already has an answer here: Static nested class in Java, why? 13 answers In most of the cases I've seen that nested classes are static . Lets take an example of Entry class in HashMap static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; Entry<K,V> next; final int hash; ..... ..... } Or Entry class of LinkedList private static class Entry<E> { E element; Entry<E> next; Entry<E> previous; ..... ..... } What I know so far about nested class is: - A non-static nested class has full access to the members of the class within which it is nested. - A static nested

Linked lists, arrays, and hardware memory caches

Deadly 提交于 2019-12-06 00:12:30
问题 While questions have been asked before about linked lists versus arrays, the answers mostly boil down to what most of us probably have already learned at some point: Lists are good at inserting and deleting Arrays are good at random access Now respectable people like Bjarne Stroustrup have argued that arrays practically always outperform linked lists because they make much better use of the caching architecture implemented in modern hardware. He also states that the performance advantage of

Why does java linkedlist implementation use the interface deque?

瘦欲@ 提交于 2019-12-05 23:39:01
I was looking at the java implementation of LinkedList, and found this: public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable Why should a LinkedList support the Deque interface? I understand the desire to add elements to the end of the linked list, but those methods should have been incuded in the List interface. The LinkedList implementation happens to to satisfy the Deque contract, so why not make it implement the interface? IIRC, deque stands for double end queue . In the case you mention, it's not logical to define a