I have a linked list I made and am practicing a simple recursion function sum() which returns the sum of all numbers in each node of the list. Here is my sum() function:
int sum(ListNode *ptr){ if (ptr->next) return (ptr->num + sum(ptr->next)); else return(0); }
where the initial arg passed through the ListNode * parameter is my head node (with null value). The output is fine until that point, then the program locks up and I get the "...has stopped working" error.
I have been asked to write three functions for a Liked List that can add remove and reverse the elements of the Linked List. Now, I have done the add function and It is displaying the elements properly using recursion function. But My removal function is not working and I tried all the possible logic I can think of, I dont know, where did I make mistakes.
This is My class for the Linked List
#ifndef __Linked_Lists_Recursive_function_Implementation___NumberList__ #define __Linked_Lists_Recursive_function_Implementation___NumberList__ #include <iostream> using namespace std;
I need to make the functions using these function prototypes. I am mainly having problems with GetFirst() and SwapData() but how to do it..
Header File with Prototypes Code: #ifndef LINKEDLIST_H #define LINKEDLIST_H /** * @file * This file provided a doubly-linked list implementation capable of storing any arbitrary data. * The list implementation relies on a chain metaphor: a list is merely a sequence of links * (ListItems) and there is no separate construct to represent the entire list, each ListItem in it
namespace main_savitch_5 { class polynode { public:
// CONSTRUCTOR: Creates a node containing a specified initial coefficient (init_coef), initial exponent (init_exponent), and initial links forward and backward (init_fore and init_back).
I have no problem traversing at both forward and backward but not simultaneously. I can traverse forward, and traverse again forward with no problem. I can also traverse backward and traverse backward again with no problem (take note this is by not exiting the program). Without exiting the program, with the same datas inputed, if i traversed forward, i cannot traverse backward (it only gives me infinite loop of the first data) and vice versa.
#include <iostream> using namespace std; struct Node { int data; Node *next; Node *prev;
I have a linkedList search function given and I am having a little trouble getting it to work. I've made a few attempts with no success. Given normal search code:
template <class Type> bool orderedLinkedList<Type>::search(const Type& searchItem) const { bool found = false; nodeType<Type> *current; //pointer to traverse the list
current = first; //start the search at the first node
[Code] .....
My attempt to make it a recursive search:
template <class Type> bool orderedLinkedList<Type>::search(const Type& searchItem) const { //bool found = false; nodeType<Type> *current; //pointer to traverse the list current = first; //start the search at the first node
Now, when I iterate back through my list (I want to start at the tail and work my way towards the head), I can only ever get the 1st node to print, then the 2nd node is garbage, which means, to me, that I've linked something wrong.
I'm trying to write a program that manipulates a doubly linked list. My professor wants it to have two structs, one called Node (containing the data, and pointers to the next and previous nodes) and one called DLList, which contains the nodes for the head and tail (which is then passed to all of my functions).
I'm a little confused how to access the head and tail, for instance, if I want to initially set them to null in the main function (he emphasized the need for this), or to use them in my functions. I've tried a lot of variations to call the head and tail, but I keep getting told that head and tail are undeclared in the function.
How might I access my head and tail, for instance in a main function, when they're defined like this? (I took out all of the logic in my functions for clarity)
I need to delete the Nth node from a doubly linked list. I know I just cant delete it out right. I have all the goodies including a templetized node class. This is the code we're given:
template <typename T> void doublyLinkedList<T>::deleteKthElement(const int item) { }
It accepts an int, and should go to the Nth node and delete it.
i am trying to develop a doubly linked list class. I became stuck at a few of the functions as well as the main.cpp file. i have to insert a character sentence "TRICK OR TREAT" spaces included. I am stuck on the display and the listsize functions and im unsure if my insert has the head node in it. The function parameters were given to us and most of this code was given from a single linked list.
When I run this in main it gives me a windows error message. I believe it has something to do with my insertAtEnd function but I've gone over it a million times....
#include<iostream> #include<string> #include<vector> #include"RhymeGame.h" using namespace std; Game::Game() { head = NULL;
Create a notepad that allows the user to write text on the console. For this purpose, the user should be able to control and track the movement of the cursor. The user can access, add and delete any part of the text. To add or delete a text, the user can take the cursor to that location (using the arrow keys) and perform the required operation. The working of the program (i.e. the movement of the cursor, add and delete operation) must be consistent with the working of the real notepad. However, you do not have to handle word wrapping.
In addition, the user should be able to save and load the data in a text file using S and L respectively. The program will automatically save the data in file save.txt, and load the data from the same file. There is no need to ask the user for the file name. Use Q to quit the notepad. Don’t forget to implement the destructor.
Internally, the notepad is composed of two-dimensional doubly linkedlist. Its implementation is just like a doubly linked list with an additional property that it can grow in two dimensions. Since text can be written on multi lines, each row of the 2D-linkedlist represents one line. Each node contains four links which it uses to connect to node before, after, below and above it. In addition each node can store a character.
We had to convert a single-line text editor which uses arrays to one that uses OOP and double-linked lists and I have been doing it in steps. I have, for the sake of convenience, put my headers, implementation and main all in one file.
I'm compiling this program in Hercules (the getch function uses C code).
I keep getting the error from the compiler saying "Undefined Symbol" for functions:
I've been working on this linked list priority queue . I know that the root of the problem is in my swapUp() function (swapping the positioning of two nodes based on their priority), because the list works great up until it is called. The seg fault is not actually being caused by swapUp(), it's being caused by peekAt(), which returns the element in the node at position n. But the error does not occur unless swapUp() is called first, so that is where the issue is (I think).
There is also a seg fault being caused in the destructor, which I believe may have the same root cause in swapUp().
I've been working on a doubly linked list project for my Data Structures course. I've got everything to work so far, except for traversing in both directions. Traversing in the forward direction works fine, but when I try choosing to traverse backwards (starting from the tail of the linked list), it just says that the value is not in the list. That being said, the output looks like this:
This is not true, as you can obviously see, the value 26 is in the list, just two elements to the left of the tail (the value in tail for this particular case, obviously, is 36). The output should have been something like, "The value 26 was found 2 elements from the tail node". Here's the code for the findValue() function:
Code: template <typename T> bool LinkedListX<T>::findValue()//bool valueFound { int traverseDirection = 0; bool found = false; int key;
I was trying to reverse a linklist in reverse direction using the recursion. I was able to reverse n - 1 element but it is not printing the first one. Below is my code.
Code:
typedef struct linklist { int data; linklist *next; };
void add(int data,linklist **node) {
[code]....
This is happening since recursion is starting from second node, which is due to reason not printing the first one when recursion print values from stack once
node != NULL
Condition is met.
Currently I am using below statement for printing the first element;
reverse_recur(node); printf(" Print In Reverse Order %d ",node->data);
I've walked through it a few times, and I feel like this correct. However, my program's output is off and I'm guessing this function is the culprit. I think I just need a different pair of eyes to take a look at it.
I have a text file that needs to be read by command line arguments. The text are all numbers and can have multiple numbers on one line separated by a space. I cannot use an array or sort the numbers.So say I have a text file, listNums.txt:
12 473 8 29 30 1 8 248 17 55 29 84 5
Basically I need to read one number, find out if its odd or even by dividing by 2, search the odd or even doubly linked list that it would go into to see if its in there, if its not then add it to the bottom of the list.
I need to create a templated doubly linked list, with an iterator class within the list class. This program is to function just like the STL list class but I only need to implement functions that I am using, My trouble is I am kind of clueless on the iterator part and the fact that the list is templated is giving me syntax grief. I have pasted the code I have done so far.
1. On the syntax implementing the list and iterator functions outside of the class 2. I am not sure when to deference the iterator in the functions, but think I have it right so far 3. For the reverse function can I copy the list into a new list in reverse then re add them to the original list overwriting the same values? I have the code I have so far there 4. For the iterator erase function, I am not sure if I am deleting the node correctly. 5. I am not sure if I need template <typename T> above the iterator functions. Does the iterator class need to be a template? Right now it is not.
// Templated doubly linked list class
#include <iostream> using namespace std; template <typename T> class list { private: Node *head; Node *tail;