471,831 Members | 1,260 Online

# manipulating linked list

Hi all,

I have a linked list in which each node is of follwoing type.

struct node {
struct node *fptr;
int data;
int flag; /* flag could be ORIG=1 or DUPLICATE=2 */
}

I need to write a function, that would manipulate the linked list so
that all the nodes
with flag = DUPLICATE are put at the end of the linked list.

for eg, let the original list be,
head---->DUPLICATE---->ORIG---->ORIG---->DUPLICATE ----->ORIG--->
NULL

The final list after manipulation should be,
head--->ORIG----->ORIG----->ORIG---->DUPLICATE------>DUPLICATE
------>NULL

Only requirement is that the nodes with flag = DUPLICATE should lie
at the end.

I don't want the exact code, I just need an algorithm or some hints.

Many thanks for any help in advance ...

Aug 3 '06 #1
6 2300

ju**********@yahoo.co.in wrote On 08/03/06 09:56,:
Hi all,

I have a linked list in which each node is of follwoing type.

struct node {
struct node *fptr;
int data;
int flag; /* flag could be ORIG=1 or DUPLICATE=2 */
}

I need to write a function, that would manipulate the linked list so
that all the nodes
with flag = DUPLICATE are put at the end of the linked list.

for eg, let the original list be,
head---->DUPLICATE---->ORIG---->ORIG---->DUPLICATE ----->ORIG--->
NULL

The final list after manipulation should be,
head--->ORIG----->ORIG----->ORIG---->DUPLICATE------>DUPLICATE
------>NULL

Only requirement is that the nodes with flag = DUPLICATE should lie
at the end.

I don't want the exact code, I just need an algorithm or some hints.
Let the original list be L. Make two new lists L1,L2,
initially empty. Remove items from L one by one; append
each ORIG item to L1 and each DUPLICATE item to L2. Then
append L2 to L1 and return L1 as the result. Be careful of
boundary cases: L empty to begin with, L1 and/or L2 empty
after traversing L, etc.

--
Er*********@sun.com

Aug 3 '06 #2
ju**********@yahoo.co.in wrote:
Hi all,

I have a linked list in which each node is of follwoing type.

struct node {
struct node *fptr;
int data;
int flag; /* flag could be ORIG=1 or DUPLICATE=2 */
}

I need to write a function, that would manipulate the linked list so
that all the nodes
with flag = DUPLICATE are put at the end of the linked list.

for eg, let the original list be,
head---->DUPLICATE---->ORIG---->ORIG---->DUPLICATE ----->ORIG--->
NULL

The final list after manipulation should be,
head--->ORIG----->ORIG----->ORIG---->DUPLICATE------>DUPLICATE
------>NULL

Only requirement is that the nodes with flag = DUPLICATE should lie
at the end.

I don't want the exact code, I just need an algorithm or some hints.

Many thanks for any help in advance ...
Original list == A
New list == B
While A not null:
if A.duplicate:
append to B
remove from A
end if
next A
end While
Append B to A

Aug 3 '06 #3
In article <1154617512.341234@news1nwk>
Eric Sosman <Er*********@sun.comwrote:
Let the original list be L. Make two new lists L1,L2,
initially empty. Remove items from L one by one; append
each ORIG item to L1 and each DUPLICATE item to L2. Then
append L2 to L1 and return L1 as the result. Be careful of
boundary cases: L empty to begin with, L1 and/or L2 empty
after traversing L, etc.
There is a nice method of doing this in C that is difficult
in many other languages:

struct list *l1, *l2;
struct list **l1end = &l1, **l2end = &l2;
struct list *p;

for (p = orig_list; p != NULL; p = p->next) {
if (goes_in_l1(p)) {
*l1end = p;
l1end = &p->next;
} else {
*l2end = p;
l2end = &p->next;
}
}
*l2end = NULL;
*l1end = l2;

/* new list is now headed by l1 */

The order of the last two lines is important. (Exercise: why?)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 3 '06 #4

Chris Torek wrote:
In article <1154617512.341234@news1nwk>
Eric Sosman <Er*********@sun.comwrote:
Let the original list be L. Make two new lists L1,L2,
initially empty. Remove items from L one by one; append
each ORIG item to L1 and each DUPLICATE item to L2. Then
append L2 to L1 and return L1 as the result. Be careful of
boundary cases: L empty to begin with, L1 and/or L2 empty
after traversing L, etc.

There is a nice method of doing this in C that is difficult
in many other languages:

struct list *l1, *l2;
struct list **l1end = &l1, **l2end = &l2;
struct list *p;

for (p = orig_list; p != NULL; p = p->next) {
if (goes_in_l1(p)) {
*l1end = p;
l1end = &p->next;
} else {
*l2end = p;
l2end = &p->next;
}
}
*l2end = NULL;
*l1end = l2;

/* new list is now headed by l1 */

The order of the last two lines is important. (Exercise: why?)
assert(l1end->next == l2end);

A perfect demonstration of the right place to use assert!

<OT I would call then temporary pointers l1head and l2head.
In other words, you can add things to the start of a list, but not
the end. I am apparently at odds with the rest of the universe on
this, but wasn't aware of it until a few months ago. When did
we start calling the head the "end"? Has it always been that way, or
am
I going insane? Does "head" imply "start" only to me?
</OT>

--
Bill Pursell

Aug 4 '06 #5
"ju**********@yahoo.co.in" wrote:
>
I have a linked list in which each node is of follwoing type.

struct node {
struct node *fptr;
int data;
int flag; /* flag could be ORIG=1 or DUPLICATE=2 */
}

I need to write a function, that would manipulate the linked list
so that all the nodes with flag = DUPLICATE are put at the end of
the linked list.
Simply sort the list with mergesort, using 'flag' as the sort
field. You can find some examples in my published source at:

<http://cbfalconer.home.att.net/download/>

I suspect hashlib application examples will have suitable example
code.

--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.netUSE maineline address!
Aug 5 '06 #6
In article <ea*********@news1.newsguy.comI wrote, in part:
> struct list *l1, *l2;
struct list **l1end = &l1, **l2end = &l2;
struct list *p;

for (p = orig_list; p != NULL; p = p->next) {
if (goes_in_l1(p)) {
*l1end = p;
l1end = &p->next;
} else {
*l2end = p;
l2end = &p->next;
}
}
*l2end = NULL;
*l1end = l2;

/* new list is now headed by l1 */

The order of the last two lines is important. (Exercise: why?)
In article <11**********************@b28g2000cwb.googlegroups .com>,
Bill Pursell <bi**********@gmail.comwrote:
>assert(l1end->next == l2end);
This should not even compile (unless you turn on NDEBUG), since
l1end->next has type "struct list *", while "l2end" has type
"struct list **".
><OT I would call then temporary pointers l1head and l2head.
In other words, you can add things to the start of a list, but not
the end.
But that is not what they are; and this code *does* add things to
the end of each list. That is the tricky thing about l1end and
l2end: each time, it points to the "struct list *" that should
point to a new item in the corresponding list. Initially, the
"struct list *" that should point to the new items are "l1" and
"l2" respectively, but as items are added to those lists, they
change.

That is, initially, we have (l1end == &l1), but once there is
one item in list l1, we have (l1end == &l1->next). When there
are two items in list l1, we have (l1end == &l1->next->next),
and so on.

It is possible to avoid the need to set *l2end to NULL before
setting *l1end to l2, by initializing l2 to NULL at the top of the
code. The assignment to *l2end is then always redundant (in the
code shown above, it is redundant if and only if at least one item
has been placed into list l2).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 7 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 3 posts views Thread by da Vinci | last post: by 11 posts views Thread by C++fan | last post: by 5 posts views Thread by Dream Catcher | last post: by 10 posts views Thread by Kent | last post: by 6 posts views Thread by Steve Lambert | last post: by 12 posts views Thread by joshd | last post: by 51 posts views Thread by Joerg Schoen | last post: by reply views Thread by Atos | last post: by 7 posts views Thread by QiongZ | last post: by reply views Thread by Dimash Kudaiber | last post: by reply views Thread by NeoPa | last post: by 1 post views Thread by guiromero | last post: by 7 posts views Thread by Petrol | last post: by reply views Thread by aboka | last post: by 9 posts views Thread by CD Tom | last post: by 2 posts views Thread by beacampos | last post: by reply views Thread by beacampos | last post: by reply views Thread by RonaldHiggins22 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.