473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2432


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
L7
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: da Vinci | last post by:
OK, this is a pretty weird question, but shouldn't be to hard to accomplish. Remember, I am a beginner so please, if I say something stupid, be gentle. :-) I do not know vectors yet, which would...
11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.