473,623 Members | 3,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linked list need help...

hi all,
i need help with linked lists...
the problem is this, "reverse the contents of a singly linked list
without using a temporary node"...
solution with code will be appreciated...

Nov 15 '05 #1
13 2177
XXXXXX.working. in.my.blood wrote:
hi all,
i need help with linked lists...
the problem is this, "reverse the contents of a singly linked list
without using a temporary node"...
solution with code will be appreciated...

It's not going to happen. We don't do homework assignments, sorry.

You're free to ask specific questions on a solution you've come up with
and can't seem to complete, and we'll be happy to answer any language
questions you might have.

But do your own homework first.

Oh, and if you're that hell-bent on getting the quick and easy solutions
without understanding: consider using Google. This problem is very old.

S.
Nov 15 '05 #2
i don want to be rude but i was asked this question by one of the HRs
in an interview. i couldn't answer the question, i dont admit that i
coudn't get the job coz' i didn't answer this but just want to know,
if u don have an answer just leave it, and i am not asking anyone to do
homework assignments its just a request.
thank you very much for the help
....

Nov 15 '05 #3
XXXXXX.working. in.my.blood wrote:

hi all,
i need help with linked lists...
the problem is this, "reverse the contents of a singly linked list
without using a temporary node"...
solution with code will be appreciated...


I can't imagine how a temporary node would be helpful.

/* BEGIN list.h */

#ifndef H_LIST
#define H_LIST

typedef struct list_node {
struct list_node *next;
void *data;
} list_type;

list_type *list_make(long unsigned);
list_type *list_rev(list_ type *);
list_type *list_sort(list _type *,
int (*)(const list_type *, const list_type *));
void list_free(list_ type *, void (*)(void *));

#endif

/* END list.h */

/* BEGIN list.c */

#include <stdlib.h>

#include "list.h"

static long unsigned list_count(list _type *);
static list_type *node_sort(list _type *, long unsigned,
int (*)(const list_type *, const list_type *));
static list_type *list_merge(lis t_type *, list_type *,
int (*)(const list_type *, const list_type *));
static list_type *list_split(lis t_type *, long unsigned);

list_type *list_rev(list_ type *head)
{
list_type *next_node, *previous;

if (head != NULL) {
next_node = head -> next;
head -> next = NULL;
while (next_node != NULL) {
previous = next_node -> next;
next_node -> next = head;
head = next_node;
next_node = previous;
}
}
return head;
}

list_type *list_make(long unsigned count)
{
list_type *node, *list;

list = count != 0 ? malloc(sizeof *list) : NULL;
if (list != NULL) {
node = list;
while (--count != 0) {
node -> data = NULL;
node -> next = malloc(sizeof *node -> next);
if (node -> next == NULL) {
list_free(list, free);
return NULL;
} else {
node = node -> next;
}
}
node -> data = NULL;
node -> next = NULL;
}
return list;
}

void list_free(list_ type *node, void (*free_data)(vo id *))
{
list_type *next_node;

while (node != NULL) {
next_node = node -> next;
free_data(node -> data);
free(node);
node = next_node;
}
}

list_type *list_sort(list _type *head,
int (*compar)(const list_type *, const list_type *))
{
return node_sort(head, list_count(head ), compar);
}

static long unsigned list_count(list _type *head)
{
long unsigned count;

for (count = 0; head != NULL; head = head -> next) {
++count;
}
return count;
}

static list_type *node_sort(list _type *head, long unsigned count,
int (*compar)(const list_type *, const list_type *))
{
long unsigned half;
list_type *tail;

if (count > 1) {
half = count / 2;
tail = list_split(head , half);
tail = node_sort(tail, count - half, compar);
head = node_sort(head, half, compar);
head = list_merge(head , tail, compar);
}
return head;
}

static list_type *list_split(lis t_type *head, long unsigned count)
{
list_type *tail;

while (count-- > 1) {
head = head -> next;
}
tail = head -> next;
head -> next = NULL;
return tail;
}

static list_type *list_merge(lis t_type *head, list_type *tail,
int (*compar)(const list_type *, const list_type *))
{
list_type *list, *sorted, **node;

node = compar(head, tail) > 0 ? &tail : &head;
sorted = list = *node;
*node = sorted -> next;
while (*node != NULL) {
node = compar(head, tail) > 0 ? &tail : &head;
sorted -> next = *node;
sorted = *node;
*node = sorted -> next;
}
sorted -> next = head != NULL ? head : tail;
return list;
}

