linked-list

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

跟風遠走 提交于 2019-12-22 13:40:09
问题 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

Linked list implementation in c without using double-pointer

瘦欲@ 提交于 2019-12-22 12:19:42
问题 I have implemented a simple linked list in C language, but can it be implemented without using double-pointer(**).I want to implement same program by using only single pointers. #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; void push(struct node** head_ref, int new_data) { struct node* new_node = (struct node*) malloc(sizeof(struct node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } void append(struct node** head

Warnings when creating a singly linked list with arrays

99封情书 提交于 2019-12-22 10:49:40
问题 #include <stdio.h> typedef struct { int data; struct node *next; }node; void print(node *head) { node *tmp = head; while (tmp) { printf ("%d ", tmp->data); tmp = tmp->next; } } int main() { node arr[5] = { {1, &arr[1]}, {2, &arr[2]}, {3, &arr[3]}, {4, &arr[4]}, {5, NULL} }; print(arr); return 0; } Why do i get these warnings while compiling with gcc -Wall ? (even without -Wall, gcc produces the same warnings) list.c: In function ‘print’: list.c:15:7: warning: assignment from incompatible

Display after deletion in linked list in C

浪尽此生 提交于 2019-12-22 10:05:49
问题 Actually this was another problem but it changed so I decided to open a new question. My code is typedef struct inner_list { int count; char word[100]; inner_list*next; } inner_list; typedef struct outer_list { char word [100]; inner_list * head; int count; outer_list * next; } outer_list; void delnode(outer_list **head,char num[100])//thanks to both Nir Levy and Jeremy P. { outer_list *temp, *m; m=temp=*head; /*FIX #1*/ while(temp!=NULL) { if(strcmp(temp->word,num)==0) { if(temp==*head) {

Why does java linkedlist implementation use the interface deque?

﹥>﹥吖頭↗ 提交于 2019-12-22 09:43:14
问题 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. 回答1: The LinkedList implementation happens to to satisfy the Deque contract, so why not make it implement the

Implementation of LinkedList in python __getitem__() method

萝らか妹 提交于 2019-12-22 07:58:27
问题 I am implementing a LinkedList in python(3.7.4) and the code of the module is below :- LinkedList.py class Node: def __init__(self,value): self.value = value self.ref = None class LinkedList(Node): def __init__(self): self.__head = None self.__cur = None self.__count = 0 def add(self,value): if self.__head is None: self.__cur = Node(value) self.__head = self.__cur else: self.__cur.ref = Node(value) self.__cur = self.__cur.ref self.__count += 1 def getList(self): temp = self.__head while temp!

C++ Linked list behavior

江枫思渺然 提交于 2019-12-22 05:53:25
问题 I have some C code, where in there are two linked lists(say A and B) and A is inserted at a particular position into B and A still has elements. How do I simulate the same behavior effectively using the C++ STL? If I try splice, it makes the second one empty. Thanks, Gokul. 回答1: You need to copy the elements. Consider something like this: std::copy(a.begin(), a.end(), std::inserter(b, b_iterator)); If you want the same nodes shared by two lists, this is simply not supported by std::list (STL

Linked List Implementation in C

江枫思渺然 提交于 2019-12-22 01:39:54
问题 I am new to Linked LIsts and I am trying to implement a Linked List in C . Below in my code :- #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; void insert (struct node *head, int data); void print (struct node *head); int main() { struct node *head ; head= NULL; printf("new\n"); insert(head,5); printf("%d\n",head); insert(head,4); insert(head,6); print(head); print(head); print(head); } void insert(struct node *head,int data){ printf("%d\n",head); if(head ==

Destructor for a linked List

萝らか妹 提交于 2019-12-22 01:03:31
问题 I have a linked_list and currently my destructor is not working properly. Not entirely sure why. Can somebody explain me how to solve this? class linked_list { private: struct node { // String in this node std::string data; // Pointer to next node struct node *next; }; //First item in the list struct node *first; Here is my destructor linked_list::~linked_list(void) { while (first) { delete first; first = first->next; } } 回答1: The problem lies here: delete first; first = first->next; When you

atomic swap with CAS (using gcc sync builtins)

泄露秘密 提交于 2019-12-21 17:23:27
问题 Can the compare-and-swap function be used to swap variables atomically? I'm using C/C++ via gcc on x86_64 RedHat Linux, specifically the __sync builtins. Example: int x = 0, y = 1; y = __sync_val_compare_and_swap(&x, x, y); I think this boils down to whether x can change between &x and x; for instance, if &x constitutes an operation, it might be possible for x to change between &x and x in the arguments. I want to assume that the comparison implicit above will always be true; my question is