I have been trying to create a linked list of "Minions" where each has x coordinates, y coordinates, wp (weapon power), and a pointer to next minion.
the code:
#include<cstddef>
#include<iostream>
using namespace std;
struct minion{
int x;
int y;
int wp;
minion* next;
[Code] ....
Problem is: I made 3 for loops in main, first one to display the current weapon power which expected to output 10 10 10 (initial set weapon powers), 2nd for loop increases their weapon power (for whatever reason, a level up or something), third loop should display the new increased weapon power.
BUT, what i get is 10 and below it 30. looks like min4's wp only increased. There must be something wrong i am doing while i try to loop through my linked list and i cant figure it out...
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'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 have a method that traverses a list to compare a number from the list to user input from elsewhere in the program. I have tried a few different things and I am probably overlooking something simple but my problem is my if statement is executing the else statement every run no matter if the input matches. This function is to return the location in the list that the num matches. -1 if no match, 0, 1, 2, otherwise depending on the number of nodes.
int NumberList::find(int num) { int temp = 0, count = 0; ListNode *nodePtr;
I'm trying to write a function that takes two linked lists and creates a third one with only the common elements.
It assumes the first list (the caller) has no dups, but it doesn't seem to be working. The program doesn't crash, it just hangs when it is supposed to display L3 (the third list)..everything else runs and is displayed fine.
I have a linked list comprised of chars like so...
Code:
node1 - "p" node2 - "o" node3 - "p"
I need a function that will take in three perameters...node *replaceChar(node *head, char key, char *str)Stipulations of this function. head is the head of the list, 'key' and 'str' are guaranteed to contain alphanumeric characters only (A-Z, a-z, and 0-9). str can range from 1 to 1023 characters (inclusively). So if I call this function with these perameters..
Code:
node *head == /*the head of the list to be examined*/ char key == "p"char *str == "dog"The new list will look like this... node1 - 'd' node2 - 'o' node3 - 'g' node4 - 'o' node5 - 'd' node6 - 'o' node7 - 'g'
All instances of 'p' were replaced with 'dog' I have a toString function which takes in a string and converts it to a linked list and returns the head. So assume that you can call the function on str = "dog" so...
Code:
toString(str) == /*this will return the head to the list made from the str*/
If it's unclear what my question is...I am stumped on how to write the replaceChar function the one that takes in three perameters..
// Write a function called insertEntry() to insert a new entry into a linked list.
Have the procedure take as arguments a pointer to the list entry to be inserted (of type struct entry as defined in this chapter), and a pointer to an element in the list after which the new entry is to be inserted.
// The function dveloped in exercise 2 only inserts an element after an existing element in the list, thereby prenting you from inserting a new entry at the front of the list.
(Hint: Think about setting up a special structure to point to the beginning of the list.)
This is a working version of the exercise, but I don't think I'm doing what's asked. I was able to add an element to the beginning of the list using an if statement, not creating a special structure that points to the beginning of the list. How would I go about creating a special structure that points to the beginning of the list to add a new element at the beginning of the list?
I am working on building a graph of a maze problem using an adjacency list and my own custom class Node.
Each Node has a vNum and jumpAmount integer variable, the vNum is the unique key.
If I have an array of lists of Node objects declared by: Code: forward_list<Node> adjacencyList[10] I skip the 0 row and only use 1-9. It makes more sense logically for my program and Im not worried about wasted memory at this point.
How could I go about traversing and outputting member variables for all the Nodes in each list in my array?
where the values in brackets are the nodes that have an edge with the ith row node. Had to put it in code tags for the thread to be allowed to post.
I am using the forward_list library for my list functions and this is where my problem arises. The cplusplus.com - The C++ Resources Network website iterates through the list using:
Code: for ( auto it = mylist.begin(); it != mylist.end(); ++it ) std::cout << ' ' << *it;
Obviously this works for ints, doubles, etc, but not objects with member variables. My attempt to replicate this code for my problem looks like:
Code: for( int i = 1; i < 10; ++i ) { cout << "Node " << i << " -> { "; for( Vertex it = adjList[i].begin(); it != adjList[i].end(); ++it ) { cout << it.getVNum() << ", "; }}
I have to come up with user defined conversions for this to work and I have never done such a thing. Is there a way to go about doing this to avoid user defined conversions, because if there isnt I feel like attempting to do this specific problem might be a little too difficult for someone who hasnt defined any custom conversions.
Potentially, I could write my own List class and have a next pointer member variable that points to the next object in the list and use that to use a dot reference to a get function to display member variables. This is how I learned linked lists back in my Data Structures class. However I am trying to avoid that for the time being, as that would be a lot more code to implement rather than just figuring out how to use the forward_list library.
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
What is "if(prev)"? Wouldn't "prev" always have the same value? Secondly, if tmp is NULL (which will be the case when the loop if(ptr->val == val) finds a match the first time it is run), is *prev assigned a NULL?
I'm having a small issue here with my linked list.I built a linked list with strings and it worked perfectly.Now since i'm using strtok() to separate the string.for now here's what i've got:
I have the codes for the functions: "Given the functions that we have discussed and defined in the class and the code that we have created, create the code for the Delete Node function and the Search function. Create an application that will incorporate all the functions that we have discussed and the new ones that you need to create. The application should allow the linked list to be built initially from a text file. The application should then allow for the user select an operation to perform. The acceptable operations are
- to search for an item - to delete an item - to enter a new item - to exit the application
After the initial build and loading of data from the textfile, the application should print a listing of each item in the list and the node that it is located in. after a delete or insert, the application should display an output of the list again showing each item in the list and the node that it resides in.
The data item for this problem will be the inventory structure that we created in class and the data is in the inventory file that you already have downloaded."
I am having difficulty getting my program to run. I have looked and looked many times trying to figure out what I am doing wrong. I have a hunch that it's my load function that's acting up, but from my perspective, my creation of my singly linked list looks good. From the creation of my singly linked list, does anything stand out as being abnormal?
I been ask to create a sorted linked list module, and to test each function with 1 unit, 2 +.so i wrote my addtofront function( we have to use the same as the teacher)
but i cant seem to test it because it always display a seg fault.... i dont know what i did wrong....how do i call this function in main to display a string i want?
I decided to make a linked list program using classes and templates.I implemented my linked list and added some basic functions, everything worked just fine. Then I proceeded to add templates...First, the error list:
#pragma once template<class T> class Node { private: Node* next;
[code]....
To sum up: Something's wrong with the templates that I used in "List.cpp" and what it could be.
My program is almost done all that is left is entering big numbers, the program can add and subtract small numbers but not big numbers because it puts them all in one node, I need the program to store each number in a node.
#include<iostream> #include<string> using namespace std; class Node { public: int value; Node * next;
I have created this program to merge two linked list into one ,but however everything is working fine but on execution it providing a segmentation fault error ( on calling merging function )
code :
#include<iostream> #include<stdio.h> using namespace std; struct node{ int data; node* link;
I am unable to make any logical algorithm to access middle of stack. I am using stack implemented with linkedlists, I have following functions available for stack like push,pop,top.
class List; List *deletezeroendlist(List* L); class List { public: intdigit; List*nextDigit; public: List():digit(0), nextDigit(NULL){} List(int d, List *next):digit(d), nextDigit(next){}
I have tried many different ways but it is still not the answer / perform the function List *deletezeroendlist(List* L)
I know how to add a node and delete a node from the beginning and from the end of the List. Now I want to sort the List by Age. I tried everything but there must be a very little mistake that I can't find. Here is my code:
#include <iostream> using namespace std; struct Node { char name[20]; int age; double height;