Thread: lists help

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4

    Question lists help

    Hi, i'm new and i've useing C programming language for a couple of months and i have problem: i can't sort lists
    e.g. : i type :3 and then 1 and 2 and i have to print 1/n 2/n 3/n.
    How to do this? Please help!!!
    i 've tried to do somethong bit i couldn't ;(

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what have you got?

    Show us the code, and we will help you along.

    Note that sorting linked lists essentially means swapping the pointers within the list - and it's definitely awkward. It is often simpler to "move" elements to a new list in sorted order (that is, delete from first list, and add it to a new lies using a variant of insertion sort).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int n;
    typedef struct Element  *PElement;
    
    struct Element {
           int data;
           PElement next; 
    };
    PElement stack,tmp2,tmp3;
    
    int var1=0,var2=0; 
    PElement push ()
    {
             
             PElement tmp = (PElement) malloc(sizeof(struct Element));
             
             
             
             printf("type some digit "); 
             scanf("%d", &tmp->data);
             tmp2 = tmp;
             var1 =tmp2->data;
    
             printf("var1 :%d var2 :%d \n",var1, var2);
             if(var1 < var2)
             {  
    
                 tmp->next = stack;
    
             }
             
             else 
             {
    
                 tmp -> next = stack;
                 stack = tmp;
                 tmp3 = stack;
                 var2 = tmp3->data;
             }
    
             printf("var1 :%d var2 :%d \n",var1, var2);
             return stack;
    
    }
    
    
    PElement pop ()
    {
             
             while(stack != NULL)
             {
                 printf("Data is %d\n" , stack->data);
                 stack = stack -> next; 
                 printf("\n\n\n");
    
             continue;
             }
             
    
            
            
            return stack; 
    }
    
    
    int main () 
    {
        for(;;)
        {
            printf("1 push \n");
            printf("2 pop\n");
            scanf("%d",&n);
            switch (n)
            {
                   case 1:push();
                   break;
                   case 2:pop();
                   break;
                   default:printf("MISTAKE");
             }
        }    
        
        getchar(); 
          
        return 0;  
    }
    i know this is wrong but i don't know how to fix it, i'm new in programming and i haven't
    met such problems before and i need the code for tomorrow so if you can help me untill then i'll be very thankful and appreciatеd thank you for helping me

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It looks like you started on an insertion list, but you need a loop to find the appropriate spot if it's not going at the first space of the list. Currently, you either insert at the top or in second place, which isn't quite right.

    [Actually, I don't think your insert works at all right - you need to set stack to the new element if you insert at the top, and if not, you need to fix it so that it inserts in the right place].

    --
    mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    Note that sorting linked lists essentially means swapping the pointers within the list - and it's definitely awkward. It is often simpler to "move" elements to a new list in sorted order
    Na, this is single linked list, it is not so hard to swap pointers and that will almost certainly be part of the assignment. I am somewhat worried about "by tomorrow" tho since I don't think this code contains any sorting mechanism yet (right?).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What's with the global variables. n, var1, var2, tmp, tmp2 should not be globals. stack should be in main and passed into/out of functions as appropriate.

    Your pop function alters the global stack pointer such that it's always NULL after the call. You're hemorrhaging memory because of this.

    Your functions return values but you ignore them.

    You have an unnecessary continue in your while loop.

    Don't cast malloc calls.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4
    Quote Originally Posted by hk_mp5kpdw View Post
    What's with the global variables. n, var1, var2, tmp, tmp2 should not be globals. stack should be in main and passed into/out of functions as appropriate.
    so what am i supposed to do? i don't know. if you know colud you tell? please?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mylord View Post
    so what am i supposed to do? i don't know. if you know colud you tell? please?
    var1 and var2 should be local to push().

    "stack" is maybe what most people would call "head", except your pop() function is wrong -- it will just go thru the whole list (not pop one item at a time off), and you will lose a pointer to your first element (which was in "stack", ie, the head), so you will now have no way to access the list.

    The normative way would be to declare the head in main and then use an initialize function. You should find a link list tutorial and use that. You must have these basics in order before you have a hope of sorting.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4
    Quote Originally Posted by MK27 View Post
    var1 and var2 should be local to push().

    "stack" is maybe what most people would call "head", except your pop() function is wrong -- it will just go thru the whole list (not pop one item at a time off), and you will lose a pointer to your first element (which was in "stack", ie, the head), so you will now have no way to access the list.

    The normative way would be to declare the head in main and then use an initialize function. You should find a link list tutorial and use that. You must have these basics in order before you have a hope of sorting.
    thank you! now i'm tottaly confused, but not because of you, but because of my lack of knowledge . Thank you, really
    i appriciate this

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I found the continue at the end of a while loop most amusing - for when you don't trust the loop to keep going on its own!
    printf("MISTAKE");!!!

    The Pop function actually leaks every item from the stack and returns NULL.
    The Push function actually sometimes performs a push.
    Niether Push nor Pop should be doing any IO at all - that's not their job.
    Four newlines between each value when printing them seems excessive.
    The main function has an infinite loop with no way to exit it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating lists of pointers
    By The_Kingpin in forum C Programming
    Replies: 2
    Last Post: 12-11-2004, 08:10 PM
  2. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM