#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}node;
void init(node* head, node* tail){
head = (node*)malloc(sizeof(node));
tail = (node*)malloc(sizeof(node));
head->next = tail;
tail->next = tail;
}
void push(node* head, int data){
node *temp;
temp = (node*)malloc(sizeof(node));
if(temp == NULL){
printf("%s", "Out Of Memory");
}else{
temp->data = data;
temp->next = head->next;
on the next Line, there there's printf method which print number 7. It's for debug. but this printf methods didn't worked. so I noticed
temp->next = head->next;
this code has an error. but I couldn't find the reason.. I was thinking of memory allocation issue. but still didn't get it..!
printf("%d", 7);
head->next = temp;
printf("push (%d)", data);
}
}
void main(){
node* head = NULL;
node* tail = NULL;
init(head, tail);
push(head, 10);
}
initfunction, but understand when passing a pointer to a function, the function receives a copy of the pointer (with its very own and very different pointer address). So if you need to pass-and-allocate a pointer without returning-and-assigning the new memory address, you need to pass the address of the pointer as a parameter (e.g.node**, and calling withinit(&head, &tail);) so that you operate on the same pointer in yourinitfunction. Otherwise you allocate and assign the new address to a copy and the change is never seen inmain().printf("%s", "Out Of Memory")->printf("Out Of Memory")no need for"%s"