In this code below, when I comment out the first and second call to display() function in main, It works fine. But without commenting, after adding 2nd element it says - segmentation fault.
I am new to pointers. Please suggest what might be wrong.
#include<stdio.h>
#include<malloc.h>
//-------------------------------------------------
struct node
{
int data;
struct node *next;
}*start=NULL;
//------------------------------------------------------------
void creat()
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
}
//------------------------------------------------------------------
void display()
{
struct node *new_node;
printf("\nThe Linked List : ");
new_node=start;
while(new_node!=NULL)
{
printf("%d--->",new_node->data);
new_node=new_node->next;
}
printf("NULL\n\n");
}
//----------------------------------------------------
void main()
{
creat();
display();
creat();
display();
creat();
display();
}
output ->
$./a.out
Enter the data : 4
The Linked List : 4--->NULL
Enter the data : 6
Segmentation fault (core dumped)
This is happening because of current->next=new_node; in else block.
When you enter data second time start is declared global so its value persists between function call but as current is declared within fuunction it's scope is limited to function only.
So when you enter data second time current is null so accessing current->next causes segmentation fault.
So declare current node after start variable.
Second time you call creat, you are using an ininitialized current.
current_node is a local variable with auto storage. When you return from the function, it gets deleted.
Try adding
struct node* current = NULL;
right after you define start. Then, remove it from creat.
The offending code is:
current->next=new_node;
You are dereferencing a NUll pointer which is illegal.
What you should have done was to declare current as a global variable as well:
struct node
{
int data;
struct node *next;
}*start=NULL,*current=NULL;
This will fix your display issue as well.
Related
This is a program to print two link lists using one function.
I have used two functions i.e create and display.Create() is to create the linklist and display() to display the result of linklist.
But this code is printing NULL. I'm not not getting where is the error???
`#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node* next;
}
*start=NULL,*start1=NULL;
//to create the linklist
struct node* create(struct node* ptr)
{ int ch;
do
{
struct node* new_node=(struct node*)malloc(sizeof(struct node));
struct node* current;
printf("enter the data\n");
scanf("%d",&new_node->data);
new_node->next=NULL;
if(ptr==NULL)
{
ptr=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
printf("Do u want to add more ");
scanf("%d",&ch);
}while(ch!=0);
return ptr;
}
//to display the linklist
void display(struct node* temp)
{
while(temp!=NULL)
{
printf("%d--->",temp->data);
temp=temp->next;
}
printf("NULL\n");
}
int main()
{
clrscr();
create(start);
display(start);
printf("\n 2nd linklist\n");
create(start1);
display(start1);
getch();
return 0;
}
First problem
current must be declared outside the do/while loop, otherwise you will get undefined behaviour because there is no guarantee that current will keep it'svalue from one iteration to the next. However with certain compilers you may get away with it.
Second problem
Calling create(start); will not modify start because variables in C are passed by value. You need to write start = create(start);. See also this SO question. In your program start is still NULL after the call to the create function. You could have found out that easily by yourself.
I am unable to append a new node in the linked list. I have already identified the problem area but after much research and trying many things I am still unable to resolve the issue.
The problem is with the for loop in insert_node(char,struct **) function and traverse(struct *) function both of which never seem to terminate:
// program that stores name of the user using linkedlist
#include<stdio.h>
#include<stdlib.h>
typedef struct LIST{
int flag;
char name;
struct LIST *next;
} LISTNODE;
LISTNODE *head=NULL,*newnode=NULL;// global pointers
LISTNODE* initialize(); //initializes struct node with default values and returns a node
void insertNode(char c,LISTNODE** temp);
void traverselist(LISTNODE *temp);
int main(){
char ans,ch;
printf("\n\nEnter your name and hit enter-\n\n");
do{
printf("your name:");
fflush(stdin);
scanf("%c",&ch);
insertNode(ch,&head);
printf("\n\ninsertnode-back to main()");
printf("Want to continue?(Y?N):");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y'||ans=='Y');
printf("\n\ntraverselist-leaving main()");
traverselist(head);
printf("\n\ntraverselist-back to main()");
return 0;
}
void insertNode(char c, LISTNODE **temp){
printf("\n\ninto insertnode: before initialize");
LISTNODE* temp2;
newnode=initialize();
printf("\n\nback to insertnode:after initialize");
//printf("\nnewnode->name=%c",newnode->name);
//printf("\nnewnode->flag=%d",newnode->flag);
newnode->name=c;
//printf("\nnewnode->name=%c",newnode->name);
//printf("\nnewnode->flag=%d",newnode->flag);
//for(;(*temp)!=NULL;temp=&(*temp)->next);
/*while((*temp)->next!=NULL){
temp=&(*temp)->next;
printf("\n\nIn while!");
}
*/
for(;*temp!=NULL;temp=&((*temp)->next))
printf("\n\nIn for!") ;
//printf("\n\nout of while!");
(*temp)=newnode;
}
LISTNODE* initialize(){
static int count=0;
LISTNODE *tempnewnode;
printf("\n\nINto inintialize!");
tempnewnode=(LISTNODE*)malloc(sizeof(LISTNODE));
if(tempnewnode==NULL){
printf("No memory available. Aborting!");
exit(0);
}
else{
tempnewnode->flag=0;
tempnewnode->name='*';
tempnewnode->next=NULL;
if(count==0){
head=tempnewnode;
count++;
}
}
return tempnewnode;
}
void traverselist(LISTNODE *temp){
printf("\n");
for(;temp!=NULL;temp=temp->next){
printf("%c",temp->name);
}
}
Please help!
The problem is inside the insert_node function, specifically with the loop:
for(;*temp!=NULL;temp=&((*temp)->next))
printf("\n\nIn for!");
You'd be better advised not to use the reference temp in your loop, as it overwrites the head->next back to itself. Create another temporary pointer.
I changed the insertNode(char, LISTNODE**) to the following:
void insertNode(char c, LISTNODE *temp){
LISTNODE** temp2=&temp;
newnode=initialize();
printf("\n\nback to insertnode:after initialize");
newnode->name=c;
for(;(*temp2)!=NULL;temp2=&(*temp2)->next)
printf("\n\nIn for!") ;
(*temp2)=newnode;
}
and function is called like this:
insertNode(ch,head);
It works just fine!
The problem is this portion of your insertNode function
for(;*temp!=NULL;temp=&((*temp)->next))
printf("\n\nIn for!") ;
//printf("\n\nout of while!");
(*temp)=newnode;
Here you should first check if the link list is empty or not if it is empty then you can create the new node and assign the address of it to temp.
If not then depending upon whether you want to insert the new element in the end, beginning or middle of the list you should traverse the list then perform the insertion.
For example if you want to perform insertion at the beginning then after creating the new node you should assign the address of the start pointer of the list to the next of newly created node and move the start to the new node as you have to keep track of the start pointer.
I am working on a simple text editor in C. I am having troubles with inserting an element in a linked list.
Here is my structure:
struct node {
struct node *previous;
int c;
int x;
int y;
struct node *next;
}*head;
Here is my insertion code:
void checker(int ch, int xpos, int ypos)
{
int flag=0;
struct node *temp,*temp1,*insert_node=NULL;
temp=(struct node *)malloc(sizeof(struct node));
temp=head;
while(temp!=NULL)
{
if(temp->x==xpos && temp->y==ypos)
{
insert_node->c=ch;
insert_node->x=xpos;
insert_node->y=ypos;
if(temp->previous==NULL) //this is for inserting at the first
{
insert_node->next=temp;
head=insert_node;
}
else //this is for inserting in the middle.
{
temp1=temp;
temp=insert_node;
insert_node->next=temp1;
}
flag=1;
break;
}
temp=temp->next;
}
//this one's for the normal insertion and the end of the linked list.
if(flag==0)
characters(ch,xpos,ypos);
}
None of the inserting in the first and middle works. I do not know where it went wrong. Please help me.
temp=(struct node *)malloc(sizeof(struct node));
temp=head;
You are allocating space for a new node, but then you loose the address of this new node assigning temp=head.
The problem is that insert_node is a local variable in your function checker() which is also initialized as NULL. Doing insert_node->c means NULL->c which i'm sure you'll agree with me that is wrong.
Try to dynamically allocate memory for your variables before using them and you should be fine.
insert_node will always be NULL in the code you posted.
Also, you may want to split your code more; start by isolating part of it in a find() function.
#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);
I am trying to append one list with another . If i pass a pointer-to-the-pointer of both the lists and just display them, then , the code works fine. But if i use code to reach the NULL pointer of the first list and then equate it to the first one of the second, then it gives a segmentation fault. Please let me know what the mistake is. Code is below :
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
}*Head,*New;
void display(struct node **p)
{
struct node *curptr;
curptr=*p;
if(curptr==NULL)
printf("list is empty");
else
{
while(curptr)
{
printf("->%d",curptr->data);
curptr=curptr->next;
}
}
}
void combine(struct node **a,struct node **b)
{
//display(&(*a));
struct node *aptr;
aptr=*a;
while(aptr)
aptr=aptr->next;
aptr->next=*b;
*b=NULL;
display(&(*a));
//display(&(*a));
//display(&(*b));
}
void main()
{
Head=NULL;
New=NULL;
int choice;
while(1)
{
case 9:
{
printf("Combining two lists");
combine(&Head,&New);
break;
}
The problem is here:
while(aptr)
aptr=aptr->next;
aptr->next=*b
When you break out of the while loop aptr will be NULL next when you try to do aptr->next you get the SEGV.
To fix this break out of the loop when you reach the last node(aptr->next will be NULL) rather than aptr becoming NULL.
Something on these line:
// if fist list does not exist.
if(*a == NULL) {
*a = *b;
return;
}
struct node *aptr;
aptr=*a;
// loop till you reach the last node of fist list.
while(aptr->next)
aptr=aptr->next;
// append.
aptr->next=*b;
*b=NULL;
while(aptr)
aptr=aptr->next;
runs till aptr is NULL, after that
aptr->next=*b;
causes a segmentation fault since you dereference NULL.