Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include<stdio.h>
#include<stdlib.h>
//Linked list implementation
typedef struct SLL{
int info;
struct SLL *link;
}Node;
Node *head=NULL;
// Node *rear=NULL;
void insert_rear(int x)
{
Node *temp=malloc(sizeof(Node));
Node *temp1=NULL;
if(temp==NULL) /* When malloc is unable to fetch Memory */
{
printf("\n Insufficient memory");
}
if(head==NULL) /* When there is no node created */
{
temp->info=x;
temp->link=head;
head=temp;
}
else
temp1=head;
while(temp1->link!=NULL)
{
temp1=temp1->link;
}
printf("\n Temp1=%d",temp1);
temp->info=x;
temp->link=NULL;
temp1->link=temp;
}
void insert_front(int x)
{
Node *temp=malloc(sizeof(Node));
if(temp==NULL) /* When malloc is unable to fetch Memory */
{
printf("\n Insufficient memory");
}
temp->info=x;
temp->link=head;
head=temp;
}
void display()
{
int i=0;
Node *temp=head;
printf("\n List Elements: \n ");
while(temp!=NULL)
{
printf(" %d) %d",++i,temp->info);
temp=temp->link;
printf("\t Link= %u \n",temp);
} printf("\n");
}
void main()
{
int x,choice,i;
printf("\n To insert at front enter 1 \n To insert at rear enter 2 \n To exit enter 4 \n");
while(choice!=4)
{
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter an ELEMENT to be inserted at FRONT \n");
scanf("%d",&x);
insert_front(x);
display();
break;
case 2: printf("Enter an ELEMENT to be inserted at LAST \n");
scanf("%d",&x);
insert_rear(x);
display();
break;
}//End of switch
}//End of while
}//End of main
I was coding this linked list program and I came up with a problem in insert_rear() function.
When I add few elements using insert_front() and then add elements at rear along with the existing Nodes using insert_rear() the programs works perfectly fine.
But when I try to add a Node without any existing Nodes using insert_rear() my program does not work for some reason.
So I took some time messing with my program and removed the following portion of code to see if I'm able to add a new node without having any existing node:
else
temp1=head;
while(temp1->link!=NULL)
{
temp1=temp1->link;
}
printf("\n Temp1=%d",temp1);
temp->info=x;
temp->link=NULL;
temp1->link=temp;
}
and it does work work, that is with only the following code I'm able to add to a new node before having any existing nodes
if(head==NULL) /* When there are no existing nodes created */
{
temp->info=x;
temp->link=head;
head=temp;
}
but along with the else condition my code does not work and program crashes.
Please help me correct this error. I have a feeling I did something stupid which I'm not unable to find.
When the list is empty and you add the first element you forget to quit and instead continue down in your function. Try something like this instead
void insert_rear(int x)
{
Node *temp=malloc(sizeof(Node));
Node *temp1=NULL;
temp->info=x;
temp->link=NULL;
if(temp==NULL) /* When malloc is unable to fetch Memory */
{
printf("\n Insufficient memory");
abort();
}
if(head==NULL) /* When there is no node created */
{
head=temp;
}
else
{
temp1=head;
while(temp1->link!=NULL)
{
temp1=temp1->link;
}
printf("\n Temp1=%d",temp1);
temp1->link=temp;
}
}
You should be careful with the use of global variables, best ist to avoid them altogether.
In the function insert_front() you change the head of the list...
Johannes
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I need to create a c program for BST but I can't create left child or right child for the root node. Creation of root node is ok. I can also search the element in the root node. But Can't add more than one element in the tree
Please help me solve this problem, kindly explain about the mistake that I made
#include<stdio.h>
#include<stdlib.h>
typedef struct tree
{
struct tree *left;
int data;
struct tree *right;
}node;
node *ROOT;
//////////////////////////
void *bud()
{
return (node *)malloc(sizeof(node));
}
/////////////////////
void createNode(int data, node *ptr)
{
if(ptr==NULL)
{
ptr=bud();
ptr->data=data;
ptr->left=NULL;
ptr->right=NULL;
ROOT=ptr;
ptr=NULL;
printf("DONE");
return;
}
else if(data==ptr->data)
{
printf("Duplication not possible");
return;
}
else if (data<ptr->data)
{
//ptr=ptr->left;
createNode(data,ptr->left);
}
else if (data>ptr->data)
{
//ptr=ptr->right;
createNode(data,ptr->right);
}
else
{
printf("INVALID");
}
}
//////////////////////////////////////
void search(int data,node *ptr)
{
if(ptr==NULL)
{
printf("NOT FOUND");
return;
}
else if (ptr->data==data)
{
printf("Item Found");
return;
}
else if(data<ptr->data)
{
//ptr=ptr->left;
search(data,ptr->left);
}
else if (data>ptr->data)
{
//ptr=ptr->right;
search(data,ptr->right);
}
else
{
printf("INVALID");
}
}
/////////////////////////////////
void main()
{
int ch;
ch=0;
while (ch!=6)
{
printf("\nMENU\n1. Create Tree\n2. Search\n3. Inorder\n4.
Preorder\n5. Postorder\n6. Exit\n EnterYour Choice: ");
scanf("%d",&ch);
if(ch==1)
{
int dat;
printf("\nEnter the number to be inserted: ");
scanf("%d",&dat);
createNode(dat,ROOT);
}
else if(ch==2)
{
int dat;
printf("Enter number to be searched: ");
scanf("%d",&dat);
search(dat,ROOT);
}
else if(ch==6)
{
return;
}
else
{
printf("\nEnter a valid choice\n");
}
}
}
The problem is logical error. I can't create the left or right child of the tree. Whatever I insert to the tree gets inserted to the root node.
I can't create the left or right child of the tree. Whatever I insert to the tree gets inserted to the root node.
This is happening because of this statement in your createNode():
ROOT=ptr;
Whenever you are inserting an element in the tree, you are traversing tree recursively and when you find the appropriate location to insert, you are passing it to createNode() which is NULL (either ptr->left or ptr->right) and ROOT is reassigned to ptr.
If I just make changes in your program to make work it properly, the createNode() will look like this:
void createNode(int data, node **ptr)
{
if(*ptr==NULL)
{
*ptr=bud();
(*ptr)->data=data;
(*ptr)->left=NULL;
(*ptr)->right=NULL;
printf("DONE");
return;
}
else if(data==(*ptr)->data)
{
printf("Duplication not possible");
return;
}
else if (data<(*ptr)->data)
{
createNode(data,&((*ptr)->left));
}
else if (data>(*ptr)->data)
{
createNode(data,&((*ptr)->right));
}
else
{
printf("INVALID");
}
}
And in main(), you need to do:
createNode(dat,&ROOT);
However, there is a scope of improvement in your program, for e.g. separate the node creation and insertion operation in your program. I am leaving it up to you to find the improvements and better ways of theirs implementation.
The error is in your createNode() function: you pass node *ptr and assign like this ptr=bud();. But this only modifies local variable, not pointer in parent node. Try passing node **ptr and do dereferencing inside of the function *ptr=bud();. Use & operator to derive pointer to pointer. As #H.S. mentioned ROOT=ptr; statement makes your program lose node. Besides you allocate memory and don't free it (you can't even do that, because you lose pointer to ROOT node).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Q please help me this program is not working properly.
not displaying the value.this program is an example of singly linked list which I am trying to run on c.
`
#include<stdio.h>
#include<stdlib.h> //malloc defined
struct node
{
int data;
struct node *next;
};
add() //add function
{
int value;
struct node *n;
n=(struct node*)malloc(sizeof(struct node)); //mem allocation
printf("enter the value to add\n");
scanf("%d",&value);
n->data=value;
n->next=NULL;
// n=n->next;
// n->next=NULL;
}
delete() //delete function
{
// n=n->next;
struct node *n; //declaration
printf("the node deleted is %d",n->data);
free(n);
}
display() //display function
{
struct node *n;
while(n!=NULL)
{
printf("%d",n->data);
n=n->next;
}
}
int main()
{
int ch;
while(1)
{
printf("do you want to add node press 1\n");
printf("do you want to delete node press 2\n");
printf("do you want to display node press 3\n");
printf("do you want to exit press 4\n");
scanf("%d",&ch);
switch(ch)
{
case 1:add();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
default: printf("wrong choice!!!\n");
}
}
return 0;
getch();
}
please help me this program is not working properly.
not displaying the value.this program is an example of singly linked list which I am trying to run on c.
printf("%d",n->data); is not printed because:
struct node *n; // n is not determined.
while(n != NULL) // undefined behaviour
n is different from the other n that is in the add() function. You never passed the struct to the other functions so it will not do what you wanted it to do.
in the function display(), the local variable 'n' is only declared but not defined, so here 'n' is only a wild pointer. It's happened in the function delete() as well.
it's not a correct way to implement the single linked list. In the function add() what you wrote is only create a node, but not add a node to linked list.
#include<stdio.h>
#include<stdlib.h> //malloc defined
struct node
{
int data;
struct node *next;
}*n,*p;
create() //add function
{
int value;
n=(struct node*)malloc(sizeof(struct node)); //mem allocation
printf("enter the value to add\n");
scanf("%d",&value);
n->data=value;
n->next=NULL;
}
add()
{
int value;
// struct node *p;
p=(struct node*)malloc(sizeof(struct node));
printf("enter the value to add next\n");
scanf("%d",&value);
n->next=p;
p->data=value;
p->next=NULL;
}
delete() //delete function
{
printf("the node deleted is %d",p->data);
n->next=NULL;
free(p);
}
display() //display function
{
while(n!=NULL)
{
printf("%d\n",n->data);
n=n->next;
}
}
int main()
{
int ch;
while(1)
{
printf("do you want to create node press 1\n");
printf("do you want to add node press 2\n");
printf("do you want to delete node press 3\n");
printf("do you want to display node press 4\n");
printf("do you want to exit press 5\n");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:add();
break;
case 3:delete();
break;
case 4:display();
break;
case 5:exit(0);
default: printf("wrong choice!!!\n");
}
}
return 0;
getch();
}
I have created a simple linked list using c where we can insert and delete elements at any place in the list. The code worked properly until i tried to delete the first node using the following code.
typedef struct l_list
{
int data,index;
struct l_list *next_node;
}node;
static int total_node=0;
node *search(node *,int,node **);
int main()
{
int choice,key;
char ans;
node *new_node,*head, *p_node,*index_change,*cur;
node *get_node();
head=NULL;
printf("Program for Linked List.\n");
do
{
printf("\n1. Create Node");
printf("\n2. Delete Node");
printf("\n3. Traverse the List");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
do
{
total_node++;
new_node=get_node();
printf("\nEnter the data you want to insert: ");
scanf("%d",&new_node->data);
if(head==NULL)
{
head=new_node;
head->index=1;
}
else
{
printf("\nWhich node you want to insert it as:\n");
for(int i=1;i<=total_node;i++)
{
printf("%d ",i);
}
printf("==) ");
scanf("%d",&key);
//printf("\b\b-|-");
if(key==1)
{
new_node->next_node=head;
head=new_node;
}
else
{
p_node=search(head,key,&cur);
new_node->next_node=p_node->next_node;
p_node->next_node=new_node;
//p_node=NULL;
}
new_node->index=key;
index_change=new_node->next_node;
while(index_change!=NULL)
{
index_change->index=++key;
index_change=index_change->next_node;
}
}
printf("\nDo you want to insert more node in the linked list: [y/n]");
//ans=getch();
}while((ans=getch())=='y');
break;
//Deletion code.
case 2:
do
{
if(head==NULL)//head is first node of the list
{
printf("\nUNDERFLOW!\nThe linked list is already empty.\n");
}
else
{
printf("Which node you want to delete:\n");
for(inti=1;i<=total_node;i++)
printf("%d ",i); //total_node=variable taken
printf("==) "); //to track the total no of node
scanf("%d",&key); //key=node index to be deleted
//printf("\b\b-|-");
if(key==1)
{
//If we need to delete the first node when only one node is left
if(total_node==1)
{
//p_node=head;
head=NULL;
}
//If we need to delete the first node when more than one node are there
else
{
//p_node=head;
head=head->next_node;
}
total_node--;
}
else
{
p_node=search(head,key,&cur);//returns node just before the node to be deleted
p_node->next_node=cur->next_node;//cur gets the value of the node that is to be deleted.
total_node--;
}
index_change=p_node->next_node;
while(index_change!=NULL)//to change the index of following nodes.
{
index_change->index=key++;
index_change=index_change->next_node;
}
}
printf("\nDo you want to delete more nodes: [y/n]\n");
}while((ans=getch())=='y');
case 3:
if(head==NULL)
printf("\nThe linked list is empty.\n");
else
{
printf("\nThe elements of linked lists are as follows:\n\n");
p_node=head;
while(p_node!=NULL)
{
printf("[%d]->%d ",p_node->index,p_node->data);
p_node=p_node->next_node;
}
}
break;
}
}while(choice!=4);
return 0;
}
node *get_node()
{
node *temp1;
temp1= new node;
temp1->next_node=NULL;
return temp1;
}
node *search(node *head,int key,node **cur)
{
node *current,*prev;
current=head;
while(current!=NULL)
{
if(current->index==key)
{
return prev;
}
prev=current;
current=current->next_node;
*cur=current;
}
return prev;
}
using this code if i try to delete the first node the program gets crashed. And when i use a temporary variable such as
if(key==1)
{
if(total_node==1)
{
p_node=head;
head=NULL;
}
else
{
p_node=head;
head=p_node->next_node;
}
total_node--;
}
the program works properly. So what i want to ask is can we delete the head node directly or we always require another temporary structure pointer to delete the head node.
In this line :
index_change=p_node->next_node;
you dereference p_node. But in the cases where you delete the first node, you don't set a value for p_node. The crash you observe is likely because p_node does not hold a valid memory address.
It is difficult to say what is happening without the exact error you are getting, and the complete program.
One thing that immediately looks wrong is that you assign nodes (your struct Linked_List), which includes the data. This is not what you want with a linked list. In a linked list, you just want to modify pointers, and prevent copying the data.
Also, you program is very badly structured. Consider moving the specific operations into separate functions, and write tests for each of these. This will also allow you to post a concise, complete code sample with your question.
In order to close in on the error, you can either use a debugger, or you can add print statements to your code that tell you what is happening.
In this line:
p_node=search(head,key,&cur);//returns node just before the node to be deleted
you pass head pointer which is already set to NULL if you try to delete 1st node.
So you can not dereference the head pointer inside the search function which your code must be doing (as I believe) since you do not have any other way to get to the start of linked list.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
struct node{
int data;
struct node *next;
};
struct node *head,*temp;
void insert()
{
struct node *var;
head=NULL;
var=(struct node*)malloc(sizeof(struct node));
printf("enter the data:");
scanf("%d",var->data);
temp=head;
if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
if(temp->next==NULL)
{
temp->next=var;
temp=temp->next;
temp->next=NULL;
}
}
}
void display()
{
temp=head;
if(temp==NULL)
{
printf("empty list");
}
while(temp->next!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
void main()
{
int value,choice;
printf("\nenter choice:");
scanf("%d",&choice);
while(choice==1)
{
insert();
display();
printf("\nenter choice:");
scanf("%d",&choice);
}
getch();
}
i have made a linklist using c above but the code is not displaying the linklist,instead it shows null pointer compilattion as output,how to solve the problem,i'm new to c coding so cant find adequate solution for this ????
scanf("%d",var->data);
//--> scanf("%d",&(var->data));
scanf's argument must be pointer type.
With every insert, you reset head to NULL. So you will always insert new values at the head and any existing values will be just left in memory, leading to a memory leak.
I guess you want to move the line head=NULL; to the beginning of the main method.
And fix your scanf like keeptalk said.
You seem to be missing to initialise *var's member next after having allocated *var using malloc().
#include<stdio.h>
#include<stdlib.h>
//double linked list
struct node {
int data;
struct node *rnext;
struct node *lnext;
}*first=NULL,*last=NULL;
//double linked list
void insertion() {
struct node *nn=malloc(sizeof(*nn));
printf("enter data to be inserted\n");
scanf("%d",&nn->data);
nn->rnext=NULL;
nn->lnext=last;
if(first == NULL) {
first = nn;
last = nn;
}
else{
last->rnext=nn;
}
last=nn;
}
void display() {
struct node *temp;
if(first==NULL) {
printf("list is empty\n");
return;
}
temp=first;
while(temp!=NULL) {
printf("%d \n",temp->data);
temp=temp->rnext;
}
}
void deletion() {
struct node *temp;
if(first==NULL) {
printf("list is empty\n");
return;
}
temp=first;
first=first->rnext;
first->lnext=NULL;
free(temp);
}
int main() {
int option;
do {
printf("enter option 1.insert\n 2.display\n 3.delete\n 4.exit\n");
scanf("%d",&option);
switch(option) {
case 1:
insertion();
break;
case 2:
display();
break;
case 3:
deletion();
break;
}
} while(option!=4);
}
This is a program written for deleting and inserting a node in double linked list. The program compiles without error, but it fails at run-time with a segmentation fault error while deleting a node when there is only one node in the list. Can anyone please help with the solution for this segmentation fault?
Here is some sample output from the program:
./out
enter option 1.insertion
2.display
3.deletion
4.exit
1
enter data to be inserted
11
enter option 1.insertion
2.display
3.deletion
4.exit
2
11
enter option 1.insertion
2.display
3.deletion
4.exit
3
Segmentation fault
The absolute easiest way to solve this one is run it in a debugger. You probably won't even need to learn how to step through your code or anything - just fire up, run, and read the line.
If you are on a *nix as your tag indicated:
Compile your code with -g flag.
Load as, e.g. gdb a.out.
Run now that it's loaded - (gdb) run.
Do whatever you need to reproduce the segfault.
bt or where should give you a stack trace - and an exact line that is causing your problem.
I'm sure enough you can solve it from there to post this as an answer; but if not, knowing the exact line will make it very much easier to research and solve.
At least two bugs:
On one hand, in the insertion function, the memory allocation is incorrect:
struct node *nn=malloc(sizeof(*nn));
It should be :
struct node *nn= (struct node *) malloc(sizeof(struct node));
On the other hand, in the deletion function. If there is only one node. After the statement
first=first->rnext;
the pointer first become NULL. Then you try to use it as:
first->lnext=NULL; // first is NULL
Then segment fails.
temp=first;
first=first->rnext;//when only one, first is NULL(first->rnext)
first->lnext=NULL;//(NULL)->lnext , Segmentation fault!!
free(temp);
maybe fix to
temp=first;
if(first == last){//if only one
first = last = NULL;
} else {
first=first->rnext;
first->lnext=NULL;
}
free(temp);