Thread: linked list

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    104

    Question linked list

    OK this program runs but I need some help. I need to sorted array elements into a linked list and the print the data using the linked list. But I think that Im doing this the hard way any ideas

    Code:
     #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define unit32 unsigned int 
    #define Max 50 //number to set array
    
    typedef int (*CMPFUN)(int, int);
    
    void ArraySort(int Num[], CMPFUN fun_prt, unit32 ub)
    {  //start bubble array
       
               unit32 indx;
               unit32 indx2;
               int temp;
               int temp2;
               int flipped;
               
               if (ub <= 1)
                return;
                
                indx =1;
                do
                { 
                      flipped = 0;
                      for (indx2 = ub-1; indx2 >= indx; --indx2)
                      {
                          temp = Num[indx2];
                          temp2 = Num[indx2 -1];
                          if ((*fun_prt)(temp2,temp)>0)
                          {
                              Num[indx2 -1] = temp;
                              Num[indx2] = temp2;
                              flipped =1;
                           }
                      }
                }while ((++indx < ub) && flipped);
    }
    
    int cmpfun (int a, int b)
    {
        if (a > b)
         return 1;
        else if (a < b)
          return -1;
         else
           return 0;
    }                  
    main ()
    {
       char text [10];  
       int Num[Max]; // numbers to be added to array
       int i, c, count;
      
    
       count=0;
       
       
      for ( i = 0; i <  Max != 0;
    i++ )
       {
          fputs ("Please enter a nunber: ", stdout);
          if ( fgets(text, sizeof text, stdin) )
          {
             if ( *text == '\n' )
             {
                break;
             }
             if ( sscanf(text, "%d", &Num[i]) != 1 )
             {
                break;
             }
          }
    
       }
    
       count = i;
       ArraySort(Num, cmpfun, count);
    
       for ( i = 0; i < count; i ++ )
       {
          printf ("%d\t",  Num[i]);
       }
    
    
      printf ("      \n");
      printf ("HIT ENTER TO EXIT\n");
       getchar();
       getchar();
       return 0;    
    }
    this is how i tryed to put it into a linked lost
    Code:
     #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define unit32 unsigned int 
    #define Max 50 //number to set array
    
    typedef struct Num
      {
      int Num[Max];
      struct numbers* next;
    } Num;
    
    typedef int (*CMPFUN)(int, int);
    
    void print_Num (const Num* prt);
    Num* get_Num( void );
    
    void ArraySort(int Num[], CMPFUN fun_prt, unit32 ub)
    {  //start bubble array
       
               unit32 indx;
               unit32 indx2;
               int temp;
               int temp2;
               int flipped;
               
               if (ub <= 1)
                return;
                
                indx =1;
                do
                { 
                      flipped = 0;
                      for (indx2 = ub-1; indx2 >= indx; --indx2)
                      {
                          temp = Num[indx2];
                          temp2 = Num[indx2 -1];
                          if ((*fun_prt)(temp2,temp)>0)
                          {
                              Num[indx2 -1] = temp;
                              Num[indx2] = temp2;
                              flipped =1;
                           }
                      }
                }while ((++indx < ub) && flipped);
    }
    
    int cmpfun (int a, int b)
    {
        if (a > b)
         return 1;
        else if (a < b)
          return -1;
         else
           return 0;
    }                  
    main ()
    {
       char text [10];  
       int Num[Max]; // numbers to be added to array
       int i, c, count;
      
    
       count=0;
       
       
      for ( i = 0; i <  Max != 0;
    i++ )
       {
          fputs ("Please enter a nunber: ", stdout);
          if ( fgets(text, sizeof text, stdin) )
          {
             if ( *text == '\n' )
             {
                break;
             }
             if ( sscanf(text, "%d", &Num[i]) != 1 )
             {
                break;
             }
          }
    
       }
    
       count = i;
       ArraySort(Num, cmpfun, count);
       Num*;
      start = get_Num ();
       print_Num;
    
    
      printf ("      \n");
      printf ("HIT ENTER TO EXIT\n");
       getchar();
       getchar();
       return 0;    
    }
      
    Num* get_Num( void )
    {
         Num* *current, *first;
         int responce;
         
         current = first = malloc(sizeof ( Num ));
         while (responce){
               if ((current -> next)=
                  malloc (sizeof (Num))) == Null){
                  printf ( "OUut of memory\n Can't add more Nums\n");
                return first;
                    }
           
           current = current -> next;
          }
    current -> next = NULL;
    return first;
    }
    
    void print_Num(const Num* ptr)
    {
         int count 1;
         printf (\n\n\n\);
             while ptr != NULL){
               printf ("%d\t",  Num);
               ptr = ptr -> next;
                }
    Last edited by redmondtab; 09-25-2006 at 09:47 AM.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    IMHO, I believe it would be easier to just use a sorted linked list. That is, just enter your numbers into the linked list and let it sort it for you.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    so how would I do that

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Review this old thread and you'll find code to develop a complete sorted doubly linked list Linked List thread

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    104

    Angry

    ok i looked at that code and this is what I came up with. Let me know if i'm doing this right. I still cant get it to run.
    Code:
     #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define unit32 unsigned int 
    #define Max 50 //number to set array
    
    typedef struct Num
      {
      int Num[Max];
      struct numbers* next;
    } Num;
    
    typedef int (*CMPFUN)(int, int);
    
    void print_Num (const Num* prt);
    Num* get_Num( void );
    
    void ArraySort(int Num[], CMPFUN fun_prt, unit32 ub, Num*)
    {  //start bubble array
       
               unit32 indx;
               unit32 indx2;
               int temp;
               int temp2;
               int flipped;
               
               if (ub <= 1)
                return;
                
                indx =1;
                do
                { 
                      flipped = 0;
                      for (indx2 = ub-1; indx2 >= indx; --indx2)
                      {
                          temp = Num[indx2];
                          temp2 = Num[indx2 -1];
                          if ((*fun_prt)(temp2,temp)>0)
                          {
                              Num[indx2 -1] = temp;
                              Num[indx2] = temp2;
                              flipped =1;
                           }
                      }
                }while ((++indx < ub) && flipped);
    }
    
    int cmpfun (int a, int b)
    {
        if (a > b)
         return 1;
        else if (a < b)
          return -1;
         else
           return 0;
    }   
    
      
    int Num* (get_Num)
    {
         Num* *current, *first;
         int responce;
         
         current = first = malloc(sizeof ( Num ));
         while (responce){
               if ((current -> next)=
                  malloc (sizeof (Num))) == Null){
                  printf ( "OUut of memory\n Can't add more Nums\n");
                return first;
                    }
           
           current = current -> next;
          }
    current -> next = NULL;
    return first;
    }               
    main ()
    {
       char text [10];  
       int Num[Max]; // numbers to be added to array
       int i, c, count;
      
    
       count=0;
       
       
      for ( i = 0; i <  Max != 0;
    i++ )
       {
          fputs ("Please enter a nunber: ", stdout);
          if ( fgets(text, sizeof text, stdin) )
          {
             if ( *text == '\n' )
             {
                break;
             }
             if ( sscanf(text, "%d", &Num[i]) != 1 )
             {
                break;
             }
          }
    
       }
    
       count = i;
       ArraySort(Num, cmpfun, count, Num*);
    
    
    
      printf ("      \n");
      printf ("HIT ENTER TO EXIT\n");
       getchar();
       getchar();
       return 0;    
    }
    
    
    void print_Num(const Num* ptr)
    {
         int count 1;
         printf (\n\n\n\);
             while ptr != NULL){
               printf ("%d\t",  Num);
               ptr = ptr -> next;
                }

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    struct listNode
    {
    	int data;
    	struct listNode *priorPtr;
    	struct listNode *nextPtr;
    
    }ListNode;
    
    struct listNode  *start = NULL;         
    struct listNode  *last = NULL;           
    
    void insert( struct listNode   *newnode,  /* new node */
    				struct listNode   **start, /* first node */
    				struct listNode   **last /* last node */
    				)
    {
    	struct listNode  *oldptr, *ptr;
    	if(*last == NULL) /* first node in linklist */
    	{
    		newnode->nextPtr = NULL;
    		newnode->priorPtr = NULL;
    		*last = newnode;
    		*start = newnode;
    		return;
    	}
    	ptr = *start; /* start at top of linklist */
    	oldptr = NULL;
    	while(ptr)
    	{
    		if(ptr->data < newnode->data )
    		{
    			oldptr = ptr;
    			ptr = ptr->nextPtr;
    		}
    		else
    		{
    			if(ptr->priorPtr)
    			{
    				ptr->priorPtr->nextPtr = newnode;
    				newnode->nextPtr = ptr;
    				newnode->priorPtr = ptr->priorPtr;
    				ptr->priorPtr = newnode;
    				return;
    			}
    			newnode->nextPtr = ptr; /* newnode first node */
    			newnode->priorPtr = NULL;
    			ptr->priorPtr = newnode;
    			*start = newnode;
    			return;
    		}
    	}
    	oldptr->nextPtr = newnode;
    	newnode->nextPtr = NULL;
    	newnode->priorPtr = oldptr;
    	*last = newnode;
    }
    
    int main(void)
    {
    	int x;
    	struct listNode  *start = NULL;         
    	struct listNode  *last = NULL;    
    	struct listNode  *data = NULL;
    	struct listNode  *tempnode  = NULL;   
    	srand( (unsigned)time( NULL ) );
    	for(x = 0; x< 5;x++)
    	{
    		data = (struct listNode  *) malloc(sizeof(listNode));
    		if(!data)
    			return -1;
    		data->data=  rand() ;
    		insert(data, &start, &last);
    	}
    	tempnode = start;
    	printf("From beginning to end\n");
    	while(tempnode) // print out the list  from beginning to end
    	{
    		printf("data = %d\n",tempnode->data);
    		tempnode = tempnode->nextPtr; 
    	}
    	tempnode = last;
    	printf("\n\nFrom end to beginning\n");
    	while(tempnode) // print out the list  from end to beginning
    	{
    		printf("data = %d\n",tempnode->data);
    		tempnode = tempnode->priorPtr; 
    	}
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    what you posted was just to do the linked list. I would still have to add that with my program to store the numbers into that list. Is this correct.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    i got it to work. Thanks for all of your help with the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM