linked-list

C++ Linked List node counting (need help)

那年仲夏 提交于 2019-12-25 16:43:03
问题 I'm trying to create a program that gets string input from a text file, inserting the content into a list, word by word. I also have to calculate the numbers of the duplicates. My program works fine for the small input text file (1 line of string). But whenever I feed it with a bigger text file, it crashes. Any help will be great. Here is my code: #include <iostream> #include <fstream> #include <sstream> using namespace std; class Bag { private: struct BagNode { string dataValue; int

duplicate the singly linked list

五迷三道 提交于 2019-12-25 15:01:46
问题 [3, 5, 4, 2, 1] where I need the delete the nodes close to the tail which is it should be like [1, 2, 3, 5, 4] any suggestions? public void delete() { for(Node<T> current = getHead(); current != null; current = current.getNext()){ System.out.println(temp.getValue()); removeValue(temp.getValue()); } } } } 回答1: You don't need to remove anything at all (I mean not by calling removeValue ). Just store the values you encounter in a set and if the value is already in the set, re-link your list in

Putting elements into a sorted list

大城市里の小女人 提交于 2019-12-25 12:42:01
问题 Folks, my method needs to add a new element into already a sorted list, i.e. in a proper position. The point is the method has to add the objects in a diagonal sort. For example, board.set(1,1,11); board.set(2,4,33); board.set(3,4,66); board.set(3,2,44); board.set(3,3,55); board.set(1,4,88); board.set(0,2,77); board.set(0,5,99); board.set(2,1,22); The result should be: [(2,1,22), (3,2,44), (1,1,11), (3,3,55), (3,4,66), (0,2,77), (2,4,33), (1,4,88), (0,5,99)] But my program prints this: [(3,4

Putting elements into a sorted list

纵然是瞬间 提交于 2019-12-25 12:41:46
问题 Folks, my method needs to add a new element into already a sorted list, i.e. in a proper position. The point is the method has to add the objects in a diagonal sort. For example, board.set(1,1,11); board.set(2,4,33); board.set(3,4,66); board.set(3,2,44); board.set(3,3,55); board.set(1,4,88); board.set(0,2,77); board.set(0,5,99); board.set(2,1,22); The result should be: [(2,1,22), (3,2,44), (1,1,11), (3,3,55), (3,4,66), (0,2,77), (2,4,33), (1,4,88), (0,5,99)] But my program prints this: [(3,4

List of iterators to collect entries from various lists

≯℡__Kan透↙ 提交于 2019-12-25 08:43:35
问题 A class TaskGroup mainly contains a list of Task s. I want to collect certain tasks ta1 , ta2 , tb3 , ... from various groups ga , gb , gc , ... and use this list in a display window/class. The choice of tasks for the collection is of no concern for this question. (As the question is rather conceptual than technical, the examples are mostly pseudo code. Since I'm using Qt in this particular case, I'll be using Qt classes, but the question shouldn't be limited to this case.) class Task; class

Inserting a node at a specific position in a Linked list

余生颓废 提交于 2019-12-25 08:34:47
问题 I am given the pointer to the head node of a linked list, an integer to add to the list and the position at which the integer must be inserted. After inserting this node at the desired position I need to return the head node. The code that I have written is not working for some reason and goes in an infinite loop. class Node { int data; Node next; } Node InsertNth(Node head, int data, int position) { int count = 0; Node node = head; Node prev = null; while(count != position){ count++; node =

Copying a linked list and returning a pointer to the new list

ε祈祈猫儿з 提交于 2019-12-25 06:46:59
问题 My current struct is: typedef char AirportCode[4]; typedef struct node{ AirportCode airport; struct node *next; }Node; My idea for how the function should start is this: Node *copy(Node *list) { int count = 0; while (list != NULL){ count++; list = list->next; } } Now we know how long the original list is, but the reason why I am so stumped is because I have no idea how to seperatly allocate the memory for each individual node that we have to copy to the second list. 回答1: You allocate a new

Using Linked lists and patterns in python

六眼飞鱼酱① 提交于 2019-12-25 06:36:18
问题 Trying to write a function that will iterate over the linked list, sum up all of the odd numbers and then display the sum. Here is what I have so far: from List import * def main(): array = eval(input("Give me an array of numbers: ")) ArrayToList(array) print(array[0]) print(array[1]) print(array[2]) print(array[3]) print(sumOdds(array)) def isOdd(x): return x % 2 != 0 def sumOdds(array): if (array == None): return 0 elif (isOdd(head(array))): return head(array) + sumOdds(tail(array)) else:

C: Evaluating Expression in a queue not working

房东的猫 提交于 2019-12-25 06:12:07
问题 I have created a queue using linked list that takes a string in the format 1 + 3 + 5 = or 1-3+2-4 = but the following code which is supposed to do the calculation given the sign and produce the answer is not working right for example if I passed a string like 66 - 31 - 21 + 43 = is passed the answer turns out to be -47 instead of 57 . Can someone help me solve it or point to what I'm doing wrong. void printQueue(struct node* head) { struct node* temp; char *token, *del=" "; int total = 0;

Converting from Infix to Postfix using stacks (c++)

百般思念 提交于 2019-12-25 05:46:08
问题 I'm currently working on a project to convert from postfix to infix using a stack in the form of a singly linked list. I've managed to convert expressions such as ab+ to (a+b) however when the expression gets longer such as ab+cd*- . It doesn't work. I'm considering pushing the previously converted expression back onto the stack however the stack is of type char and the expression is a string and it complains when I try to push it back. Should I make it a template and if so how would I do it