473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

double linked list

I have a simple implementation of double linked list
this code is crashing if i enter values not present in the linked
list(during insertion) or if i try to delete values not present in the
list
can u suggest how can i avoid it?

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

void main()
{
int i,n,c,x,m,flag=0;

struct node{
struct node* next;
int data;
struct node* prev;
}*h,*t,*start,*last,*z,*p,*q;

printf("enter the number of nodes");
scanf_s("%d",&n);
if(n<=0)
{
printf("no of nodes cannot be %d\n",n);
exit(0);
}
start=(node*)malloc(sizeof(node));
start->data=NULL;
start->prev=NULL;
start->next=NULL;

last=(node*)malloc(sizeof(node));
last->data=NULL;
last->next=NULL;
last->prev=NULL;

z=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&z->data);
start->next=h=z;
z->prev=NULL;
i=1;

while(i<n)
{
q=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&q->data);
z->next=q;
q->prev=z;
z=q;
++i;
}

z->next=NULL;
last->prev=t=z;

for(;;)
{
printf("\n");
printf("1.Insertion\n2.Deletion\n3.Display \n4.exit\n");
printf("\tenter your choice");
scanf_s("%d",&c);
switch(c)
{

case 1:
printf("enter the element after which node has to be inserted:\t");
scanf_s("%d",&x);

p=h;
if(x==-999)
{
z=(node*)malloc(sizeof(node));
printf("enter the data");
scanf_s("%d",&z->data);

z->next=p;
p->prev=z;
z->prev=NULL;

start->next=h=z;
}
else
{
while((p->data!=x)&&(p!=NULL))
{
p=p->next;
if(p==NULL)
{
printf("node cannot be inserted\n");
//exit(0);
}
}

if (p->next==NULL)
{
z=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&z->data);
p->next=z;
z->next=NULL;
z->prev=p;
last->prev=t=z;
}
else
{
q=p->next;
z=(node*)malloc(sizeof(node));
printf("enter the data");
scanf_s("%d",&z->data);
z->next=q;
q->prev=z;
p->next=z;
z->prev=p;
}
}
break;

case 2:
printf("enter the node to be deleted");
scanf_s("%d",&m);

p=h;
if(p->data==m)
{
start->next=h=p->next;
free(p);
}

else
{
//p=h;
q=p->next;
while((q->data!=m)&&(q!=NULL))
{
p=q;
q=q->next;
}
if(q==NULL)
{
printf("node cannot be deleted\n");
//exit(0);
}

else
{
p->next=q->next;
free(q);
}
}
break;

case 3:
p=start->next;
printf("data are");
while(p!=NULL)
{
printf("%5d",p->data);
p=p->next;
}
break;

case 4:
exit(0);
default:
printf("\t\tWrong Choice");
}
}
}

Nov 9 '06 #1
4 1879
peo_leo wrote:
I have a simple implementation of double linked list
this code is crashing if i enter values not present in the linked
list(during insertion) or if i try to delete values not present in the
list
can u suggest how can i avoid it?

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

void main()
int main (void)
start=(node*)malloc(sizeof(node));
You should not cast the return value of malloc, and it can be
helpful to use the sizeof the variable being assigned to as the
argument to malloc, in case its type ever changes:

start = malloc(sizeof(*start));

You should always check the return value of malloc for NULL.
while((p->data!=x)&&(p!=NULL))
It would be a real good idea to check p for NULL *before*
dereferencing it.

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB

Nov 9 '06 #2
"peo_leo" <pe*****@gmail.comwrites:
I have a simple implementation of double linked list
this code is crashing if i enter values not present in the linked
list(during insertion) or if i try to delete values not present in the
list
can u suggest how can i avoid it?

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

