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
Medium Priority
258 Views
HOW DO I REVERSE THE ELEMENTS OF A LIST & PRINT THEM.
0
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
• 2

LVL 8

Accepted Solution

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

typedef struct 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;
reversePrint(list);
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

ID: 1263871
thankx again
0

LVL 8

Expert Comment

ID: 1263872
:-) sure.

0

## Featured Post

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.
###### Suggested Courses
Course of the Month5 days, 13 hours left to enroll