runtime error

P

PRadyut

In the following code all the elements in the add function could not be
added in the linked list

The code : -

// test.cpp : Defines the entry point for the console application.

//



#include "stdafx.h"

#include "stdio.h"

#include "conio.h"

#include "string.h"

//#include "alloc.h"



void add(struct node **, int );

struct node

{

int data;

struct node *link;

};



int _tmain(int argc, _TCHAR* argv[])

{

struct node *p, *temp;

p=NULL;

/*append(&p, 1);

append(&p, 5);

append(&p, 17);

display(p);

addatbeg(&p, 23);

display(p);

addafter(&p, 2, 45);

display(p);

deletel(&p, 5);

display(p);*/

add(&p, 78);

add(&p, 32);

add(&p, 89);

add(&p, 75);

add(&p, 22);

add(&p, 49);

display(p);

temp =p;

int count;

for (count=0;temp;count++, temp=temp->link)

{}

printf("The size of the linked list is: %d", count);





getch();

return 0;

}



void add(struct node **q, int num)

{

struct node *r, *temp;

temp=*q;

r = (struct node*)malloc(sizeof(struct node));

r->data = num;

if (*q==NULL || (*q)->data >num)

{

*q = r;

(*q)->link=temp;

}

else

{

while(temp!=NULL)

{

if (temp->data <=num && (temp->data>num ||
temp->link==NULL))

{

r->link=temp->link;

temp->link=r;

return;

}

temp=temp->link;

}

}

}





The output in msvc compiler is : -



22

32

78

89



The size of the linked list is: 4



Any help

Thanks
Pradyut
http://pradyut.tk
http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming
India
 
B

Ben Pfaff

PRadyut said:
// test.cpp : Defines the entry point for the console application.

Your question is outside the domain of comp.lang.c, which discusses
only the standard C programming language, including the standard C
library. This is a remarkably narrow topic compared to what many
people expect.

For your convenience, the list below contains topics that are not
on-topic for comp.lang.c, and suggests newsgroups for you to explore
if you have questions about these topics. Please do observe proper
netiquette before posting to any of these newsgroups. In particular,
you should read the group's charter and FAQ, if any (FAQs are
available from www.faqs.org and other sources). If those fail to
answer your question then you should browse through at least two weeks
of recent articles to make sure that your question has not already
been answered.

* OS-specific questions, such as how to clear the screen,
access the network, list the files in a directory, or read
"piped" output from a subprocess. These questions should be
directed to OS-specific newsgroups, such as
comp.os.ms-windows.programmer.misc, comp.unix.programmer, or
comp.os.linux.development.apps.

* Compiler-specific questions, such as installation issues and
locations of header files. Ask about these in
compiler-specific newsgroups, such as gnu.gcc.help or
comp.os.ms-windows.programmer.misc. Questions about writing
compilers are appropriate in comp.compilers.

* Processor-specific questions, such as questions about
assembly and machine code. x86 questions are appropriate in
comp.lang.asm.x86, embedded system processor questions may
be appropriate in comp.arch.embedded.

* ABI-specific questions, such as how to interface assembly
code to C. These questions are both processor- and
OS-specific and should typically be asked in OS-specific
newsgroups.

* Algorithms, except questions about C implementations of
algorithms. "How do I implement algorithm X in C?" is not a
question about a C implementation of an algorithm, it is a
request for source code. Newsgroups comp.programming and
comp.theory may be appropriate.

* Making C interoperate with other languages. C has no
facilities for such interoperation. These questions should
be directed to system- or compiler-specific newsgroups. C++
has features for interoperating with C, so consider
comp.lang.c++ for such questions.

* The C standard, as opposed to standard C. Questions about
the C standard are best asked in comp.std.c.

* C++. Please do not post or cross-post questions about C++
to comp.lang.c. Ask C++ questions in C++ newsgroups, such
as comp.lang.c++ or comp.lang.c++.moderated.

* Test posts. Please test in a newsgroup meant for testing,
such as alt.test.

news.groups.questions is a good place to ask about the appropriate
newsgroup for a given topic.
 
C

CBFalconer

PRadyut said:
In the following code all the elements in the add function could
not be added in the linked list

The code : -

// test.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "stdio.h"

#include "conio.h"

#include "string.h"

//#include "alloc.h"
So far your errors include:
Using // comments (illegal in C90, unwise on newsgroups)
3 out of 5 #includes address headers not available in std C
Failure to use the proper <> include mechanism for system hdrs.

so there is not much point in evaluating anything else, is there?
Your habit of double spacing the entire article is also highly
annoying.
 
P

pete

PRadyut said:
In the following code all the elements
in the add function could not be
added in the linked list

The code : -

// test.cpp : Defines the entry point for the console application.

/* BEGIN test.c */

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

struct node {
struct node *link;
int data;
};

void add(struct node **q, int num);
int display(struct node *p);
void free_list(struct node *p);

int main(void)
{
struct node *p;
int count;

p = NULL;
add(&p, 78);
add(&p, 32);
add(&p, 89);
add(&p, 75);
add(&p, 22);
add(&p, 49);
count = display(p);
free_list(p);
printf("The size of the linked list is: %d", count);
return 0;
}

void add(struct node **q, int num)
{
struct node *r, *temp;

temp = *q;
r = malloc(sizeof *r);
if (r == NULL) {
exit(EXIT_FAILURE);
}
r -> data = num;
if (*q == NULL || temp -> data >= num) {
*q = r;
(*q) -> link = temp;
} else {
for (;;) {
if (temp -> link == NULL || temp -> link -> data >= num ) {
r -> link = temp -> link;
temp -> link = r;
break;
}
temp = temp -> link;
}
}
}

int display(struct node *p)
{
int count;

for (count = 0; p != NULL; p = p -> link) {
++count;
printf("%d\n", p -> data);
}
return count;
}

void free_list(struct node *p)
{
struct node *next;

while (p != NULL) {
next = p -> link;
free(p);
p = next;
}
}

/* END test.c */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Infinite loop problem 1
linked list error 4
Queue in C 25
please check 10
merging list problem 15
linked list error 1
Drawing missing in bitmap in a pure C win32 program 4
Queue in C 0

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top