void main()
{
int i,n,c,x,m,flag=0;

struct node{
struct node* next;
int data;
struct node* prev;
}*h,*t,*start,*last,*z,*p,*q;
I am not going to solve the specific problem for you, but I do want to
help you get more help. By the time I got to this point in your
program I had lost heart and could not go on to see what detail was
actually causing your problem.

You need to break this program up into functions, each of which does
one simple to verify task, based on the parameters it is passed. If
this is a student exercise, you would loose out big time by not having
done this. If the program is just for your own study and you have
avoided function because you are unsure of them, you need to sort that
out before you get to dynamic data structures like linked lists. The
correct use of functions is the single most important element in
program design.

A big chuck of code like this puts people off. With well defined
subunits, the reader can check each one: "yes, that will append a new
node to list p", "yes, that will delete a node", and so on.

Finally, by avoiding functions you have made all your variables
global. OK, so they are not *technically* global, but they are all in
scope for the entire program and so the value for any of z, p, q, c, m,
x... (all unhelpful names, by the way) could affect any part of the
program.

--
Ben.
Nov 9 '06 #3
On 8 Nov 2006 22:40:48 -0800, "peo_leo" <pe*****@gmail.comwrote:
>I have a simple implementation of double linked list
this code is crashing if i enter values not present in the linked
list(during insertion) or if i try to delete values not present in the
list
can u suggest how can i avoid it?

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

void main()
int main(void)
>{
int i,n,c,x,m,flag=0;

struct node{
struct node* next;
int data;
struct node* prev;
}*h,*t,*start,*last,*z,*p,*q;

printf("enter the number of nodes");
scanf_s("%d",&n);
Is there some reason you cannot use standard functions? Anyone who
wants to compile your code has to fiddle with these.
> if(n<=0)
{
printf("no of nodes cannot be %d\n",n);
exit(0);
}
start=(node*)malloc(sizeof(node));
Don't cast the return from malloc. You never check to see if malloc
succeeds.
> start->data=NULL;
start->prev=NULL;
start->next=NULL;

last=(node*)malloc(sizeof(node));
last->data=NULL;
last->next=NULL;
last->prev=NULL;

z=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&z->data);
start->next=h=z;
z->prev=NULL;
i=1;

while(i<n)
{
q=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&q->data);
z->next=q;
q->prev=z;
z=q;
++i;
}

z->next=NULL;
last->prev=t=z;
BZZT! You no longer have a double linked list. last->prev points to
z but z->next obviously does not point to last.
>
for(;;)
{
printf("\n");
printf("1.Insertion\n2.Deletion\n3.Display \n4.exit\n");
printf("\tenter your choice");
scanf_s("%d",&c);
switch(c)
{

case 1:
printf("enter the element after which node has to be inserted:\t");
scanf_s("%d",&x);

p=h;
if(x==-999)
{
z=(node*)malloc(sizeof(node));
printf("enter the data");
scanf_s("%d",&z->data);

z->next=p;
p->prev=z;
z->prev=NULL;

start->next=h=z;
}
else
{
while((p->data!=x)&&(p!=NULL))
{
p=p->next;
if(p==NULL)
{
printf("node cannot be inserted\n");
If x is not in the list, you get here.
> //exit(0);
}
And then fall through to here. But at this point, p is NULL and any
attempt to evaluate p->data (in the above while statement) invokes
undefined behavior. A seg fault is good in this case.
> }

if (p->next==NULL)
{
z=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&z->data);
p->next=z;
z->next=NULL;
z->prev=p;
last->prev=t=z;
Again, the list is broken.
> }
else
{
q=p->next;
z=(node*)malloc(sizeof(node));
printf("enter the data");
scanf_s("%d",&z->data);
z->next=q;
q->prev=z;
p->next=z;
z->prev=p;
}
}
break;

case 2:
printf("enter the node to be deleted");
scanf_s("%d",&m);

p=h;
if(p->data==m)
{
start->next=h=p->next;
free(p);
}

else
{
//p=h;
q=p->next;
while((q->data!=m)&&(q!=NULL))
These tests are in the wrong order.
> {
p=q;
If x is not in the list, q will eventually equal z.
> q=q->next;
Now q is NULL. You loop back to the while statement and attempt to
evaluate q->data. A seg fault is one of the better results you can
hope for. If the tests were reversed, you would exit the while
cleanly.
> }
if(q==NULL)
{
printf("node cannot be deleted\n");
//exit(0);
}

else
{
p->next=q->next;
You broke the list again. p->next now points somewhere valid but you
forgot to set the ->prev in that somewhere to point to q. You need
something like
q->next->prev = p;
> free(q);
}
}
break;

case 3:
p=start->next;
printf("data are");
while(p!=NULL)
{
printf("%5d",p->data);
p=p->next;
}
break;

case 4:
exit(0);
default:
printf("\t\tWrong Choice");
}
}
}

