Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reversing the elements of a list & printing them

Posted on 1999-07-24
3
Medium Priority
?
258 Views
Last Modified: 2010-04-15
HOW DO I REVERSE THE ELEMENTS OF A LIST & PRINT THEM.
0
Comment
Question by:k_f_v
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
shlomoy earned 300 total points
ID: 1263870
let's assume I have a struct:

typedef struct list {
   element* head;  /* head of the list */
   /* you might want to have more data fields here */
} List;

the count data-fiels should be increased by 1 on every element-adding to the list and decreased by 1 on every element-removal from the list.


I assume you have a list of elements (where 'element' is some sort of a struct). I assume that element looks like this:

typedef struct element {
   Key  key;
   Data data;
   struct element* next;
} Element;

Key and Data are the types of your key and data data-fields. you obviously know what next means...

Let's assume I have the following functions available:
int isEmpty(List list); /* 1 if empty 0 otherwise */
void printElement( Element element )
/* prints the data data-field of the given element */

If you only want to print your list in reversed order and not actually reverse the list you might try a recursive rutine:

first save the list->head pointer somewhere before you invoke the following rutine (since it alters the pointers):

void reversePrint( List list ) {
   if (isEmpty(list))
      return;
   list->head = list->head->next;
   reversePrint(list);
   printElement(list->head);
   return;
}

this function will work as long as your list has no loops (as lons as the last element of the list has it's next data-field pointing to a list-terminator, NULL, for example).

if you ant more solutions, just say so.

0
 

Author Comment

by:k_f_v
ID: 1263871
thankx again
0
 
LVL 8

Expert Comment

by:shlomoy
ID: 1263872
:-) sure.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

662 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question