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)
}
Related
I'm writing a program to create a singly linked list. I'm using two pointer variables one of them is named as "start" which points to the first node of the list and the other pointer variable that I'm using is named as "t". I'm using this pointer as a helper variable which helps in traversing the list without disturbing the start.
The program gets successfully compiled, but the problem I'm facing is that during run time it only allows me to add one node to the list. After that, if I try to add another node, the execution stops after entering the data of the node.
I tried a couple of things, but only one of them worked. If I declare the "helper pointer variable" as global the program starts running fine.
Why is this even happening?
I'm only using the helper pointer variable "t" in a function to traverse the list and it's not even communicating with another function in the program.
Can someone please explain why it only works with global declaration?
This is the code of the function ->
void insert()
{
struct node *newnode;
struct node *t; //<------this is the helper variable if I declare this locally
//then the problem occurs in the run time.
newnode = create();
printf("Enter data ");
scanf("%d",&newnode->info);
//printf("Node info = %d",newnode->info);
if(start==NULL)
{
start=newnode; <------ this is that start variable which is declared above globally
start->next=NULL;
t=newnode;
}
else
{
t->next=newnode;
t=newnode;
t->next=NULL;
}
printf("%d successfully added to the list.",newnode->info);
}
The variable t at the start of the function is a dangling pointer i.e. it can not be de-referenced.
Try putting this in else block before t->next=newnode;
t = start;
while(t->next)
t = t->next;
When you declare it as global (or static works too), you make it remember the last value which was stored in it, thus every time it does not initiate with a dangling pointer.
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.
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.
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:
}
Disclaimer: I'm allowed to talk about the interview question since I already was rejected by the company and never had to sign an NDA before taking the interview test anyhow.
Also, this isn't a "Write my code for me" post. I'm just curious about understanding the setup of this problem. It wanted me to fill in the body of a function that removes an element from a linked list:
typedef struct
{
int val;
node * next;
} node;
void remove_val(node ** arr, int i)
{
/* Write the procedure here */
}
I was confused about why the problem had a pointer-to-a-pointer as a parameter. I would expect that the parameter to a function would be the root of the list, which would be a pointer to a node. Right? Any idea what the first parameter was supposed to be???
What happens if you remove the head of the list? You need some way to communicate that back to the calling code. This function signature allows you to change the head of the list to point to its next, if necessary.