Remove del for email
Nov 19 '06 #4
thx to all of u all for ur responses!!
gave me a good insight!!
On Nov 19, 11:12 pm, Barry Schwarz <schwa...@doezl.netwrote:
On 8 Nov 2006 22:40:48 -0800, "peo_leo" <peo....@gmail.comwrote:
I have a simple implementation of double linked list
this code is crashing if i enter values not present in the linked
list(during insertion) or if i try to delete values not present in the
list
can u suggest how can i avoid it?
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void main()int main(void)
{
int i,n,c,x,m,flag=0;
struct node{
struct node* next;
int data;
struct node* prev;
}*h,*t,*start,*last,*z,*p,*q;
printf("enter the number of nodes");
scanf_s("%d",&n);Is there some reason you cannot use standard functions? Anyone who
wants to compile your code has to fiddle with these.
if(n<=0)
{
printf("no of nodes cannot be %d\n",n);
exit(0);
}
start=(node*)malloc(sizeof(node));Don't cast the return from malloc. You never check to see if malloc
succeeds.
start->data=NULL;
start->prev=NULL;
start->next=NULL;
last=(node*)malloc(sizeof(node));
last->data=NULL;
last->next=NULL;
last->prev=NULL;
z=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&z->data);
start->next=h=z;
z->prev=NULL;
i=1;
while(i<n)
{
q=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&q->data);
z->next=q;
q->prev=z;
z=q;
++i;
}
z->next=NULL;
last->prev=t=z;BZZT! You no longer have a double linked list. last->prev points to
z but z->next obviously does not point to last.


for(;;)
{
printf("\n");
printf("1.Insertion\n2.Deletion\n3.Display \n4.exit\n");
printf("\tenter your choice");
scanf_s("%d",&c);
switch(c)
{
case 1:
printf("enter the element after which node has to be inserted:\t");
scanf_s("%d",&x);
p=h;
if(x==-999)
{
z=(node*)malloc(sizeof(node));
printf("enter the data");
scanf_s("%d",&z->data);
z->next=p;
p->prev=z;
z->prev=NULL;
start->next=h=z;
}
else
{
while((p->data!=x)&&(p!=NULL))
{
p=p->next;
if(p==NULL)
{
printf("node cannot be inserted\n");If x is not in the list, you get here.
//exit(0);
}And then fall through to here. But at this point, p is NULL and any
attempt to evaluate p->data (in the above while statement) invokes
undefined behavior. A seg fault is good in this case.
}
if (p->next==NULL)
{
z=(node*)malloc(sizeof(node));
printf("enter data");
scanf_s("%d",&z->data);
p->next=z;
z->next=NULL;
z->prev=p;
last->prev=t=z;Again, the list is broken.


}
else
{
q=p->next;
z=(node*)malloc(sizeof(node));
printf("enter the data");
scanf_s("%d",&z->data);
z->next=q;
q->prev=z;
p->next=z;
z->prev=p;
}
}
break;
case 2:
printf("enter the node to be deleted");
scanf_s("%d",&m);
p=h;
if(p->data==m)
{
start->next=h=p->next;
free(p);
}
else
{
//p=h;
q=p->next;
while((q->data!=m)&&(q!=NULL))These tests are in the wrong order.
{
p=q;If x is not in the list, q will eventually equal z.
q=q->next;Now q is NULL. You loop back to the while statement and attempt to
evaluate q->data. A seg fault is one of the better results you can
hope for. If the tests were reversed, you would exit the while
cleanly.
}
if(q==NULL)
{
printf("node cannot be deleted\n");
//exit(0);
}
else
{
p->next=q->next;You broke the list again. p->next now points somewhere valid but you
forgot to set the ->prev in that somewhere to point to q. You need
something like
q->next->prev = p;
free(q);
}
}
break;
case 3:
p=start->next;
printf("data are");
while(p!=NULL)
{
printf("%5d",p->data);
p=p->next;
}
break;
case 4:
exit(0);
default:
printf("\t\tWrong Choice");
}
}
Nov 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
32
by: Clunixchit | last post by:
How can i read lines of a file and place each line read in an array? for exemple; array=line1 array=line2 ...
6
by: deanfamily | last post by:
I am re-posting my second problem. I have a double-linked list. I need to know if it is possible to remove just one of an item, instead of all that match the given criteria with the remove()...
3
by: Little | last post by:
Could someone help me get started on this program or where to look to get information, I am not sure how to put things together. 1. Create 4 double linked lists as follows: (a) A double linked...
1
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double...
3
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
9
by: Sheldon | last post by:
Hi, I am trying to understand linked lists and the different ways to write a linked list and double linked list. I have been trying to get this function called insert_word to work but to no...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.