I have written tree code. And I want to call it by any order in any function by any function call. i.e if I have
void inorder() {
inorder();
display();
preorder();
}
void preorder() {
inorder();
display() ;
postorder();
}
void postorder() {
postorder();
display();
postorder();
}
In this I have called first inorder then again inorder then preorder likewise how can I display function what I have called by sequence?
I try my best understanding. Excuse me if I misunderstood.
You mean you want to see how the functions are called in a tree tructure.
Then you can define a tree strucutre to store the function name. Make the construct globally accessible or pass as the parameter in each function. Everytime, you call a function, you create a tree node and put its children (here the internal called fucntions) in it by using linked list.
The process is gonna be like a DFS, only thing you need is a well-defined construct.
Related
I was searching on how can I print a reverse of linked list and I found this piece of code
/* Function to reverse the linked list */
void printReverse(struct node* head)
{
// Base case
if (head == NULL)
return;
// print the list after head node
printReverse(head->next);
// After everything else is printed, print head
printf("%d ", head->data);
}
The problem is that I do not understand the part where it reaches the last pointer which points to NULL and how it comes back one by one and prints the linked list in reverse order.
Is the return statement what makes it go step by step back? Or something else? Please help because I do not understand.
This is a recursive function. Recursive functions work by pushing variables onto the execution stack. The execution order pops off the stack in reverse order from the order it was pushed on the stack. This means that the last execution prints first, then the one previous to that, and so forth until all executions of printReverse print their values.
This is a recursive function; that is to say, it calls itself.
Let's use the sequence example as your example data:
When you first run the function, it's pointing at the head, and the head is the item containing the data e. It has a next property, which is the item containing the data x. Therefore, head == NULL evaluates False and the if statement doesn't fire.
The function then calls a second copy of itself, pointing to the next item in the list - the item containing x. This has a next property, which is the item containing a. The function then calls a third copy of itself with this item, and so on and so forth until it hits the final e, and calls a copy of itself with the next property of that item - that is to say, it calls itself with NULL as the input.
This copy of the function then returns due to the if statement- crucially, it returns to the function that called it, in this case the copy of itself the level above. This copy then carries on where it left off, executing printf("%d ", head->data); - in this case, it prints an e. Then it falls out of the body of the function and so returns to the function that called it. This copy of the function will have a head->data value of l, and this will print as above. As such, it then runs through back up the chain until it gets to the original function call, prints the first e, and then returns to the function that called it.
Hope this makes sense, I know it's not the best or most elegant explanation of recursion but there's a lot of material out there if this is confusing explaining exactly how recursion works and how useful it can be!
The thing that makes it go in reverse is to have the printf() call after the recursive call to itself.
You can try interchanging the two calls as in
void printNormal(struct node* head)
{
// Base case
if (head == NULL)
return;
// Before all, we print this node info.
printf("%d ", head->data);
// print the rest of the list after this node
printReverse(head->next);
}
And you'll get the forward printing of the list.
The mission of the so commented Base case is to avoid infinite recursion. The end of the list is special, we can do the check on entering the printReverse() function, so we can deal with the special case of an empty list, or we have to do it at each call (then we should put a check before calling the function inside it, and in the code that calls the recursive function (better to put it at the beginning).
As others have already explained the recursive version. I suggest you to have a look at this iterative version of printing linked list in reverse order. This is more easy to understand and implement as compared to recursive version, however it increases time complexity.
// To ease testing of code here linked list is created randomly of given size.
#include<stdio.h>
#include<cstdlib>
#include<time.h>
#define size 10 //Size of list
struct node
{
int info;
struct node * next;
};
typedef struct node* Node;
Node start;
int main()
{
int i;
srand(time(NULL));
for(i=0;i<size;i++)
{
Node ptr;
ptr=(Node)malloc(sizeof(node));
ptr->info=rand()%100; //random list is created of given size.
ptr->next=NULL;
if(start==NULL)
start=ptr;
else
{
ptr->next=start;
start=ptr;
}
}
printf(" Traversal of Linked List in forward direction -\n");
Node temp=start;
while(temp!=NULL)
{
printf(" %d ",temp->info);
temp=temp->next;
}
printf("\n Traversal of Linked List in backward direction -\n");
Node ptr=NULL;
while(ptr!=start)
{
temp=start;
while(temp->next!=ptr)
temp=temp->next;
printf(" %d ",temp->info);
ptr=temp;
}
return 0;
}
For more details visit-
https://github.com/SahdevKansal02/Data-Structures-And-Algorithms.git
Jim Rodgers hits the nail on the head. However, you could make life very easy for yourself by having a doubly linked list.
So for example your entry (node) has both a previous and next member pointing to the previous entry and next entry respectively. Then you don't need more "obfuscated" code than necessary.
I have linklist c-program which is working correctly, but a late requirement was that no global variables must be use. So I had to redesigned everything to avoid passing out parameters, but there are parts which is hard to redesign and some of the functions are calling functions and another functions, one example is on the display.
int main() {
display(&head, &tail);
}
void display(myrec **head, myrec **tail) {
sort(&*head, &*tail)
then sort again call function swap
}
My question is how to correctly pass out parameters multiple times. Program is not working correct using sort(&*head, &*tail), there is no syntax error but there are now missing entries on the data. I tried to code everything on the display function just to check and it does, so I am guessing that I am doing something wrong in passing the variables. thanks.
Its not recursion, the head and tail are variables that holds the state of the linklist, so in swap it is change, I also need to reflect the changes to the root caller and other part of the program need the new state of the head and tail.
Re-passing the out parameters to another function is adding "&" again and again.
exiting display will make head and tail variable reflect the changes made on the other functions. Not sure if its the best way but it is working.
Ex.
display(&head, &tail);
void display(myrec **head, myrec **tail) {
sort(&*head, &*tail)
}
void sort(myrec **head, myrec **tail) {
swap(&*head, &*tail)
}
In C, I want to pass a pointer to another function so I coded the following,
node* list // contains linked list of nodes
void function (node* list){
/*Whatever I do here, I want to make sure that I modify the original list,
not the local copy. +
how do I pass the original pointer 'list' to this function so that I'm working
on the same variable? */
}
[EDIT]
I just like to clarify few things here,
I actually have a void function which takes a struct argument,
void function (struct value arg){ }
and in the struct, I'm defining one of my internal variables as,
node* list-> list;
so in my function, if I do something like,
arg->list
am I accessing the original list variable?
To answer your edit:
No, you are passing the structure by value, so the function contains a copy of the structure. If you don't return the structure in the function and grab it from where you called the function from, it will be lost.
Keep in mind that C does not pass by reference. It is instead simulated by passing the pointer. You could instead do this:
void function (struct value* arg)
{
....
}
And it will modify the original structure.
The question has already been discussed here
Say we don't use the two pointers first and last and simply write
if(*head_ref->next==NULL)
{
p=*head->ref;
return;
}
recursive(&head_ref->next);
p->next-next=p;
p->next=NULL;
This doesn't work, I know it shouldn't, but can't understand why.
First, is there any problem if we pass *head_ref? It does actually change the head, but as we are in recursion, should it cause any problem? Also what should we pass as argument in recursive calling?
The correct code is below I just tried to implement that in a different way and I know it's wrong
struct Node *head;// Declared Global
void Reverse(struct Node *p)
{
if(p->next==NULL)
{
head=p;
return;
}
Reverse(p->next);
p->next->next=p;
p->next=NULL:
}
Hello I recently asked some questions on linked lists in C.
The link was found here
First I want to thank everyone for helping me with this. But I have one issue I cannot understand. I even asked the professor but he emailed me back with little information. Basically I am writing a linked list in C (see above link). One of the things the professor gives us in the header file is this:
void list_map( INTLIST *list, void (*f)(void *) );
/*Applies a function to each element of the list */
So I emailed him about this and said:
Another question, in the header file you did not define a sorting function, do we need to write a sorting function with the prototype and finally what is list_map
And he replied with:
You are asked to implement a sorting function f, which is called through list_map(list, f). Hope it clears your doubts.
My only doubts are this was not fully taught. I can understand how to sort the linked list in fact here is some pseudo code:
tmp=head;
while(tmp!=NULL)
{
tmp2=tmp->next; //pointer to next node
while(tmp2!=NULL)
{
if (tmp2->data < tmp->data)
{
int x = tmp2->data;
tmp2->data = tmp->data;
tmp2->data = x;
}
tmp2=tmp2->next;
}
tmp=tmp->next;
}
I know the experts might say this is not the most efficient, and I understand that right now I am just learning and trying to get things working. I can clean up afterwords...so on to my question.
My question is given I have the sort function (in the professor's case he calls it f). How would I call this sorting function when the signature is:
void list_map(INTLIST* list, void (*f) (void*));
Would I just say:
list_map(myList, f()); //apply function f to the current linked list
Or do I really need to define list_map somewhere? I am not the typical student just looking for someone to do my work. I am really trying to understand this as best I can.
Thanks to all of you.
[EDIT PORTION]
I wanted to add that one of the posters Kaleb P. said
"Thus, your job is to create a sorting
function that you will pass in to
list_map. Note that the correct syntax
for passing it in will be:"
So should my code simply be this:
in the .h file I prototype the function as:
void myCustomSort(void*);
And then in the .cpp it becomes:
void myCustomSort(void*f)
{
tmp=f->head;
while(tmp!=NULL)
{
tmp2=tmp->next; //pointer to next node
while(tmp2!=NULL)
{
if (tmp2->data < tmp->data)
{
int x = tmp2->data;
tmp2->data = tmp->data;
tmp2->data = x;
}
tmp2=tmp2->next;
}
tmp=tmp->next;
}
}
And to call it in main I would just do:
list_map(myListPointer, &myCustomSort);
But don't I need to define list_map anywhere? Because it is in the .h file do I not have to define it?
Assuming list_map is implemented like this, giving f each node in sequential order,
void list_map(INTLIST *list, void (*f)(void *)) {
INTLIST *node;
for (node = list; node; node = node->next)
f(node);
}
you can implement a selection sort
void list_sort(INTLIST *list) {
list_map(list, swap_head_with_smallest);
}
where void swap_head_with_smallest(void *) swaps the datum of a given node with the smallest of the data of any nodes following it in the list.
As this is homework, I'm trying not to give the whole solution away.
void swap_head_with_smallest(void *list) {
INTLIST *head = list;
INTLIST *smallest;
/* set smallest the smallest node of
head, head->tail, head->tail->tail, etc. */
/* swap head->datum and smallest->datum */
}
Your professor is trying to teach you a concept that's common in functional programming, which is the idea of a higher-order function. A higher-order function can take other functions as parameters, sort of like
list_of_cosines = map(cos, list_of_inputs)
where list of inputs is a series of floating point values, and cos is the normal cosine function. The map function will call cos for each value in list_of_inputs and return a list of the corresponding results.
C functions cannot take other function types as parameters, but they can take pointers to functions as parameters (usually termed a callback); the canonical example is the qsort() library function, which takes as one of its parameters a pointer to a function that accepts two pointers to void and returns -1, 0, or 1 depending on whether v1 < v2, v1 == v2, or v1 > v2, respectively. For example:
int compareIntValues(const void *v1, const void *v2)
{
int lv1 = *(int *) v1; // convert inputs from pointers to void
int lv2 = *(int *) v2; // to the type we're actually interested in
if (lv1 < lv2) return -1;
if (lv1 > lv2) return 1;
return 0;
}
int main(void)
{
int values[] = {3, 1, 4, 5, 7, 9, 6, 2};
...
qsort(values, // buffer containing items to sort
sizeof values / sizeof values[0], // number of items to sort
sizeof values[0], // size of each item
compareIntValues); // comparison function
...
}
qsort() will then call compareIntValues to order the elements in values. Similar to array expressions, a function designator will have its type implicitly converted from "function returning T" to "pointer to function returning T" depending on the context.
At this point I'm guessing, but it looks to me like your professor wants you to write the list_map function so that it will call a sorting function f with the list as the parameter, something like the following:
void list_map(INTLIST *list, void (*f)(void *))
{
// sort the list by passing it to f
f(list); // or (*f)(list);
}
void sortListAscending(void *ptr)
{
INTLIST *ilptr = ptr;
/**
* sort the list in ascending order
*/
}
void sortListDescending(void *ptr)
{
INTLIST *ilptr = ptr;
/**
* sort the list in descending order
*/
}
int main(void)
{
INTLIST *theList;
...
list_map(theList, sortListAscending); // sort the list in ascending order
...
list_map(theList, sortListDescending); // sort the list in descending order
...
}
The interface provided by your professor is a bit confused; either both the list pointer and the parameter to f() should be void * (in which case you could use list_map to map functions to different list types) or both the list pointer and parameter to f should be INTLIST * (since you seem to be dealing with INTLIST types).
If I'm right, then the exericise is a bit pointless on the surface (why not call the sorting function directly?), but it could be your professor is building up to something a bit more general-purpose. After all, there's no reason that f has to be a sorting function; it could be a function to display the list, or to save the list to a file, or something else.
I have a different example of how to use callbacks to sort a list here; it may help illustrate why this method is useful.
EDIT
ephemient's example of what list_map needs to do is probably much closer to what your professor intends than what I wrote.
The second parameter to list_map is a pointer to function returning void and taking in a void pointer. Since list_map appears to be a map function, I would guess that it will call f (the pointer to a function) for each element of the list.
Thus, your job is to create a sorting function that you will pass in to list_map. Note that the correct syntax for passing it in will be:
void yourCustomSort(void*);
list_map(myList, yourCustomSort);
I would guess that the void* being passed into your sorting function will probably be to a node within the linked list.
MergeSort is a good choice for sorting linked lists.
I believe that the list_map function calls the function pointer f() which is a pointer to a function that takes a void pointer and return void. If that is the case this is crazy way to implement a sort but doable.
Define a function like
void Test(void *)
{...}
And pass it in to the list_map() like so
list_map(listptr,Test);
I would assume your Test function gets called for each item in the list.
If in your node there is a pointer to the head of the list you have to use the pointer to the list as a frontier. Let me explain.
The map function is a common concept in functional programming, for now, you have just to know that is a function that get a list and apply another function (the applied function) to every node of the list was given to it. I bet you already knew it.
C language haven't, as far I remember, a map function, so you have to define one by your own it. It is not very difficult: Just start from the head of the list and walk to the tail. For every step pass the current listnode to the function that perform the operation you need to be performed (in this case the sort).
Now there is your assignment.
You cant get any data from the applied function (it returns void)
You have to walk down until the end of the list passing every single node to the function that do the sorting.
It is useless to sort the list you haven't already visited, as you would keep to sort it for every node (sorting an already sorted set it is not too smart to me) ;)
Your applied function get a single pointer. This in clearly stating that that pointer (the node you are at) represent a line: on his left (to the head) there is the part of list already sorted, to the right (to the tail) there are the wild elements.
Since Your applied function get as input just a simple void * , I think it is better to leave the pointers alone and exchange the payload of the nodes
Said that, the pseudocode for your sorting function, one of the possibile ones, can be:
void ascendingSort(void*f)
{
cursor=f->head;
placed=false;
while(!placed and cursor!=f) {
if (cursor->data < f->data) {
cursor = cursor->next;
} else {
swap( cursor->data, f->data);
placed=true;
}
}
while(cursor!=f) {
cursor = cursor->next;
swap(cursor->data, f->data);
}
}
Or, in a more concise form:
void ascendingSort(void*f)
{
cursor=f->head;
while(cursor!=f) {
if (cursor->data > f->data) {
swap( cursor->data, f->data);
}
cursor = cursor->next;
}
}