/* END list.c */

/* BEGIN list_sort.c */

#include <stdio.h>
#include <stdlib.h>

#include "list.h"

#define NODES 17
#define LU_RAND_SEED 123456789LU
#define LU_RAND(S) ((S) * 69069 + 362437 & 0xffffffff)

struct score {
float literature;
float history;
float sociology;
};

int hist_score_comp (const list_type *, const list_type *);
void score_init(list _type *, long unsigned);
void score_print(lis t_type *);

int main(void)
{
list_type *score;

score = list_make(NODES );
if (score == NULL) {
fputs("The score list was not allocated.\n", stderr);
exit(EXIT_FAILU RE);
}
score_init(scor e, LU_RAND_SEED);
puts("BEGIN output from list_sort.c\n\n Random order");
score_print(sco re);
puts("Sorted on history");
score = list_sort(score , hist_score_comp );
score_print(sco re);
puts("Reverse Sorted order");
score = list_rev(score) ;
score_print(sco re);
puts("END output from list_sort.c\n") ;
list_free(score , free);
return 0;
}

void score_init(list _type *node, long unsigned seed)
{
while (node != NULL) {
node -> data = malloc(sizeof (struct score));
if (node -> data == NULL) {
fputs("malloc problem\n", stderr);
exit(EXIT_FAILU RE);
}
seed = LU_RAND(seed);
((struct score *)node -> data) -> literature
= seed % 40 + 60.0f;
seed = LU_RAND(seed);
((struct score *)node -> data) -> history
= seed % 40 + 60.0f;
seed = LU_RAND(seed);
((struct score *)node -> data) -> sociology
= seed % 40 + 60.0f;
node = node -> next;
}
}

void score_print(lis t_type *node)
{
float score;

while (node != NULL) {
score = ((struct score *)node -> data) -> literature;
printf("literat ure %d ", (int)score);
score = ((struct score *)node -> data) -> history;
printf("history %d ", (int)score);
score = ((struct score *)node -> data) -> sociology;
printf("sociolo gy %d\n", (int)score);
node = node -> next;
}
putchar('\n');
}

int hist_score_comp (const list_type *head, const list_type *tail)
{
float first = ((struct score *)head -> data) -> history;
float second = ((struct score *)tail -> data) -> history;

return second > first ? -1 : second != first;
}

/* END list_sort.c */
--
pete
Nov 15 '05 #4
"XXXXXX.working .in.my.blood" <bh********@gma il.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
i don want to be rude
Then don't :)
but i was asked this question by one of the HRs
in an interview.
That's fine. But we're not preparing or training for interviews in this
group, we're to help with standard C here, which, believe or not, has
vanishingly little to do with this your problem of singly-linked list
reversal. What you want is an algorithm. And its implementation (please note
that implementation != algorithm) could be done in Pascal or any other
language supporting pointers/references/links/whatever. So, why this group?
i couldn't answer the question, i dont admit that i
coudn't get the job coz' i didn't answer this but just want to know,

....

