Reputation: 2243
I have my link list defined in the following way:
typedef struct node {
int data;
struct node* link;
} Node;
typedef struct list {
int a;
Node* root;
} List;
While deleting node with value 'x' from the above list I use following recursive function:
void pop(List *l, int x) {
Node* temp = l->root;
printf("Entered\n");
if (temp == NULL) {
return;
}
else if ((l->root)->data == x) {
pophead(l);
pop(l, x);
}
else {
Node* previous;
while (temp->data != x) {
if (temp->link == NULL) {
break;
}
previous = temp;
temp = temp->link;
}
if (temp->link == NULL) { //last element has been reached
if (temp->data == x) {
previous->link = NULL;
free(temp);
}
else {
return; }
}
else {
previous->link = temp->link;
free(temp);
temp = previous->link;
List* templist = create();
templist->root = temp;
pop(&temp, x); //**can replace with pop(templist,x)**
}
}
}
The last line in the pop function(in bolds) works well with 2 variations:
Variation 1. pop(templist,x)
: This pases List*
pointer as arguments.
Variation 2. pop(&temp,x)
: This passes address of node in link list.
Variation 2 should not ideally work for 2 reason:
Here's the complete code are as follows:
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node* link;
}Node;
typedef struct list{
int a;
Node* root;
}List;
List* create(){
List* temp=(List*)malloc(sizeof(List));
temp->root=NULL;
return temp;
}
void push(List* l,int x){
Node* temp=l->root;
Node* n=(Node*)malloc(sizeof(Node));
n->data=x;
n->link=NULL;
if(temp==NULL)
{
l->root=n;
}
else{
while(temp->link!=NULL){
temp=temp->link;
}
temp->link=n;
}
}
void pophead(List * l){
Node* temp=l->root;
if(temp==NULL){
return;
}
else{
l->root=l->root->link;
free(temp);
}
}
void pop(List *l,int x){
Node* temp=l->root;
printf("Entered\n");
if(temp==NULL){
return;
}
else if((l->root)->data==x){
pophead(l);
pop(l,x);
}
else{
Node* previous;
while(temp->data!=x){
if(temp->link==NULL){
break;
}
previous =temp;
temp=temp->link;
}
if(temp->link==NULL) { //last element has been reached
if(temp->data==x){
previous->link=NULL;
free(temp);
}
else{
return;
}
}
else{
previous->link=temp->link;
free(temp);
temp=previous->link;
List* templist=create();
templist->root=temp;
pop(templist,x);
}
}
}
void display(List* l){
Node* temp=l->root;
while(temp!=NULL){
printf("%d\n",temp->data);
temp=temp->link;
}
}
void main(){
List* l1=create();
push(l1,10);
push(l1,20);
push(l1,30);
push(l1,40);
pophead(l1);
pop(l1,30);
display(l1);
}
Upvotes: 0
Views: 51
Reputation: 87386
Your code actually has a bunch of compilation errors and warnings. Make sure that you know how to see all the errors and warnings your compiler is reporting. Make sure you fix all of those before assuming that your code is valid C code.
The first error I see has to do with your mistaken use of struct Node
: that struct is not defined anywhere, but struct node
(lowercase) is.
And when I use your code on godbolt.org with GCC, I do get a warning for the line you were concerned about:
<source>:55:33: warning: passing argument 1 of 'pop' from incompatible pointer type [-Wincompatible-pointer-types]
55 | pop(&temp,x); //**can replace with pop(templist,x)**
| ^~~~~
| |
| Node ** {aka struct node **} ~~~~~~^
Upvotes: 1