C++ :: Linked Lists - Retrieve Data From Nodes?

Feb 9, 2013

I am working on a linked list that instead of the traditional integer value for a data, i'm using my own class. Unfortunately when i do this, i cant seem to retrieve my data from my Node.

#include "Customer.h"
class Node {
// TO DO:: Fill in.
//Constructors and destructor
public:
Node();
Node(int atime, int ttime);
Node(int atime, int ttime, Node* nd);

[Code] .....

In my main code, I've been trying something like

Node* c = new Node(1,2)
Customer x = c->cust; // or Customer x = c->get_cust();
cout<<x.get_atime()<<endl;
cout<<x.get_ttime()<<endl;

It should be displaying 1 and then 2, but just outputs 0 and 0.

View 1 Replies


ADVERTISEMENT

C++ :: Nodes In Linked Lists?

Feb 26, 2015

I created a bunch of nodes and I made each one before one another. Im having trouble adding another node to the last one.

#include <iostream>
using namespace std;
struct nodeType{
int info;
nodeType *link;
};
void printList(nodeType *head) {
nodeType *current = head;

[code]....

The node with the value of 400 is the node that has to be last.. How would that work?

View 3 Replies View Related

C++ :: Swapping Two Adjacent Linked Lists Nodes

Oct 22, 2014

I am working on a program where I sort elements into alphabetical order and then when one is less than the other I swap them. I first did it by swapping the data but they want me to swap the nodes instead and I am having trouble doing that.

Node *add_node( Node *list, const string &s ) {
struct Node *n = new struct Node;
n->word = s; // copy string s to word
n->next = 0;

// add node n to the list
// the list should always be in ascending alphabetical order
n->next = list;
list = n;

[Code] ....

View 2 Replies View Related

C++ :: Creating A Linked List Of Common Elements From Two Other Linked Lists

Apr 29, 2013

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.

template <typename T>
LList <T> LList <T>:: common (LList <T> &B)//common fct
{
Node <T> *hunter1 = Head;

[Code]......

View 10 Replies View Related

C :: Linked List - Adding Nodes

Jul 23, 2014

I wrote function to add to elements in the list, but when I call printList function it returns me empty list ! I'm new with linked list in C

Output:

Empty list
List is empty add element at the begining
New node with packet num 245
List is not empty add element at the end
New node with packet num 486

Linked list: Empty

Main:

Code:

int main(){
struct node * start ;
start = NULL;
int i;
/*Check if list is empty*/
if(start == NULL){
printf("Empty list

[Code]...

View 2 Replies View Related

C++ :: Linked List Adding Nodes?

Oct 17, 2014

Ok so I am having difficulty adding nodes to my linked list....

how to add a third node while keeping track of the address...Thats where I get lost..I don't know how to keep track of the addresses of the next node..

#include <iostream>
using namespace std;
typedef struct Node{

[Code]......

View 1 Replies View Related

C/C++ :: Storing Nodes Into A Linked List?

Apr 18, 2015

I'm simply trying to locate possible logic errors because if I could fill this list properly, I can finish my project very easily. This is just a small portion of a very immersive project.

I am trying to create a linked list that holds objects of type Location *. I have Location defined as

typedef struct location{
char *name;
char *longer;
char *shorter;

[Code].....

I wish to clarify but can not find where to edit the OP. I believe the list is still empty because I attempt to do a simple read through the list by accessing the head and then reassigning the list to the tail of the list. However, the program never enters the while loop

while(world !=0){

View 2 Replies View Related

C :: Printing Info From Nodes In A Linked List

Oct 16, 2013

I created a linked list, but I wanted to make sure it was storing each word appropriately and going over the text. It compiles and runs, so I know it works. I'm just not sure if it's doing what I want...

Code:
#include <stdio.h>
#include <stdlib.h>
//creating the linked list
struct node {

[Code].....

View 3 Replies View Related

C :: Linked List Insert Node Between Two Nodes

Oct 25, 2013

I have a problem with inserting a node between two nodes.

Code:
#include <stdio.h>
#include <stdlib.h>
struct listelem {
int nbr;
struct listelem *next;

[Code] ....

View 8 Replies View Related

C++ :: Rebinding Allocator For The Nodes Of Linked List?

Mar 27, 2014

I am coding a singly-linked list container. Of course, internally it uses Node<T>.

Question: what is the correct way to use the allocator given by the user? I ask, because I've read this on the rival C++ Reference:

std::list<T, A> allocates nodes of some internal type Node<T>, using the allocator std::allocator_traits<A>::rebind_alloc<Node<T>>, which is implemented in terms of A::rebind<Node<T>>::other if A is an std::allocator

[URL]...

The above doesn't seem right to do, because then what should pointer and const_pointer be?

using pointer = std::allocator_traits<Alloc>::pointer;
using const_pointer = std::allocator_traits<Alloc>::const_pointer;
// but we're using Alloc<Node<T>> not Alloc<T>
// so maybe do this?
using pointer = value_type *;
using const_pointer = const value_type *;

View 6 Replies View Related

C++ :: Deleting Linked List Nodes Recursively

Jun 25, 2014

I am creating a Linear linked list in c++. I've written all the functions for it, but now I want to try and do them using recursion.

I managed to make functions for adding and displaying nodes on the list, but I am stuck on the function for removing nodes. First I want to try and write a function the removes the last item on the list. But for some reason my code isn't working properly.

Here is the code :

void removeLastItem(node * &head) {
if(!head) {
delete head;
head = NULL;

[Code] ....

NODE - My structure name
NEXT - The pointer to next element.
HEAD - The first (head) pointer.

The couts in the if statements are just for testing. In fact after I Run my program it does as it is supposed - enters the second if /b]case as many times as there are elements and then executes the first [b]if statement. But for some reason it does not delete the actual node.

View 2 Replies View Related

C++ :: Swapping Nodes In Doubly Linked List?

Nov 15, 2014

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().

PRIORITY QUEUE:

#ifndef JMF_PriorityQueue
#define JMF_PriorityQueue
#include <iostream>
#include <string>
template <typename T>
class PriorityQueue{

[Code] .....

Okay, so I've tried implementing SwapUp() in a different new way, but it's still giving me the same problem

template <typename T>
void PriorityQueue<T>::swapUp(Node * target){
Node * partner = target->next; //Partner = target next

[Code] .....

This is such an elementary logic problem I don't know why I'm having so much trouble with it.

View 4 Replies View Related

C :: Quick Sorting Array Of Nodes In A Linked List?

Oct 19, 2013

I'm trying to sort the elements in a linked list which contain a variable amount of data in any given case. In the sample code, the code is more static, but I plan on adding it to much more dynamic code once I have it figured out. My main problem is that I am not sure how to sort the linked list while still keeping the correct pointers to the nodes. I thought about writing my own custom quick sort instead of using the C-standard library function, but how I would keep the pointers to the next nodes correct eluded me. Here is my code so far :

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

[Code]......

View 3 Replies View Related

C++ :: Linked List - How To Sort Nodes In Ascending Order

Apr 18, 2013

At the line number 65 that's my sort method first i sum up all the value in the nodes after that i want to sort the Nodes In ascending order but the method is not working ...

#include <iostream>
#include <conio.h>
using namespace std;
// Node Class

[Code] ....

View 3 Replies View Related

C++ :: How To Randomly Insert Certain Numbers Into Linked List With 10 Nodes

Feb 8, 2014

How to randomly insert certain numbers into a linked list with 10 nodes. Meaning I want to put for example numbers 1 5 10 15 20 25 30 35 40 50 in random locations in the linked list.

View 1 Replies View Related

C++ :: Singly Linked List - Delete Duplicate Nodes

Oct 10, 2014

I don't know why my code is not working. It's from line 249 - 266

void SLL::deleteDuplicate(){
Node* temp = head;
Node* p = temp;

[Code].....

View 1 Replies View Related

C++ :: Sorting Nodes In Linked List From Highest To Lowest

Nov 21, 2014

I need to sort the Linked list from highest to lowest or in this case. The highest Bribe gets the higher priority on the list. I have looked all over the internet and have found some pretty decent examples but I still don't truly understand how to sort it. I think from looking at so many examples I have confused myself even more. I was reading about using Doubly Linked list but I don't even know if were allowed to use that.

1. The program runs perfectly at the moment. It prints out the list but does not sort it.
2. How to make sure that I am deleting the allocated memory correctly in the deconstructor.

Header File

#include <iostream>
#ifndef PERSON_H
#define PERSON_H
struct PersonRec;
class PersonList {

[Code] .....

View 6 Replies View Related

C++ :: Swap Adjacent Nodes In Doubly Linked List?

Feb 28, 2013

I have been trying to swap two adjacent nodes for my linked list sort. Its not meant to be optimal, just meant to work. My problem is I either lose nodes or get Access Violation errors.

PHP Code:

void List::sortList() {
    Node *current = _head;
    Node *rightNode = NULL;        
    if (_head->_data > _head->_next->_data) {            
        current = _head;
        rightNode = _head->_next;

[Code] .....

View 10 Replies View Related

C :: Reversing Every K Nodes Of Linked List - Program Crashes While Running

Mar 11, 2013

The code below is for reversing every k nodes of the linked list. While running the Program it crashes.

Code:
#include<stdio.h>
#include<stdlib.h>
struct node {
int info;
struct node *next;

[Code] ....

View 1 Replies View Related

C/C++ :: Accept Items In Singly Linked List And Sort Nodes?

Jan 11, 2013

i want to make a program that accepts 15 items in a singly linked and sorts the nodes.

View 1 Replies View Related

C++ :: Linked Lists - Trying To Revive

Dec 27, 2013

I'm trying to set up a simple implementation of a double linked list. I can't make it fly.

Code:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

[Code] ...

I seem to create a root vertex, but I can't establish if I connect sub nodes to my list.

View 13 Replies View Related

C :: Merging Two Linked Lists?

Mar 6, 2015

I am merging two linked list but the output of the program is not printing insert

Code:
typedef struct Merge
{
int info;

[Code].....

View 1 Replies View Related

C :: Dynamic Linked Lists

Oct 14, 2013

In our homework assignment it states to, "write a set of dynamic linked lists" and so on. Now, the problem I'm confusing myself is at the storage inside of each node. To my understanding, each node contains the prev and next nodes, but also a pointer to the data, in this case being a string. He has asked us to manage these strings as rows of chars such as

char[0] = c // first node being addressed here
char[1] = a
char[2] = t
char[3] =
char[4] = d // second node starting here
char[5] = o
char[6] = g
char[7] =

I have written my code where each node is holding a string, not separated as shown above... my question is to how you can build your doubly linked list where each node is being address a set of chars.

View 1 Replies View Related

C/C++ :: Working With Linked Lists?

May 11, 2014

I'm trying to do is let the user type in something like A654321 (note: these are ALL the characters, i.e. 'A', '6', '5', '4', etc..) Then I just want to display that current number back to them and I am getting some weird pointer memory allocation error..

#include<iostream>
using namespace std;
//To rename data-type "int" or integer to "element" (Both are equivalent)..
typedef int element;
//To declare an appropriate SENTINEL.
char const SENTINEL = '#';

[code]....

It seems like the first part that it bugs out at is in my Clean(); function..

Unhandled exception at 0x01383FBB in VegMe.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0.

It doesn't like head = head->next; ?Also, ignore the ReverseList(); function, it is not being used as of now.

View 7 Replies View Related

C :: 3D Array Of Pointers To Linked Lists

Aug 8, 2013

I am having some trouble getting a 3d array of pointers to link up with a linked list for each of the array elements. the 3d array of pointers is declared like this:

Code:

struct particle *cell[MAXCELLS][MAXCELLS][MAXCELLS]; and there are linked lists declared like this:
Code: struct particle { /* structure for particles */
double sw[3]; /* square well components */
double hs[3]; /* hard sphere components */
double u[3]; /* unit vector for rotations */
struct particle *link;
};

I want to get the array pointers 'cell[x][y][z]' to point to the first observed particle in a function next to main, something like this:

Code:

void generate_list(){
int i,j,k,l;
/* determine the number of cells to decompose the simulation box in each of x, y and z directions*/
int(cells_x) = floor(boxX/cell_size);
int(cells_y) = floor(boxY/cell_size);
int(cells_z) = floor(boxZ/cell_size);
/* initialise the array of pointers to NULL */
for (j=0;j<cells_x;j++){

[Code]...

I am getting a pointer type cast error when I compile "assignment from incompatible pointer type",

View 6 Replies View Related

C :: Reading A File To Linked Lists?

Jun 20, 2013

I have to write a program that reads from a text file, which contains a list of stock hourly prices and the company names. Something like this:

78.52 82.56 75.10 71.97 Water Company
22.40 25.68 21.37 22.96 Mega Shipping Inc

Each company data will be kept in a structure that contains: a pointer for the name, an array of the prices, and the average price for the day. The structures will be kept in a linked list. Each node structure will contain a pointer to one company data, and a pointer to the next node structure.This is all I have so far..

Code:

typedef struct {
char *companyName;
double hourlyPrices[NUM_PRICES];
double avgPrice;
} COMPANYDATA;

}

[code]...

We have to sort the nodes alphabetically. My question is, how do I read the data from the file one line at a time, and insert them alphabetically one by one?

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved