This program is (as you can see in the notation) a homework assignment I'm doing for class. I wrote the whole thing, which as far as I can tell right now is logically functional. It takes commands from a user and uses them to denote what happens to a linked list (adding, removing, counting nodes ect.). This code compiles perfectly fine on both my OS X machine with gcc 4.something and on my schools UNIX cluster (i think they're running solaris of some sort and gcc 2.95.something) and the -Wall argument doesn't give any errors either. I get an error after one or two inputs usually that says Bus Error (Core Dump) and I have no idea where it is coming from, i checked my pointers and they all seem to be straight to me. If anyone can just give it an extra set of eyes and tell me if you see anything that could be causing this is would be greatly appreciated. Thank you.
/* Adam Ross Cohen -- 000891451 -- CSI 333 Fall 2009 Program 2 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* create a struct for the nodes of the linked list */
struct node
{
/* each node holds an int value and the pointer to the next node */
int value;
struct node *next;
};
int main(void)
{
/* Prototypes for all functions */
void insert(struct node**, int);
void delete(struct node**, int);
void print(struct node**);
void count(struct node**);
void count_dv(struct node**);
void count_occ(struct node**, int);
void count_r(struct node**, int, int);
/* initialize the linked list*/
struct node *head;
/* int holders for taking in values */
int val1, val2;
/* string holder for the commands */
char cmd;
/* set the list to null for starters */
head = NULL;
/* prompt the user for the first input */
printf("command?");
/* read in the initial input */
scanf("%s %d %d", &cmd, &val1, &val2);
/* check to see if the command ends, if not go the the proper function */
while(strcmp(&cmd, "end") != 0)
{
if(strcmp(&cmd, "ins" ) != 0)
{
insert(&head, val1);
}
else if(strcmp(&cmd, "del") != 0)
{
delete(&head, val1);
}
else if(strcmp(&cmd, "pri") != 0)
{
print(&head);
}
else if(strcmp(&cmd, "cnt") != 0)
{
count(&head);
}
else if(strcmp(&cmd, "cdv") != 0)
{
count_dv(&head);
}
else if(strcmp(&cmd, "occ") != 0)
{
count_occ(&head, val1);
}
else if(strcmp(&cmd, "ran") != 0)
{
count_r(&head, val1, val2);
}
/* if the input matches no commands it must be invalid, tell the user */
else
{
printf("invalid input");
}
/* prompt the user for the next command and take in its values */
printf("command?");
scanf("%s %d %d", &cmd, &val1, &val2);
}
/* thank the user and return zero to end main */
printf("Thank you, come again.");
return 0;
}
void insert(struct node **h, int v)
{
/*Creates a new node with the int v and inserts it in the
list in non-descending order*/
/* declare temp lists */
struct node *temp, *temp2;
/* make sure the system allocates new memory for another node */
if((temp = (struct node *)malloc(sizeof(struct node))) == NULL)
{
printf("Space allocation failed. \n");
exit(1);
}
/* set the new nodes value */
temp->value = v;
temp->next = NULL;
/* if the list was empty the new node is the whole list */
if(*h == NULL)
{
*h = temp;
}
/* if the list wasnt empty loop through until the value of the
current node is less than the value you're adding, then
make the old list point to the new node and the new node point
back to the next node in the list */
else
{
while((*h)->value < v)
{
temp2 = *h;
*h = (*h)->next;
}
temp2->next = temp;
temp->next = *h;
}
}
void delete(struct node**h, int v)
{
/* removes a node from the list */
/* create a temporary node */
struct node *temp;
/* if the value isnt there tell the user and do nothing */
if((*h)->value > v)
{
printf("That value is not in the list. \n");
}
/* if the first node is the match just skip it */
else if((*h)->value == v)
{
*h = (*h)->next;
}
/* otherwise loop through the list and find where it is, then make
the previous nodes pointer go to the next node in the list */
else
{
while((*h)->value < v)
{
temp = *h;
*h = (*h)->next;
}
temp->next = (*h)->next;
}
}
void print(struct node **h)
{
/* prints the list */
/* if the list is empty tell the user */
if(*h == NULL)
{
printf("The list is empty.");
}
/* if the list isnt empty loop through node by node and print the value
to the prompt */
else
{
while((*h)->next != NULL)
{
printf("%d \n", (*h)->value);
}
}
}
void count(struct node **h)
{
/* counts the number of node in the list */
/* make a counter int and set it to zero */
int i;
i = 0;
/* if the list is empty tell the user */
if(*h == NULL)
{
printf("0 \n");
}
/* otherwise loop through the list and add one to the counter every time
the next node exists */
else
{
while((*h)->next != NULL)
{
i++;
*h = (*h)->next;
}
printf("%d \n", i);
}
}
void count_dv(struct node **h)
{
/* counts how many different values exist in the list */
/* make a temp list and set it to null */
struct node *temp;
temp = NULL;
/* if the list passed in is null tell the user theres
no values */
if(*h == NULL)
{
printf("0 \n");
}
/* otherwise when the next node still exists if the value of
the current node isnt in the temp list put it in, if not
just continue to the next node, the number of nodes in the
temp list is the amount of induvidual distinct values the original
list had */
else
{
while((*h)->next != NULL)
{
if(temp == NULL)
{
insert(&temp, (*h)->value);
}
else
{
while(temp->next != NULL)
{
if(temp->value == (*h)->value)
{
}
else
{
insert(&temp, (*h)->value);
}
}
}
}
}
}
void count_occ(struct node **h, int v)
{
/* counts how many times an induvidual number occurs in the list */
/* create a counter int and set it to zero */
int i;
i = 0;
/* if the list is empty tell the user no values match */
if(*h == NULL)
{
printf("0 \n");
}
/* if it isnt empty then while the next node still exists check if
the current ones value is equal to the match value, if it is
incriment the counter, print the counter when you hit the last node */
else
{
while((*h)->next != NULL)
{
if((*h)->value == v)
{
i++;
}
*h = (*h)->next;
}
printf("%d \n", i);
}
}
void count_r(struct node **h, int a, int b)
{
/* counts existing value within a range from a to b including a and b */
/* create a counter variable and set it to zero */
int i;
i = 0;
/* if the low val is higher than the high val the range doesnt exist */
if(a > b)
{
printf("Invalid Range");
}
/* if the range is valid while the next node exists if the number is >=a or <=b
incriment the counter, print the counter value when you hit the last node */
else
{
while((*h)->next != NULL)
{
if(((*h)->value >= a) && ((*h)->value <= b))
{
i++;
}
*h = (*h)->next;
}
printf("%d \n", i);
}
}