timeforsleep
asked on
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*/
}
#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*/
}
ASKER
Edited text of question.
ASKER
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.
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;
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...