• C

program help

Here is my source so far. I'm looking for help to debug and complete this. Anything someone could do to help would be great!

#include<stdlib.h>
#include<time.h>
#include<stdio.h>

struct ListNode{
      int data;
struct Listnode *nextptr;
};

typedef struct ListNode LISTNODE;
typedef LISTNODE  *LISTNODEPTR;


void InsertList(LISTNODEPTR *, int);

printList(LISTNODEPTR, int);

sumList(LISTNODEPTR, int);

averageList(LISTNODEPTR, int);


main()
{
      LISTNODEPTR listptr = NULL;
    int i;
      int x;
      srand(time(NULL));
      
      for(i = 1; i <= 25; i++)
      {
            i = rand()%101;
            InsertList(&listptr,x);
      }
      printList(listptr);

      printf("the sum is %d\n",sumList(listptr));

      printf("The average is %f\n", averageList(listptr));

      return 0;
}


void InsertList(LISTNODEPTR *listptr, int info)/*I have some problems with this function*/
{
      LISTNODEPTR current;
      current = malloc(sizeof(LISTNODE));
      
      listptr -> info = current -> info;
      current -> info = listptr;
}

int printList(LISTNODEPTR, int p)/*need help creating function*/

int sumList(LISTNODEPTR, int)
{
/*need help here also*/      



}

timeforsleepAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

captainkirkCommented:
1) what specific problems are you having??

2) I noticed that you have a #include within your struct ListNode definition... might want to get that moved up to where the other #includes are...
0
timeforsleepAuthor Commented:
Edited text of question.
0
timeforsleepAuthor Commented:
I'm having trouble with the InsertList function.  Also I just don't understanding how to get the data from the list to print(printList function). I was looking for any sample code the would illustrate this.
0
mmessuriCommented:
timeforsleep:

Here is a snippet of one of my linked list lib's.  This should help you out in the inserting of nodes function (note that nfree and alloc_mem are just wrapper functions around the functions free and calloc).

Hope this helps.

void add_node(void *item, LINKEDLIST **llist)
{
      LINKEDLIST *nnode = NULL;

      if( (*llist) == NULL)
            init_list(llist);
      else{
            nnode = (LINKEDLIST *)alloc_mem(1, sizeof(LINKEDLIST));
            (*llist)->next = nnode;
            nnode->prev = (*llist);
            (*llist) = nnode;
      }
      (*llist)->item = item;
}

void init_list(LINKEDLIST **llist)
{
  (*llist) = (LINKEDLIST *)alloc_mem(1, sizeof(LINKEDLIST));
}

/*
 -- Note: This delete_node function does not free any
 --                        dynamicly allocated memory in use by the
 --                    object whose address is stored in the
 --                        void ptr item.  It is up to the user to
 --                        free this.
*/
void delete_node(LINKEDLIST **llist)
{
      LINKEDLIST *pnode;

      if( (*llist) == NULL)
            return;

      if( (*llist)->prev != NULL){
            pnode = (*llist)->prev;
            pnode->next = NULL;
            nfree((void **)llist);
            (*llist) = pnode;
      } else
             nfree((void **)llist);
}

/*
 -- Rather than trusting the user to clean up the memory in use
 -- by the object pointed to via the void ptr item; the user
 -- can call free_node rather than delete_node when removing a
 -- node from the linked list.
*/
void free_node(LINKEDLIST **llist)
{
      void *list_item = NULL;

      /* First free the object pointed to by item */
  if( (*llist)->item != NULL){
            list_item = (*llist)->item;
        nfree(&list_item);
      }

      /* Now free the node itself */
      delete_node(llist);
}

/*
 -- Insert node is used to insert a new linked list node into
 -- the x position from the start of the list. Note that this
 -- is a zero based function (i.e. the first node of this list
 -- is node #0)
*/
void insert_node(int x, void *item, LINKEDLIST **llist)
{
      LINKEDLIST *start;
      LINKEDLIST *nnode;
  LINKEDLIST *t1;

  int pos = 0;
      int totalnodes = 1;
      

  start = find_start(&totalnodes, llist);      

      /* We are now standing at node #0 */

      if(totalnodes > 1 && x <= totalnodes){
            while(pos != x - 1){
                  start = start->next;      
                  pos++;
            }

            /* The next node position is where we want to insert the new
                   node at.
            */
            nnode = (LINKEDLIST *)alloc_mem(1, sizeof(LINKEDLIST));
            nnode->item = item;

            t1 = start->next;

            /* Correct all of the next/prev node pointers */
            nnode->prev = start;
            nnode->next = t1;
            start->next = nnode;
            t1->prev = nnode;
  }
}

/*
 -- remove node is used to remove a linked list node at
 -- the x position from the start of the list. Note that this
 -- is a zero based function (i.e. the first node of this list
 -- is node #0).  Should the user need to free the object pointed
 -- to by item, then the user should set the item_free var to 1
*/
void remove_node(int x, unsigned int item_free, LINKEDLIST **list)
{
      LINKEDLIST *start;
      LINKEDLIST *t1;
      LINKEDLIST *t2;
      int totalnodes = 1;
      int pos = 0;
      
      /* Move to the start of the list */
      start = find_start(&totalnodes, list);

      if(x > totalnodes)
            return;

      while(pos <= x){
            start = start->next;
            pos++;
      }

      t1 = start->next;
      t2 = start->prev;

  if(item_free && start->item != NULL)
            nfree(&start->item);
      nfree((void *)&start);

      if(t1 != NULL){
        t1->prev = t2;
        t2->next = t1;      
      }
}

FILE: listobjs.h

#ifndef TRUE
  #define TRUE 1
  #define FALSE 0
#endif

#ifndef BOOL_ID
  typedef unsigned int BOOL;
  #define BOOL_ID
#endif

/*
 -- Define a generic linked list node.
 -- Through the use of a void pointer, this linked list will
 -- support any type.  
*/

typedef struct LIST_NODE {
      void *item;
      struct LIST_NODE *next;
      struct LIST_NODE *prev;
} LINKEDLIST;
0
szetoaCommented:
/***
timeforsleep,

I think it's time for you to take a break before working on your code again.  It works for me every time I got stuck in a problem :)  Anyway, here's what I found in your code that may help.  In your code:
==========================
for(i = 1; i <= 25; i++)
{
    i = rand()%101;
    InsertList(&listptr,x);
}
==========================
You are using i as an index to your loop count, but inside the for loop, you SET i to a new number.  And you pass x to the function InsertList.  Should it be: x = rand()%101; instead???

Also, you defined your structure as:
==========================
struct ListNode{
    int data;
    struct Listnode *nextptr;
};
==========================

AND your InsertList function as:
==========================
void InsertList(LISTNODEPTR *listptr, int info)
{
    LISTNODEPTR current;
    current = malloc(sizeof(LISTNODE));
    listptr -> info = current -> info;
    current -> info = listptr;
}
==========================
The only elements in ListNode are data and nextptr.  Shouldn't the last two statements in your function be:

listptr->data = current->data;
current->nextptr = listptr;

Usually when dealing with link list, you need more than one pointers to keep track of things.  I usually use one pointer to point to the head of the list and one to the current (or last) node on the list (forget about sorting for now).  I have modified your code to make it to work.  You just need to understand it and implement your own sumList() and averageList().
***/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct ListNode{
    int data;
    struct ListNode *nextptr;
};

typedef struct ListNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;

void InsertList(LISTNODEPTR *, int);
printList(LISTNODEPTR);
sumList(LISTNODEPTR);
averageList(LISTNODEPTR);

main()
{
    LISTNODEPTR listptr = NULL,  // head pointer
                curptr  = NULL;  // current node pointer
    int i, x;
    srand(time(NULL));

    for(i = 1; i <= 25; i++)
    {
        x = rand()%101;
        InsertList(&curptr,x);

        if ( ! listptr )  // set first node
            listptr = curptr;
    }

    printList(listptr);
    printf("the sum is %d\n",sumList(listptr));
    printf("The average is %d\n", averageList(listptr));
    return 0;
}

void InsertList(LISTNODEPTR *cptr, int info)
{
    LISTNODEPTR listptr;  // not to confuse with listptr in main()

    listptr = (LISTNODEPTR)malloc(sizeof(LISTNODE));
    listptr->data = info;  // set data
    listptr->nextptr = NULL;  // initialize link

    if ( ! *cptr )  // first node
        *cptr = listptr;
    else  // link list not empty; add new node
    {
        (*cptr)->nextptr = listptr;  // link new node
        *cptr = listptr;  // set new node to current
    }

    return;
}

int printList(LISTNODEPTR listptr)  // all you need is head node
{
    LISTNODEPTR ptr = listptr;

    while ( ptr )
    {
        printf( "%d\n", ptr->data );
        ptr = ptr->nextptr;  // advance to next node
    }

    return 0;
}

int sumList(LISTNODEPTR)
{
    // implement this yourself
    return 0;
}

int averageList(LISTNODEPTR)
{
    // implement this yourself
    return 0;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.