Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,130 people online right now. Registration is fast and FREE... Join Now!




linked list

 
Reply to this topicStart new topic

linked list

mitchelltab
25 Sep, 2006 - 07:33 AM
Post #1

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
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 out it into a linked list
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;
            }
              
    


This post has been edited by mitchelltab: 25 Sep, 2006 - 07:49 AM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Linked List
25 Sep, 2006 - 08:29 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,013



Thanked: 18 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
The print method is fine, there is not an easier way to go about is as you must traverse the elements.
as far as sorting, bubble does take a while, try puting in a catch if you traverse all elements without trading then the sorting is done.
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Linked List
25 Sep, 2006 - 08:36 AM
Post #3

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
QUOTE(William_Wilson @ 25 Sep, 2006 - 09:29 AM) *

The print method is fine, there is not an easier way to go about is as you must traverse the elements.
as far as sorting, bubble does take a while, try puting in a catch if you traverse all elements without trading then the sorting is done.

sirry. but you are talking a little about my head So are you saying to add my linked list in while i doing the sort. how do i do that
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Linked List
25 Sep, 2006 - 10:05 AM
Post #4

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
You keep updating the linked list after every add operation.
This is known as the adaptive method.
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Linked List
25 Sep, 2006 - 10:47 AM
Post #5

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
QUOTE(born2c0de @ 25 Sep, 2006 - 11:05 AM) *

You keep updating the linked list after every add operation.
This is known as the adaptive method.

So in my sort array list I have to write the code for the linked list in there. So the way that i wrote it up top is wrong.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Linked List
26 Sep, 2006 - 06:05 AM
Post #6

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
A function should be written for a specific purpose only so that it can be reused at later stages and in programs.
So, No...Let the sort array be used to sort the array ONLY.

Instead call the sort function in the add()/delete() functions of the Linked List.
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Linked List
26 Sep, 2006 - 07:42 AM
Post #7

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
QUOTE(born2c0de @ 26 Sep, 2006 - 07:05 AM) *

A function should be written for a specific purpose only so that it can be reused at later stages and in programs.
So, No...Let the sort array be used to sort the array ONLY.

Instead call the sort function in the add()/delete() functions of the Linked List.

with the second code that i posted How would i do that. Or would I have to rewrite. I know that me code to sort works. So why can't i just add teh funcation to but it into a linked list after it has been sorted. That is what i was trying to dowith the second code that i wrote. Or show me where I'm going wrong. please.
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Linked List
26 Sep, 2006 - 01:45 PM
Post #8

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
i need hlep i have rewritten to code. But I'm still not able to get the numbers from the array to the linked list. any help

CODE
   #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define unit32 unsigned int
#define Max 50 //number to set array

typedef int (*CMPFUN)(int, int);
struct listNode
{
    int data;
    struct listNode *priorPtr;
    struct listNode *nextPtr;

}ListNode;

struct listNode  *start = NULL;        
struct listNode  *last = NULL;    
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;
}          

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;char text [10];  
   int Num[Max]; // numbers to be added to array
   int i, c, count;
    struct listNode  *start = NULL;        
    struct listNode  *last = NULL;    
    struct listNode  *data = NULL;
    struct listNode  *tempnode  = NULL;  
    srand( (unsigned)time( NULL ) );
   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< 5;i++)
    {
        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;
}


              



This post has been edited by mitchelltab: 27 Sep, 2006 - 11:02 AM
User is offlineProfile CardPM
+Quote Post

mitchelltab
RE: Linked List
27 Sep, 2006 - 08:29 AM
Post #9

D.I.C Head
**

Joined: 13 Jul, 2006
Posts: 104


My Contributions
I wanted to say thansk for all of you help. I was able to figure out my code.

This post has been edited by mitchelltab: 27 Sep, 2006 - 02:49 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:49AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month