473,320 Members | 2,020 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,320 software developers and data experts.

Singular Link List

Hello,
I am Learning Link List. Today wrote a simple program, but not working. Program is like this
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<alloca.h>
  4.  
  5.         struct node{
  6.         int data;
  7.         struct node *link;
  8.         };
  9. void addafter(struct node **q,int value);
  10. void display(struct node *q);
  11. void main()
  12. {
  13.         struct node *p;
  14.         p=NULL;
  15.         addafter(&p,11);
  16.         addafter(&p,12);
  17.         addafter(&p,13);
  18.         addafter(&p,14);
  19.                 display(p);
  20. }
  21. void addafter(struct node **q,int value)
  22. {
  23.         struct node *temp,*r ;
  24.         if(*q==NULL){
  25.                 temp=(struct node *)malloc(sizeof (struct node));
  26.                 temp->data=value;
  27.                 temp->link=NULL;
  28.                 *q=temp;
  29.         free(temp);
  30.                 }
  31.         else{
  32.         temp=*q;
  33.                 while(temp->link!=NULL)temp=temp->link;
  34.                 r=(struct node *)malloc(sizeof(struct node));
  35.                 r->data=value;
  36.                 r->link=NULL;
  37.                 temp->link=r;
  38.                 *q=temp;
  39.                 }
  40. }
  41. void display(struct node *q)
  42. {
  43.         while(q!=NULL)
  44.                 {
  45.                 printf("%d\n",q->data);
  46.                 q=q->link;
  47.                 }
  48. }
  49.  
I am supposed to get answer 11 12 13 14, but getting only last two numbers 13 and 14. First time when i executed this code it gave correct answer but next time onwards wrong. I think somewhere memory corruption is happening.
I am trying to execute this code in LINUX. Please anybody can help.
Jul 10 '07 #1
4 1980
bartonc
6,596 Expert 4TB
Moved from Member Introductions.
Jul 10 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
I made some notes in your code below. I didn't look at the while thing.

void addafter(struct node **q,int value)
{
struct node *temp,*r ;
if(*q==NULL){
temp=(struct node *)malloc(sizeof (struct node));
temp->data=value;
temp->link=NULL;
*q=temp;
free(temp); <<<you just deleted the node but *q still has the address!!!
<<<do not delete the node until you remove the node
<<<from the list.
}
else{
temp=*q;
while(temp->link!=NULL)temp=temp->link;
r=(struct node *)malloc(sizeof(struct node));
r->data=value;
r->link=NULL;
temp->link=r;
*q=temp; <<<temp is already *q so this does nothing
<<<did you mean *q = r ???
}
}
Jul 10 '07 #3
Moved from Member Introductions.
I did not get you. What you want to say? please.
Jul 11 '07 #4
I made some notes in your code below. I didn't look at the while thing.
Thanks, this solved my problem.
Jul 11 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Shawn Windle | last post by:
----begin node.h-------- #ifndef NODE_H #define NODE_H #include <iostream> //NULL using namespace std; class node {
8
by: sudhirlko2001 | last post by:
How to swap two nodes of doubly Linklist
2
by: Steve - DND | last post by:
Just wondering if anyone out there has any code to convert a plural word to it's singular form and vice versa. Most of our database tables are named in a plural fashion. When we go to create...
2
by: Robert W. | last post by:
In my current work I noticed that I have several circumstances where I need to create little if/else constructs to handle the phrasing of a message. This typically involves a ternary situation like...
7
by: Bosconian | last post by:
This might seem like a trivial thing, but has anyone has come up with a better way of outputting a singular vs. plural string? For example: // default plural label $string = "appointments";...
2
by: eyh5 | last post by:
Hi, I'm wondering where I may get the C source code for a linear-algebraic technique known as the singular value decomposition (SVD). I found the code (a function called svdcmp) from...
4
by: debiasri | last post by:
Hello, I searching the code for singular value decomposition for using it in AMMI model analysis. Kindly somebody help me.
1
by: vinay3744 | last post by:
Hi I am currently working on figuring out a code for performing a singular value decomposition for String arrays. Can some one help me get started on this. I have already done a declaration...
0
ADezii
by: ADezii | last post by:
Rather than using CurrentProject.Connection or entering your own Connection information, ADO supports storing Connection information in an external file called a Data Link File (which normally has a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.