linked-list

delete elements in a singular linked list

≡放荡痞女 提交于 2019-12-02 17:48:50
问题 in this code i am deleting the element in the linked list 11->12->13->14->15->12->16 if i want to delete 12 it deletes only the first time occurrence element i.e o/p wll be 11->13->14->15->12->16 but i want to delete all the occurrence of 12, how to do that? can anyone give me some inputs? #include<stdio.h> #include<stdlib.h> void insertbeg(); void delpos(); void display(); struct node { int info; struct node *link; }*first=NULL; struct node *create(); int item,key; main() { int choice; while

How to fix a type error when working with stack.top() in the <stack> library in c++

老子叫甜甜 提交于 2019-12-02 17:45:50
问题 I'm trying to implement a function in my linked list that pushes the values of one list into a stack, and then pops off those values into another list. The problem is, when I try to std::cout << x , the first stack's topmost element, I get this error: c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ostream:682:5: error: no type named 'type' in 'struct std::enable_if<false, std::basic_ostream<char>&>' #include <iostream> #include <cstddef> #include <string> #include <stack> #include <vector> using

Difference in LinkedList, queue vs list

不羁的心 提交于 2019-12-02 17:33:00
What is the difference when creating these two objects Queue<String> test = new LinkedList<String>(); and List<String> test2 = new LinkedList<String>(); What are the actual differences between test and test2 ? Are both of them LinkedList ? Are there performance differences or reasons to use one over the other? The two statements you've written each construct a LinkedList<String> object to hold a list of strings, then assign it to a variable. The difference is in the type of the variable. By assigning the LinkedList<String> to a variable of type Queue<String> , you can only access the methods

Remove all nodes in linked list

一个人想着一个人 提交于 2019-12-02 17:30:13
问题 I have a linked list contains 3 nodes like the image shown: There is a head pointer and temp1 pointer point to the front of the list, and tail point points at the end of the list. I want to remove all the nodes, and change it back to its original initial form ( tail = NULL, head = first_node , but the first node doesn't have any value in the data and next field). Because I want to start putting up some new values in it. To remove all those data, is this code going to remove nodes inside this

Creating a linked list with a for loop

雨燕双飞 提交于 2019-12-02 17:28:33
问题 Here is my struct struct ListItem{ int data; struct ListItem *next; }; Assuming the first node of the linked list will have data = 0, I want to write a for loop that creates a linked list of size 5 but I'm not sure how to work I tried the following int main(int argc, char* argv[]){ struct ListItem a; a.data = 0; for (int i = 1; i < 5; i++){ struct ListItem *pointer = &a; struct ListItem nextnode; nextnode.data = i; a.next = &nextnode; pointer = pointer->next; } } But the result is a.data = 0

Correctly implementing a singly linked list C++

巧了我就是萌 提交于 2019-12-02 17:26:54
问题 I have a list with names of employers such as: Node 1: Jill, Matt , Joe, Bob, Matt Node 2: Jeff, James, John , Jonathan, John , Edward Node 3: Matt, Doe, Ron , Pablo, Ron , Chase , Ron , Chase, Loui and I'm trying to get it to where if it sees a repeat it will send it to the front of the list and delete that current node, so that it will look like this Node 1: Matt , Jill, Joe, Bob Node 2: John , Jeff, James, Jonathan, Edward Node 3: Chase , Ron , Matt, Doe, Pablo, Loui Unfortunately, My

Interview question: remove duplicates from an unsorted linked list

对着背影说爱祢 提交于 2019-12-02 17:21:13
I'm reading Cracking the Coding Interview, Fourth Edition: 150 Programming Interview Questions and Solutions and I'm trying to solve the following question: 2.1 Write code to remove duplicates from an unsorted linked list. FOLLOW UP: How would you solve this problem if a temporary buffer is not allowed? I'm solving it in C#, so I made my own Node class: public class Node<T> where T : class { public Node<T> Next { get; set; } public T Value { get; set; } public Node(T value) { Next = null; Value = value; } } My solution is to iterate through the list, then for each node to iterated through the

Pointer to struct in C# to create a linked list

孤人 提交于 2019-12-02 17:19:33
问题 C# does not like pointers, but I need to use them now to create a linked list, like we would do in C. The struct is simple: public unsafe struct Livro { public string name; public Livro* next; } But I get the error: "Cannot take the address of, get the size of, or declare a pointer to a managed type". Any ideas? 回答1: You can just use a class instead of a struct : public class Livro { public string Name { get; set; } public Livro Next { get; set; } } This will provide the proper behavior

Algorithm for Shuffling a Linked List in n log n time

六月ゝ 毕业季﹏ 提交于 2019-12-02 15:58:21
I'm trying to shuffle a linked list using a divide-and-conquer algorithm that randomly shuffles a linked list in linearithmic (n log n) time and logarithmic (log n) extra space. I'm aware that I can do a Knuth shuffle similar to that could be used in a simple array of values, but I'm not sure how I would do this with divide-and-conquer. What I mean is, what am I actually dividing? Do I just divide to each individual node in the list and then randomly assemble the list back together using some random value? Or do I give each node a random number and then do a mergesort on the nodes based on the

Why does std::list::reverse have O(n) complexity?

强颜欢笑 提交于 2019-12-02 15:40:50
Why does the reverse function for the std::list class in the C++ standard library have linear runtime? I would think that for doubly-linked lists the reverse function should have been O(1). Reversing a doubly-linked list should just involve switching the head and the tail pointers. Hypothetically, reverse could have been O(1) . There (again hypothetically) could have been a boolean list member indicating whether the direction of the linked list is currently the same or opposite as the original one where the list was created. Unfortunately, that would reduce the performance of basically any