On that matter, I'd consider preparing for the interview longer and better.
There're lots of typical interview questions for programmers on the net. Try
finding and solving those that are told to be asked at microsoft. I found
those (about 100) and had a like a month of fun solving them (no I wasn't
solving them from 9am to 6pm, I had other things to do, my job, that's why
it wasn't quick). And there were questions going far beyond simple list
reversal. If it's (still) important to you, do the same. At least give it a
good try, crack your brains solving them. And it's best if you do it w/o
external help -- that's how you can make your head work and see what you
can.

Alex
Nov 15 '05 #5
"XXXXXX.working .in.my.blood" <bh********@gma il.com> wrote in
news:11******** **************@ o13g2000cwo.goo glegroups.com:

hi all,
i need help with linked lists...
the problem is this, "reverse the contents of a singly linked list
without using a temporary node"...
Why would you need a temp node? Just reverse all the pointers.
solution with code will be appreciated...


Solution above; code denied.
Nov 15 '05 #6
pete ha scritto:
#ifndef H_LIST
#define H_LIST


Why the above directives?
Thanks
Nov 15 '05 #7
"Alexei A. Frounze" <al*****@chat.r u> wrote in message
news:3q******** ****@individual .net...
"XXXXXX.working .in.my.blood" <bh********@gma il.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
but i was asked this question by one of the HRs
in an interview.
That's fine. But we're not preparing or training for interviews in

this group, we're to help with standard C here, which, believe or not, has
vanishingly little to do with this your problem of singly-linked list
reversal. What you want is an algorithm. And its implementation (please note that implementation != algorithm) could be done in Pascal or any other
language supporting pointers/references/links/whatever. So, why this group?

The ad I saw said said don't submit a resume unless you could solve the
problem in 20 minutes in any language. I wrote up a C version in about
25 while watching TV (ie: during commercials). It certainly seemed like
homework, since the constraint of a singly-linked list that you want to
transverse two ways is very artificial. I almost wrote a nasty email to
the firm, but I am much too shy and subtle...
i couldn't answer the question, i dont admit that i
coudn't get the job coz' i didn't answer this but just want to

know,

It's not difficult, but once reversed, you lose the forward reference -
you'd have to re-reverse it to get back to the original list. It takes
N+(N-1)+(N-2) ... + 1 operations in my algorithm. Of course, you could
just set up a second list that was doubly-linked and would be just
pointers to the old list. That would take one pass, well two if you then
update the original list. Damn I should have thought of that before!
Hey! Can I have my interview back...?! :-0
On that matter, I'd consider preparing for the interview longer and better. There're lots of typical interview questions for programmers on the net. Try finding and solving those that are told to be asked at microsoft. I found those (about 100) and had a like a month of fun solving them (no I wasn't solving them from 9am to 6pm, I had other things to do, my job, that's why it wasn't quick). And there were questions going far beyond simple list reversal. If it's (still) important to you, do the same. At least give it a good try, crack your brains solving them. And it's best if you do it w/o external help -- that's how you can make your head work and see what you can.


"see what you can." Can WHAT?!!

--
Mabden
Nov 15 '05 #8
__frank__ a écrit :
pete ha scritto:
#ifndef H_LIST
#define H_LIST
Why the above directives?


It's a guard against multiple inclusions.

When you write a lbrary, you can't know in advance how will the headers
be included by the users. This guard prevents against multiple
inclusions into a same compile unit.
Nov 15 '05 #9
the problem is this, "reverse the contents of a singly linked list
without using a temporary node"...

You can use two temp pointers ( note that it is not a temporary node)
and rearrange all the pointers .... Try to figure out the algorithm ...

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4817
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am ordered by my instructor and she does have her reasons so I have to use char*. So there is alot of c in the code as well Anyways, I have a linked list of linked lists of a class we defined, I need to make all this into a char*, I know that I...
10
5480
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the following: struct Node { int integer_value;
5
2319
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set up the class link and tel_list. I set up a class person with strings for name, town, and number. I just don't know how to set up the classes w/ their methods (constructors and operations). I'm stuck on the assignment operator and the add and...
8
5794
by: simpleman | last post by:
Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the linked list since the input will all be in one line. eg. 1234567890. Also I have to use one integer per node. Any help/suggestion will be greatly appreciated. Thanks in advance
5
6049
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user hits the right and left keys to move this insertion point (cursor) Here is the problem:
1
3296
by: Little | last post by:
Hello everyone. I am trying to do the following program and am unable to get the beginning portion to work correctly. The scanner works when I print the statements without the double linked list portion but I just need help with the beginning portion with the double linked lists. Here is the information needed to understand the code: Create 4 double linked lists as follows: (a) A double linked list called NAMES which will contain all...
12
3941
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 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
1
15531
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked list. So on the list I can go Trevor, Kevin, Allan in a straight row but I can also call out there last name when I am on their first name in the list. Sorry if it doesn't make sense trying to explain best I can. So far I have // list.cpp
2
1686
by: phiefer3 | last post by:
Ok, first of all I'm not sure if this is the correct forum for this question or not. But hopefully someone can help me or at least point me in the direction of the forum this belongs. First of all, I am using C++, however it's managed C++ or visual C++, or whatever microsoft calls it. I'm using MSVS2005, and working on a Windows Forms Application project from the C++ projects tab. I'm pointing this out because apparently the syntax used in...
7
5761
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
8221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6104
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5560
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2593
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1468
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.