linked-list

Bubble sort in c linked list [closed]

℡╲_俬逩灬. 提交于 2019-12-05 08:14:54
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . I need to do is read in an input file into a linked list. Part of the file is: NameA, 25 NameB, 33 NameC, 23 NameD, 39 And after i need to sort by the number (bubble sort) and write it to another file. Here is what i have: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node{ char

Extract middle element from a single linked list [closed]

主宰稳场 提交于 2019-12-05 08:08:21
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I have a question: Find the middle element from a single linked list. I need to know the way/method of this problem. 回答1: You can use two pointers to iterate through the list - one which iterates twice as fast as the other. When the fast pointer reaches the end of the list then

Insertion in the middle of ArrayList vs LinkedList [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-05 07:57:41
This question already has answers here : When to use LinkedList over ArrayList in Java? (32 answers) Closed 6 years ago . Talking in Java's context. If I want to insert in the middle of either an ArrayList or a linkedList , I've been told that Arraylist will perform terribly. I understand that it is because, we need to shift all the elements and then do the insertion. This should be of the order n/2 i.e. O(n). But is not it the same for linkedList . For linked List, we need to traverse till the time we find the middle, and then do the pointer manipulation. In this case too, it will take O(n)

Sorting a linked list in c++

落花浮王杯 提交于 2019-12-05 07:28:09
问题 I'm getting mad with infinite loop, what do you think is suitable solution? void sorting () { node * temphead = head; node * tempnode = NULL; for (int i=0; i<count; i++) { for (int j=0; j<count-i; j++) { if (temphead->data > temphead->next->data) { tempnode = temphead; temphead = temphead->next; temphead->next = tempnode; } temphead=temphead->next; count++; } } } I tried to increment count and use many conditions with while- before and after the for loop with no result 回答1: An easier way to

Passing a linked list head through a function as address in C

老子叫甜甜 提交于 2019-12-05 04:51:41
问题 I have a question regarding passing the head of a linked list in C through a function. So the code goes something like this: #include <stdio.h> //Defining a structure of the node struct node { int data; struct node* next; }; void insert (struct node* rec, int x) { struct node* temp = (struct node*)malloc(sizeof(struct node)); temp->data = x; temp->next = NULL; rec = temp; // head and rec is now pointing to the same node } void print(struct node* rec){ printf("%d", rec->data); //error occurs

Radix Sorting with using queue

落花浮王杯 提交于 2019-12-05 04:05:47
I've wanted to create a radix sort implementation using queues. I couldn't figure out which part of my code has problems or which resources should I read. My code may be totally wrong but this is my implementation without any help (I haven't taken a data structures & algorithms course yet). I created a function but it didn't work. While doing research, I saw some code samples but they seemed to be more complex for me. Firstly I wanted to find the least significant digit of all integers Then sort them in queue element whose subscript matches, then after sort copy all queues to end of 11th queue

How to reverse a linked list in Ruby

和自甴很熟 提交于 2019-12-05 03:37:33
问题 In the mutation example below, I don't understand how the linked list is reversed. class LinkedListNode attr_accessor :value, :next_node def initialize(value, next_node=nil) @value = value @next_node = next_node end end def print_values(list_node) print "#{list_node.value} --> " if list_node.next_node.nil? print "nil\n" return else print_values(list_node.next_node) end end def reverse_list(list, previous=nil) current_head = list.next_node list.next_node = previous if current_head reverse_list

Array of Linked Lists C++

怎甘沉沦 提交于 2019-12-05 03:10:19
问题 So I thought I understood how to implement an array of pointers but my compiler says otherwise =(. Any help would be appreciated, I feel like I'm close but am missing something crucial. 1.) I have a struct called node declared:. struct node { int num; node *next; } 2.) I've declared a pointer to an array of pointers like so: node **arrayOfPointers; 3.) I've then dynamically created the array of pointers by doing this: arrayOfPointers = new node*[arraySize]; My understanding is at this point,

C, Print Linked List of Strings

落爺英雄遲暮 提交于 2019-12-05 02:25:59
问题 I have to write a C program that uses a linked list. I have created a list and added elements to the list. But I don't know how to print all the elements in the list. The list is a list of strings. I figured I'd somehow increment through the list, printing every string that's there, but I can't figure out a way to do this. Short: How to I print a linked list ? 回答1: There are no stupid questions 1 . Here's some pseudo-code to get you started: def printAll (node): while node is not null: print

Plain, linked and double linked lists: When and Why?

谁说我不能喝 提交于 2019-12-05 02:05:01
问题 In what situations should I use each kind of list? What are the advantages of each one? 回答1: Plain list: Stores each item sequentially, so random lookup is extremely fast (i.e. I can instantly say "I want the 657415671567th element, and go straight to it, because we know its memory address will be exactly 657415671567 bigger than the first item). This has little or no memory overhead in storage. However, it has no way of automatically resizing - you have to create a new array, copy across all