some interview questions.

D

David Resnick

Wolfgang Riedel said:
pedantic mood on:

while (ptail->next != NULL) {
ptail = ptail->next;
pfifth = pfifth->next;

at least in c.l.c

Wolfgang


Entirely agree, brain was totally out of gear.

-David
 
C

Christopher Benson-Manica

Dave Neary said:
and conversely if i have something like
int arr[6][7];
int *ptr;
what can i do so that i can assign it like this
ptr = arr;
You can't.

Well, technically OP could (correct me if I'm wrong) by changing the
declarations...

int arr[6][7];
int (*ptr)[7]; /* ptr points to an array of 7 elements */
ptr=arr; /* legal, yes? */

Not that that's particularly useful or anything...
 
C

CBFalconer

Wolfgang said:
pedantic mood on:

while (ptail->next != NULL) {
ptail = ptail->next;
pfifth = pfifth->next;

at least in c.l.c

I maintained earlier that use of list reversal would simplify
matters. At any rate, just for sport I implemented both that and
the trailing pointer method below. The code has been compiled and
run, and seems correct apart from a one-off error in the trailing
pointer method. I am leaving that error in here, because I think
its existance is instructive. Note that the initialization
displays the list with the head last.

-------------- code begins here ---------------
#include <stdio.h>
#include <stdlib.h>

/* THERE ARE ERRORS HERE - possibly instructive */

#define POSN 5

struct node {
struct node *next;
int val;
};

/* ----------------- */

/* make a list */
static struct node *initialize(void)
{
struct node n, *p;
int i;

n.next = NULL;
for (i = 0; i < POSN + 2; i++) {
p = n.next;
if (!(n.next = malloc(sizeof *(n.next))))
exit(EXIT_FAILURE);
else {
n.next->val = rand();
n.next->next = p;
printf("%d ", n.next->val);
}
}
putchar('\n');
return n.next;
}

/* ----------------- */

/* believed necessary and sufficient for NULL terminations */
/* Reverse a singly linked list. Reentrant (pure) code */
static struct node *revlist(struct node *root)
{
struct node *curr, *nxt;

if (root) { /* non-empty list */
curr = root->next;
root->next = NULL; /* terminate new list */
while (curr) {
nxt = curr->next; /* save for walk */
curr->next = root; /* relink */
root = curr; /* save for next relink */
curr = nxt; /* walk onward */
}
}
/* else empty list is its own reverse; */
return root;
} /* revlist */

/* ----------------- */

static void tryreversal(struct node *root)
{
struct node *p;
int i;

root = revlist(root);
p = root;
for (i = 0; i < POSN; i++) p = p->next;
printf("%dth from end via reversal is %d\n", POSN, p->val);

/* This only if original list must be unharmed */
root = revlist(root);
} /* tryreversal */

/* ----------------- */

static void trytrailingptrs(struct node *root)
{
struct node *last, *trail;
int i;

trail = last = root;
for (i = 0; i < POSN; i++) last = last->next;
while (last) {
last = last->next;
trail = trail->next;
}
printf("%dth from end via trailer is %d\n", POSN, trail->val);
} /* trytrailingptrs */

/* ----------------- */

int main(void)
{
struct node *root;

root = initialize();

trytrailingptrs(root);
tryreversal(root);
return 0;
}
 
J

James Harris

message
It is quite a common name. There is one J. Harris living in the same
road as I, about 200 meters away from my home. What a frightening
thought.

Sorry. Can't answer to either of those.

.... Unless, of course, you are just down the road from me. Hmm,
what a frightening thought also!!!

-J
 
M

Michael Steve

CBFalconer said:
A simpler solution is:

1. reverse the list
2. print the 5th item
3. reverse the list (if original needed)

and the C operation of reversing a singly linked list in place is
extremely easy.

2 words.... Ring Buffer.

Michael Steve
The Sequel
 
P

pandapower

i also thought that keeping two pointers like done in mathematical
Instead of moving two pointers through the list (which
I agree violates the "single pass" requirement), you could
walk the list once and maintain a five-place array with the
most recent five pointer values:

The only hint i was given was if i could somehow relate it to relative
velocity concepts and find an answer.The answer i gave was as you
suggest,to keep an array of 5 latest members as i traverse the list,
and when i come to the end of the list i print the element.But i am a
bit confused how we can do that too,if the list finishes when in the
middle of the array.Probably we need to move the contents of the array
as we overwrite old values or probably a queue will be a better idea.


regards
 
C

CBFalconer

pandapower said:
.... snip ...

The only hint i was given was if i could somehow relate it to relative
velocity concepts and find an answer.The answer i gave was as you
suggest,to keep an array of 5 latest members as i traverse the list,
and when i come to the end of the list i print the element.But i am a
bit confused how we can do that too,if the list finishes when in the
middle of the array.Probably we need to move the contents of the array
as we overwrite old values or probably a queue will be a better idea.

Over 24 hours ago I published two solutions in a message msgid:
<[email protected]> It would appear you do not read the
answers that appear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top