472,967 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Remove "smallest" element from a linked list?

I have a simple linked list:
struct element {
struct element *next;
int start;
};

struct list {
struct element *head;
struct element *tail;
};
I would not like to make a function that removes the element with the
smallest start value.

If I have three elements in my list with the following start values:
12->5->14, the list should after the remove operation look like this:

12->14

I was thinking that I would run through all the elements in the list
and store their start value in an array:

struct element *find = list.head;
while(find != 0)
{
// store the value in an array

// jump to the next element
find = find->next;
}

I would now search through this array and return the index of the
smallest value. This value +1 is the number of times I need to travel
the head of the lists next pointer to find the smallest element in the
list. I would then have another procedure to return this element and
fix the list.
The above method seems a bit messy, is there a more optimal solution to
remove a value from a linked list with a smallest key??

Mar 29 '06 #1
4 9673
eksamor opined:
I have a simple linked list:
struct element {
struct element *next;
int start;
};

struct list {
struct element *head;
struct element *tail;
};
I would not like to make a function that removes the element with the ^^^

I assume you mean "now"...
smallest start value.

If I have three elements in my list with the following start values:
12->5->14, the list should after the remove operation look like this:

12->14

I was thinking that I would run through all the elements in the list
and store their start value in an array:

struct element *find = list.head;
while(find != 0)
{
// store the value in an array

// jump to the next element
find = find->next;
}

I would now search through this array and return the index of the
smallest value. This value +1 is the number of times I need to travel
the head of the lists next pointer to find the smallest element in
the list. I would then have another procedure to return this element
and fix the list.

The above method seems a bit messy, is there a more optimal solution
to remove a value from a linked list with a smallest key??


Yes, by keeping the pointer to the smallest element you found, as well.
Then, you can go directly there, and pluck it out. On second thought,
you'd also need the pointer to the one preceding it...

Not really a C question, though...

--
BR, Vladimir

Mother told me to be good, but she's been wrong before.

Mar 29 '06 #2
>I have a simple linked list:
struct element {
struct element *next;
int start;

};
struct list {
struct element *head;
struct element *tail;
};
I would not like to make a function that removes the element with the
smallest start value. If I have three elements in my list with the following start values:
12->5->14, the list should after the remove operation look like this:
12->14
I was thinking that I would run through all the elements in the list
and store their start value in an array:


why would you do that? You already have elements stored in linked list.
Just search the linked list for the smallest value

int check;
struct element *smallest;
struct element *previous;
struct element *prev;

struct element *find = list.head;
check = find->start;
while(find != 0)
{
// store the smallest value in check
if(check<find->start)
{
check = find->start;
smallest = find;
prev = previous;
}

// jump to the next element
previous = find;
find = find->next;
}
[snip]

I have not tested this code but i hope this gives you an idea of how to
remove the smallest element without using the array. As using array of
same number of elements will be against the whole idea of using the
linked list.

You can use the *smallest* as the node with smallset value and delete
it and use *prev* as the pointer to the previous node.

Mar 29 '06 #3

eksamor wrote:
I have a simple linked list:
struct element {
struct element *next;
int start;
};

struct list {
struct element *head;
struct element *tail;
};
I would not like to make a function that removes the element with the
smallest start value.
Not ???
If I have three elements in my list with the following start values:
12->5->14, the list should after the remove operation look like this:

12->14

I was thinking that I would run through all the elements in the list
and store their start value in an array:

struct element *find = list.head;
while(find != 0)
{
// store the value in an array

// jump to the next element
find = find->next;
}

I would now search through this array and return the index of the
smallest value. This value +1 is the number of times I need to travel
the head of the lists next pointer to find the smallest element in the
list. I would then have another procedure to return this element and
fix the list.
The above method seems a bit messy, is there a more optimal solution to
remove a value from a linked list with a smallest key??


So here's what you do :
1. One complete traversal of list to iterate through all elements and
put them into array.
2. One complete traversal of array to find the smallest element.
3. Another traversal of list to move to the smalles value element (Keep
another pointer one node before that for easy deletion or follow that
trick of deleting the element on which you are currently by copying the
value of next element into current node and then deleting the next
node, more mess)

Instead why not traverse through the list at step 1 and simultaneously
find out the smallest element and delete that. That would avoid Steps 2
and 3 above. I guess it would be better than your existing solution.

Mar 29 '06 #4
eksamor wrote:

I have a simple linked list:

struct element {
struct element *next;
int start;
};

struct list {
struct element *head;
struct element *tail;
};

I would not like to make a function that removes the element with
the smallest start value.

If I have three elements in my list with the following start values:
12->5->14, the list should after the remove operation look like this:

12->14

I was thinking that I would run through all the elements in the list
and store their start value in an array:

struct element *find = list.head;
while(find != 0)
{
// store the value in an array

// jump to the next element
find = find->next;
}

I would now search through this array and return the index of the
smallest value. This value +1 is the number of times I need to
travel the head of the lists next pointer to find the smallest
element in the list. I would then have another procedure to return
this element and fix the list.

The above method seems a bit messy, is there a more optimal
solution to remove a value from a linked list with a smallest key??


You define lists as starting with a dummy element, whose only
purpose is to point to the first element on the list proper. Then
you define searching for an element as searching for the element
before the one you want, i.e. the one which points to the actual
required element. Then you have no difficulty deleting elements,
inserting before elements, etc.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 29 '06 #5

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

Similar topics

55
by: Dev | last post by:
Hello Folks, I had faced this objective in one of my aptitude exams, that "What could be the smallest "C" program? And, as we know, smallest program means, it should execute single statement,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.