Link list problem->can't print the output...

This is a discussion on Link list problem->can't print the output... within the C Programming forums, part of the General Programming Boards category; Code: /*The code*/ #include<stdio.h> #include<stdlib.h> typedef struct node { int ele; struct node *next; }node; void insertnode(int x); void deletenode(int ...

  1. #1
    frankiepoon
    Guest

    Unhappy Link list problem->can't print the output...

    Code:
    /*The code*/
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct node {
    	int ele;
    	struct node *next;
    
    }node;
    
    void insertnode(int x);
    void deletenode(int x);
    void printlist();
    node *head=NULL;
    
    main(){
    int i=0;
    int num;
    
    while (i<5){
    	//printf("Enter a element:");
    	//scanf("%d",&num);
    	insertnode(i);
    i=i+1;
    }
    printlist();
    return 0;
    }
    
    void insertnode(int x){
    	node *newnode;
    	newnode=(node*)malloc(sizeof(node));
    	newnode->next=head;
    	head=newnode;
    }
    
    void printlist(){
    	node *current=head;
    
    	printf("Insert order:");
    	while(current!=NULL){
                       printf("%d",current->ele);
    	   current=current->next;
    	}
    
    }
    Code tagged by Hammer

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    First off, you're not actually placing the data into the list.

    Second:

    Code:
    void insertnode(int x){
    node *newnode;
    newnode=(node*)malloc(sizeof(node));
    //...each time, you point the new node's next pointer to the head, which...
    newnode->next=head; 
    //...then gets pointed to the new node!
    head=newnode; 
    }
    This yeilds something like:

    newnode-(next)->newnode

    So here's how to remedy this:

    1) declare a NULL traversal pointer, attach to head.
    1) Traverse the length of head till you hit a NULL.
    2) point traverse to newnode
    3) set newnode->next to NULL.

    That's it! Good luck.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Link List output
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-30-2001, 08:57 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21