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
ADVERTISEMENT
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
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
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
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
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
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
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
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
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
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
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
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
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
Feb 8, 2015
I'm having a little problem where after reading a file I'm unable to update my struct with some particular info. It is actually a linkedlist. This is my struct:
Code:
struct node{
int id;
char name[50];
struct node *next;
}*head;
And in my Main function:
Code:
int main(){
int i, t_id, num;
struct node *temp;
char name[50], t_name[50], input_name[50], line[LINESIZE], *value;
struct node *head = NULL;
FILE * ifp = fopen("AssignmentOneInput.txt", "r");
[Code] .....
Basically in the while loop when I input something like this for updating numbers:
Code: temp->id = num; //These are numbers separated by a comma. or this:
Code: strcpy(temp->name,value); //These are names The program crashes
View 11 Replies
View Related
Dec 17, 2014
The programme has to be a linked list to gather student info, (ie name, lastname, student no, and three grade); the first and last name must use malloc correctly, there must be an insert and delete function and a printfunction. and they must print out in alphabetical order.
Code:
#include #include #include struct student{
char *lastName;
char *firstName;
char dob[20];
char sID[20];
float grade;
[code] .....
View 7 Replies
View Related
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
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
Dec 10, 2013
I am trying to get to print a powerset from my linked list however I am not sure how to go about it. I need to create a void function for it so I know that much.
Any ways this is my code.If you do not know what a powerset is here is an example.
say you have a set {1,2,3}the power set would be the following:{{}, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}} another example say you have the power set {1,2,3,4,5} the result should be this:{{}, {1}, {2}, {3}, {4}, {5}, {1,2}, {1,3}, {1,4}, {1,5}, {2,3}, {2,4}, {2,5}, {3,4}, {3,5}, {4,5}, {1,2,3}, {1,2,4}, {1,2,5}, {1,3,4}, {1,3,5}, {1,4,5}, {2,3,4}, {2,3,5}, {2,4,5}, {3,4,5}, {1,2,3,4}, {1,2,3,5}, {1,2,4,5}, {1,3,4,5}, {2,3,4,5}, {1,2,3,4,5}}
#ifndef TEST1_H_INCLUDED
#define TEST1_H_INCLUDED
class List{
[code]....
View 5 Replies
View Related
May 2, 2014
I've got this program that I'm working on. Most of the code is from a video tutorial, but I was editing it to be able to search for an element by name. That's working fine, but suddenly the part of the program that prints out all the elements starts in an infinite loop after I input two elements, and search for one.
Here's what I've got:
[code#include <iostream> usingnamespacestd; struct node { int number; string name; node *next; }; bool isEmpty (node *head); char menu (); void insertAsFirstElement (node *&head, node *&last, int number); void insertSorted(node *&head, node *&last, int number); void remove(node *&head, node *&last); void showList (node *current); void showByName (node *current, string name); bool isEmpty (node *head) { if (head == NULL) { return true; } else { returnfalse; } } char menu () { char choice;
[Code] .....
View 8 Replies
View Related
Oct 3, 2014
Write a program that creates a forward linked list of at least 20 elements, where each element holds a random integer between 0 and 99. Print the list.
Write the function "returnMiddleList" to find the middle element of the linked list in one pass. Print the integer value of this element and the position of this element (starting at zero) in relation to the head (where the head = 0, the element pointed to by the head = 1, the element pointed to by the previous one = 2, etc).
Split the list in half at the middle element to create two entirely separate* linked lists of near equal size (+/- 1) and print the two lists. Modify the "returnMiddleList" function to accomplish this, returning the head of the second linked list and setting the link of the element pointing to that head to null. Then print the two sums of the integers stored in the elements of both lists.
Sort the two lists from least to greatest and print them out (printing at this step is optional depending on the sort approach taken). Then combine the two lists while sorting them again from least to greatest and print out the new list. (HINT: you can subdivide the lists further and sort them on a scale of one to two element lists before sorting and combining the first two unsorted lists. What is this sort called?)
I have got #1 and #2 working, but #3 and #4 is where the issue is beginning. When I split my link list into two lists and print the individual lists out, my first link list prints out 9 numbers when it should be printing out 10 (the 10th number somehow disappears?), but when I do the sum of the first list right after that, the number that has disappeared gets added in the sum! I do not know why it is disappearing, and this is one issue. Another issue is in the second list, a random "0" gets added to the list and one of the numbers is lost. My last issue is about #4 as the merge algorithm I have used does not seem to work (I am merging the list together while sorting them, but I am not using a recursion sort because we have not learned that yet).
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct nodeType {
int data;
nodeType *link;
[Code] .....
View 9 Replies
View Related
Aug 8, 2013
// First_VB_Prog.cpp : Defines the entry point for the console application.//
#include <iostream>
using namespace std;
//Simple Linked List composing of a "Body" , "Head" and a "Tail"
struct nodeType {
[Code] .....
View 3 Replies
View Related
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
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
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
View Related
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