runtime error

Discussion in 'C Programming' started by PRadyut, May 18, 2005.

  1. PRadyut

    PRadyut Guest

    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
    PRadyut, May 18, 2005
    #1
    1. Advertising

  2. PRadyut

    Ben Pfaff Guest

    "PRadyut" <> writes:

    > // 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.

    --
    "Large amounts of money tend to quench any scruples I might be having."
    -- Stephan Wilms
    Ben Pfaff, May 18, 2005
    #2
    1. Advertising

  3. PRadyut

    CBFalconer Guest

    PRadyut wrote:
    >
    > 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.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, May 18, 2005
    #3
  4. PRadyut

    pete Guest

    PRadyut wrote:
    >
    > 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 */

    --
    pete
    pete, May 19, 2005
    #4
  5. PRadyut

    PRadyut Guest

    PRadyut, May 19, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Polo Lee
    Replies:
    0
    Views:
    2,722
    Polo Lee
    Jul 7, 2003
  2. Yongsub Eric Shin
    Replies:
    7
    Views:
    856
    Martin
    Jan 14, 2004
  3. Kevin Farless
    Replies:
    13
    Views:
    15,206
    chesterchass
    Oct 3, 2007
  4. Krunom Ancini
    Replies:
    2
    Views:
    5,163
  5. Hal Vaughan
    Replies:
    11
    Views:
    1,105
    Gordon Beaton
    May 22, 2006
Loading...

Share This Page