0

I am trying to print the reverse linked list. But I am getting only one value. Where am I going wrong? Please bear with me as I am new to C.

#include<stdio.h>
#include<stdlib.h>

struct itemlist {
    int value;
    struct itemlist *next;
};

typedef struct itemlist item;

int main(void) {
    itemlist *curr,*head,*tail;

    head=NULL;
    tail=NULL;

    for (int i = 1; i < 10; i++) {
        curr=(itemlist *)malloc(sizeof(itemlist));
        curr->value=i;
        curr->next=tail;
        tail=curr;
        if (!head)
            head=curr;
    }

    curr=head;

    while (curr) {
        printf("Curr value is:%d\n",curr->value);
        curr=curr->next;
    }

    return 0;
}
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
Teja
  • 13,214
  • 36
  • 93
  • 155

8 Answers8

1

Seems like you should start print your list from tail, not from head.

Change

curr=head;

with

curr = tail;
vard
  • 2,142
  • 4
  • 30
  • 40
1

Change curr=head to curr=tail

Below is a simple example illustrating a bi-directional linked list which should get you on your way to understanding linked lists

#include<stdio.h>
#include<stdlib.h>

typedef struct
{
        int value;
        struct itemlist *next;
        struct itemlist *prev;
}itemlist;

void forward(itemlist *head)
{
    itemlist *curr = head;
    while(curr)
    {
        printf("Curr value is: %d\n", curr->value);
        curr = curr->next;
    }
}

void backward(itemlist *tail)
{
    itemlist *curr = tail;
    while(curr)
    {
        printf("Curr value is: %d\n", curr->value);
        curr = curr->prev;
    }
}

int main(void)
{
        itemlist *curr,*head,*tail;

        head=NULL;
        tail=NULL;

        for(int i=1;i<10;i++)
        {
                curr=(itemlist *)malloc(sizeof(itemlist));
                curr->value=i;
                curr->next = NULL;
                if(tail)
                {
                    curr->prev = tail;
                    tail->next = curr;
                }
                tail=curr;
                if(!head)
                    head=curr;
        }

        printf("Forwards\n");
        forward(head);
        printf("Backwards\n");
        backward(tail);

        return 0;
}
lukecampbell
  • 14,728
  • 4
  • 34
  • 32
  • Even if I change curr=head to curr=tail.. I get all values from 9 to 1 but I need 1 to 9. – Teja Apr 27 '12 at 20:01
  • @Vutukuri so you want the values in order not in reverse order? What you are doing is an add to front on the list. You need to do an add to back to get the list in the other direction. – twain249 Apr 27 '12 at 20:03
  • @Vutukuri I added forwards and backwards linked list to the example – lukecampbell Apr 27 '12 at 20:10
1

This code prints 1 to 9

#include<stdio.h>
#include<stdlib.h>

struct itemlist
{
        int value;
        struct itemlist *next;
};

typedef struct itemlist item;

int main(void)
{
        itemlist *curr,*head,*prev;

        head=NULL;
        curr=NULL;
        prev=NULL;

        for(int i=1;i<10;i++)
        {
                curr = new itemlist;
                curr->value = i;
                curr->next = NULL;

                if (head == NULL)
                   head = curr;
                if(prev != NULL)
                   prev->next = curr;

                prev = curr;
        }

        curr=head;

        while(curr)
        {
                printf("Curr value is:%d\n",curr->value);
                curr=curr->next;
        }
        return 0;
}
jlunavtgrad
  • 997
  • 1
  • 11
  • 21
1

You don't need a tail at all, but only need a recursive function. The function points to next by itself before the printf.

void print_reverse(Itemlist *it){
    if(it !=NULL){
        print_reverse(it->next);
        printf("Current value is:%d\n",it->value);
    }
}
Automate This
  • 30,726
  • 11
  • 60
  • 82
kumistheru
  • 11
  • 1
0

When you exit the loop your head (which was updated only once and when the first element was added) points to the last element.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
0

The problem is that, in the first iteration, when you assign the next element of the current item:

curr->next=tail;

the value of tail is NULL, so the head of your list can't reach the rest of it

Win32
  • 1,109
  • 10
  • 12
0

You're head and tail are mixed up:

The first Node (Node 0):

Value = 1
next = tail = NULL;
tail = Node 0
head = Node 0 

The second Node (Node 1):

Value = 2
next = tail = Node 0 
tail = Node 1 
head = Node 0 

Now you have

Node 1 -> Node 0 -> NULL and head = Node 0 and tail = Node 1

So when you print you start at Node 0 (the "head" but it's actually the tail) Print the first node then end

You have to either switch head and tail to be the correct names or start printing with tail

Edit: Since you say you want them in order you can do this:

int main(void)
{
   itemlist *curr,*head,*tail;

   head=NULL;
   tail=NULL;

   for(int i=1;i<10;i++)
   {
       curr=(itemlist *)malloc(sizeof(itemlist));
       curr->value=i;
       curr->next = NULL;

       //if there is something in the list add the current node after it              
       if(tail) 
          tail->next = curr;

       //Update the tails so it's pointing to the current last node         
       tail = curr;

       //Set the head ONCE
       //this will happened the first time when if(tail) fails
       if(!head)
         head=curr;
  }

  //start at the head
  curr=head;

  while(curr)
  {
       printf("Curr value is:%d\n",curr->value);
       curr=curr->next;
  }
  return 0;
}
twain249
  • 5,666
  • 1
  • 21
  • 26
0

At least as I understand it, your plan is to create a linked list in reverse order, then print out its contents. If so, you probably want something like this:

#include <stdlib.h>
#include <stdio.h>

struct itemlist {
    int value;
    struct itemlist *next;
};

int main() { 

    struct itemlist *head = NULL;
    struct itemlist *pos;
    int i;

    for (i=0; i<10; i++) {
        struct itemlist *node = malloc(sizeof(*node));
        node->value = i;
        node->next = head;
        head = node;
    }

    for (pos=head; NULL != pos; pos = pos->next)
        printf("%d\n", pos->value);
    return 0;
}

Note that you don't need a pointer to the "tail". The basic idea is pretty simple: start with head as an empty list (i.e., a null pointer). Insert each new node at the beginning of the list by setting its next pointer to the current beginning of the list, then setting the beginning of the list to point to the new node.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111