plz help me run this program!!!

This is a discussion on plz help me run this program!!! within the C Programming forums, part of the General Programming Boards category; i've written a code for insertion in a linked list for all the 3 cases that is ...at the end,in ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    plz help me run this program!!!

    i've written a code for insertion in a linked list for all the 3 cases that is ...at the end,in the beginning & in between.....when i run this code then it runs successfully for insertion at end and in the beginning ,it is also showing no errors while compiling the program but when i run this code for insertion in between then it prints like this:
    enter the size of list:5
    enter list:
    10
    20
    30
    40
    50
    Enter 1.to insert at end
    Enter 2.to insert in the beginning
    Enter 3.to insert in the between
    //so, when i enter here choice number 3.to insert in between
    3.
    enter the location after which the number has to be inserted:3
    enter number to be inserted:100
    then it doesn't print anything and comes back to the program...that is the screen where the source program was written.why?it's not printing anything.plz make necesaary changes in my code to make it properly run....plz help me out with my code...
    Code:
    #include<stdio.h>
    #include<conio.h>
    struct link
    {
    int data;
    struct link*ptr;
    };
    typedef struct link node;
    void main()
    {
    int size,i,choice;
    char ch;
    node*start,*temp,*p;
    clrscr();
    do
    {
    printf("enter the size of list:");
    scanf("%d",&size);
    printf("enter list\n");
    start=NULL;
    for(i=0;i<size;i++)
    {
    if(start==NULL)
    {
    start=(node*)malloc(sizeof(node));
    scanf("%d",&start->data);
    start->ptr=NULL;
    }
    else
    {
    for(p=start;p->ptr!=NULL;p=p->ptr);
    temp=(node*)malloc(sizeof(node));
    scanf("%d",&temp->data);
    temp->ptr=NULL;
    p->ptr=temp;
    }
    }
    printf("Enter 1.To insert at the end\n");
    printf("Enter 2.To insert at the beginning\n");
    printf("Enter 3.To insert in between\n");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1:
    end(start,p);
    break;
    case 2:
    beg(start,p);
    break;
    case 3:
    between(start,p,size);
    break;
    default:
    printf("wrong number entered\n");
    }
    printf("Enter y to continue\n");
    scanf("%c",&ch);
    }
    while(ch=='Y'||ch=='y');
    getch();
    }
    end(node*start,node*p)
    {
    node*temp;
    for(p=start;p->ptr!=NULL;p=p->ptr)
    temp=(node*)malloc(sizeof(node));
    printf("enter the number to be inserted\n");
    scanf("%d",&temp->data);
    temp->ptr=NULL;
    p->ptr=temp;
    for(p=start;p!=NULL;p=p->ptr)
    printf("%d",p->data);
    return(0);
    }
    beg(node*start,node*p)
    {
    node*temp;
    temp=(node*)malloc(sizeof(node));
    printf("enter the number to be inserted\n");
    scanf("%d",&temp->data);
    temp->ptr=start;
    start=temp;
    printf("new linked list is:\n");
    for(p=start;p!=NULL;p=p->ptr)
    printf("%d",p->data);
    return(0);
    }
    between(node*start,node*p,int size)
    {
    node*temp;
    int loc,count=0;
    printf("enter the location after which the number has to be inserted\n");
    scanf("%d",&loc);
    if(loc<=size)
    {
    temp=(node*)malloc(sizeof(node));
    printf("enter the number to be inserted\n");
    scanf("%d",&temp->data);
    for(p=start;p!=NULL;p=p->ptr)
    {
    count++;
    if(count==loc)
    {
    temp->ptr=p->ptr;
    p->ptr=temp;
    printf("new linked list is:");
    for(p=start;p!=NULL;p=p->ptr)
    printf("%d",p->data);
    return(0);
    }
    else
    {
    printf("location is not found\n");
    return(0);
    }
    }
    }
    return(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > void main()
    Is this a special skill you have, or are you truly stupid?

    Because I'm totally fed up of seeing your posts which routinely break so many rules and so poorly formatted to just make people puke!

    binary search program
    inserting an element into an array
    a small problem......plz help me out guys!!!!!!
    array in ascending order
    printing array elements in ascending order
    help!!!!!
    plz help me out guyz!!!!!!!!
    plz help me out......the code is correct but it's not running well.....
    array ,plz let me know
    array problem
    fibonacci problem using while loop
    c program on grades of a student

    Look back at the sorry messes you've posted, and what people have said in the past (many, many times)

    If you want people to help you, you'd better start making it easy for people to help you.

    plz is not an English word.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    It would be much easier to read your code if it were indented.

    Take a look at your between function. This is about what it says in pseudo-code right now:
    Code:
    loc = location to insert
    value = value to insert
    if loc <= size
        for each element in the list
            if this is the location
                insert element
                print list
                quit
            else
                print error message
                quit
        next element
    end if
    I think what you're going for is more like this:
    Code:
    loc = location to insert
    value = value to insert
    if loc <= size
        for each element in the list
            if this is the location
                insert element
                print list
                quit
        next element
        print error message
    end if
    Also, you'll want to free that temp value you're allocating if you don't find the location. Otherwise, it'll leak.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    i thought some 1 here would like to help me...?but i guess i was wrong....if i have come here and posted my code thats because i thought that by seeking help i would get some help...but i think salem is not willing to help me...but if any 1 here would like to help me..other than him then i would be truly grateful.......

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    It's not about wanting to help you. It's about you not listening to what advice people give you and your refusal to put forth enough energy to make it easy for people to help you.

    Why should we try to help when you're not going to listen to what we say?
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ask yourself this: Would you bother to read someone else's code, if it was not properly indented, and continued to break the same rules over and over?

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Here, I solved your problem for you. I tried to preserve your original formatting.
    Code:
    between(_OO,_,_00)node* _OO;node *_;{
    node*_0O;int _O0,O0;for(printf("enter\
     the location after which the number \
    has to be inserted\n"),scanf("%d",&_O0
    ),_=_OO,_O0--;_!=NULL&&_O0<=_00&&_O0!=
    0;_=_->ptr,_O0--);(_O0==0&&_!=NULL&&(
    printf("enter the number to be insert\
    ed\n"),scanf("%d",&O0),_0O=_->ptr,_->
    ptr=(node*)malloc(sizeof(*_)),_->ptr->
    ptr=_0O,_->ptr->data=O0,printf("new l\
    inked list is:\n")));if (_O0==0&&_!=
    NULL){for(_=_OO;_!=NULL;printf("%d\n",
    _->data),_=_->ptr);return 0;}printf("\
    location is not found\n");return !0;}
    This is the problem with unformatted code. It's difficult to read. Please follow the forum guidelines.

    By the way, it'll take you more time to untangle the above than it would to actually make the change to your program that I suggested earlier.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Bad habits die hard but you have to put some effort on changing your style man. i mean from the books you have read did you find anything that looks like your code? People will help you if they can read your code, if they cant they will not waste their time to figure out how to read your code so they can help you.
    When no one helps you out. Call google();

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    You're no longer worth the effort to talk to galmca
    Closed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Help me with my basic program, nothing I create will run
    By Ravens'sWrath in forum C Programming
    Replies: 31
    Last Post: 05-13-2007, 03:35 AM
  4. assmbly program.....Need to run
    By gopipunjabi in forum Tech Board
    Replies: 5
    Last Post: 12-05-2005, 02:24 AM
  5. program won't run standalone
    By richard in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2002, 07:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21