logo
Tags down

shadow

Why is my print function not working? Linked list


By : Sean Powell
Date : September 15 2020, 11:00 PM
wish helps you I am doing a class assignment, where I have to create a linked list representing a big number, but I cant get the print function to work. , For starters these data members
code :
node* temp;
node* curr;
void List::addNode(string digits)
{
    int o = 1;
    int j = 0;
    int i = 0;
    node *n;
    node *head;
    //…
n->data = stoi(digits.substr(i,1));
#include <iostream>
#include <string>

class List
{
private:
    struct node
    {
        int data;
        node *next;
    } *head = nullptr;
public: 
    List() = default;
    void addNode( const std::string &digits );
    std::ostream & printList( std::ostream &os = std::cout ) const;
};


void List::addNode( const std::string &digits )
{
    if ( !digits.empty() && 
         !( digits.size() == 1 && ( digits[0] == '-' || digits[0] == '+') ) )
    {

        node **current = &head;

        while ( *current )
        {
            node *tmp = *current;
            *current = ( *current )->next;
            delete tmp;
        }

        std::string::size_type i = 0;

        *current = new node { digits[i] == '-' ? -1 : 1, nullptr };

        if ( digits[i] == '-' || digits[i] == '+' ) ++i;

        for ( ; i < digits.size(); i++ )
        {
            current = &( *current )->next;

            *current = new node { digits[i] - '0', nullptr };
        }
    }
}

std::ostream & List::printList( std::ostream &os ) const
{
    if ( head != nullptr )
    {
        if ( head->data == -1 ) os << '-'; 

        for ( node *current = head->next; current != nullptr; current = current->next )
        {
            os << current->data;
        }
    }

    return os;
}

int main() 
{
    List lst;

    lst.addNode( "-123456789" );
    lst.printList() << '\n';

    lst.addNode( "987654321" );
    lst.printList() << '\n';

    return 0;
}
-123456789
987654321


Share : facebook icon twitter icon

Why does this function print a Linked List backwards?


By : Kristy Cavaligos
Date : March 29 2020, 07:55 AM
Hope this helps In the forward-printing version, it prints each node before doing the recursive call. In the backward-printing version, it prints each node after doing the recursive call.
This is not coincidental.

why is this print linked list function not working?


By : nova
Date : March 29 2020, 07:55 AM
Hope that helps I have a function as following , check this.
code :
struct node *temp, *head= NULL, *last = NULL;
    for (int i = 0; i < 10; i++) {
        temp = malloc(sizeof(struct node));
        temp->data = i+1;
        temp->ptr = NULL;
        if (head == NULL)
          head = temp;
        if (last != NULL)
          last->ptr = temp;
        last = temp;
    }
printLinkedList(head);

creating a linked list + add new node + print the list and cannot figure out why it's not working


By : Abhi
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further so i am trying to create a linked list, that has a function to add a new node at the beggining of the list, and then print the list out. i am a novice coder and am still learning how to do this, when i execute the code, nothing prints .. , The program can look the following way (without testing)
code :
#include <stdio.h>
#include <stdlib.h>

typedef char Data ;

struct node 
{
    Data d ;
    struct node *next ;
};

struct node * addNew( struct node *head, Data value )
{
    struct node *new_node = malloc( sizeof( struct node ) );

    if ( new_node != NULL )
    {
        new_node->d = value;
        new_node->next = head;
        head = new_node;
    }

    return head;
}

void printList( struct node *head )
{
    for ( ; head; head = head->next ) printf( "%c ", head->d );
}

struct node * freeList( struct node *head )
{
    while ( head != NULL )
    {
        struct node *tmp = head;
        head = head->next;
        free( tmp );
    }

    return head;
}

int main( void )
{
    struct node *head = NULL;

    head = addNew( head, 'a' );
    head = addNew( head, 'b' );
    head = addNew( head, 'c' );

    printList( head );
    printf( "\n" );

    head = freeList( head );
}
struct node *header = NULL;
struct node *second = NULL;
struct node *third = NULL;
header->d = 'a';
header->next = second;
//..
struct addnew;

Linked List print not working..?


By : Adz88
Date : March 29 2020, 07:55 AM
like below fixes the issue Why are you pointing your new node to head in insert_tail()? You need to link the last entry in the list to your new node.
You already have Node(2) present as both the head and tail, just add your Node(7) to the end:
code :
Nodeptr N = new Node(data);
tail->nextNode = N;    
tail = N; // now head points to 2, tail points to 7

linked list print function is not working


By : Tayyab Alam
Date : March 29 2020, 07:55 AM
will be helpful for those in need Your list is empty because push returned value is not used
Your main should like this:
code :
int main()
 {

    list_node* l_list = NULL;
  l_list = push(l_list, 1);
  l_list = push(l_list, 2);
  l_list = push(l_list, 6);
  l_list = push(l_list, 8);
  l_list = push(l_list, 7);
  l_list = push(l_list, 3);
  l_list = push(l_list, 4);

  printf("Given linked list \n");
  Print(l_list);

  return 0; 
}
void push(list_node** head_r, int new_data)
 {
    list_node* new_Node = (list_node*)malloc(sizeof(list_node));

    new_Node->data  = new_data;
    new_Node->next = *head_r;
    *head_r = new_Node;
  }

int main()
 {

    list_node* l_list = NULL;
  push(&l_list, 1);
  push(&l_list, 2);
  push(&l_list, 6);
  push(&l_list, 8);
  push(&l_list, 7);
  push(&l_list, 3);
  push(&l_list, 4);

  printf("Given linked list \n");
  Print(l_list);

  return 0; 
}
Related Posts Related Posts :
  • How to clone objects of unknown dynamic type in C++?
  • Where should [] of delete be put?
  • Temporary object: Clarification in terminology
  • par_unseq and "vectorization-unsafe" functions
  • libgphoto2 and Visual Studio 2019
  • What's the difference between "double* grade", "double *grade", and "double* fn()"?
  • How to license C++ software
  • Meaning of declaration float ***c
  • character array validation in C++
  • Why can't casting an address to int* be an lvalue but casting to a struct pointer can?
  • Can names in unnamed namespaces in different C++ files refer to the same named thing?
  • How to get variable no of argument with its size in C++ using variadic template
  • Understanding SHT_NOTE section ".note.ABI-tag" of an ELF exectable
  • Displaying all prefixes of a word in C++
  • Does Erase deletes heap memory used by element of stl unordered map
  • Can you cast a vector<int64> to a vector<uint8>
  • Do function parameter variables always require a & or * operator?
  • File Append In Such A Way That It Ends Line After Each Save C++
  • Calling Derived class function from a Base class pointer after typecasting it to Derived class pointer
  • is there a std::optional_function like the given
  • Access array in main by pointers by method in class
  • Is there a method/function in c++ which later constant parameters are based on the first ones?
  • How to find out which functions the compiler generated?
  • Hourglass in C++ adding arguments
  • I'm trying to encrypt a message for my homework assignment
  • Object instantiation with curly braces and : symbols
  • c++ How to add value at the beginning of the array and index it?
  • Data structure that stays sorted, allows log N insertion time, and can return the index of an element that I look for in
  • What is diffrence between return reference instance and non reference instance(return mystr & vs mystr)?
  • CListCtrl is showing different theme for Unicode and Multi byte character set
  • Delete Inherited class Pointer Array in C++
  • error: constexpr variable 'struct2Var' must be initialized by a constant expression
  • Possible problem of gcc with sleep_for and sleep_until functions
  • why does argv remove some of my characters?
  • Should I call processEvents() on a thread?
  • Is it possible / desirable to create non-copyable shared pointer analogue (to enable weak_ptr tracking / borrow-type sem
  • pthread works fine only if the number of threads is small
  • In C++ given one std::variant type, can one add additional types to make another variant type?
  • C++17 post increment operation
  • How to check if variable is of string type in template class?
  • C++ temporary variable lifetime?
  • Remove blank line in c++
  • I have to find the maximum sum of 4 numbers from an array of 5. My code fails for bigger numbers
  • TicTacToe with MiniMax algorithm 4x4
  • What is the difference between std::stable_partition() and std::partition()?
  • Filter out breaks based on stack trace
  • stable_partition on forward iterators
  • Problem with creating and accesing a 4D vector in c++
  • Linking DirectX
  • How to change and delete a variable in stack?
  • are there other ways to write a multidimensional array in an array?
  • Partial Specialization using a qualified name
  • Warning C6385 in Visual Studio
  • 'this' cannot be used in a constant expression C++
  • fill vector with random elements
  • Difference between return 0 and -1
  • Is it possible to store lambdas with different signatures in a std::vector and execute them (with their respective argum
  • Why isn't my GetProcessID function compiling in VS?
  • Implementing a non-copyable C++ class
  • Understanding index++ in single line of code
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk