简体   繁体   中英

reverse printing of linked list in c

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;
}

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

Change

curr=head;

with

curr = tail;

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;
}

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;
}

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);
    }
}

当退出循环时,您的head (仅在添加第一个元素时更新一次)指向最后一个元素。

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

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;
}

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 (ie, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM