junior48012
2008-09-17 15:32:08 UTC
Im pretty much writing a program that takes in la numerical value as well as a string of characters. I don't know what i am doing wrong as when i select the option 1 and enter a value then the string the program crashes.
The main problem im having is that i am trying to pass the numerical value as well as the string into the function to be computed and added to whatever node it needs to be done to. However For some reason its not working.
Any help appreciated.
Please and Thank you
So i have
#include
#include
// Struct used to form a linked list of integers.
struct ll {
int data;
char make;
int year;
struct ll *next;
};
void print(struct ll *front);
struct ll* insert(struct ll *front, int num, char make);
struct ll* delete(struct ll *front, int num);
void dellist(struct ll *p);
int search(struct ll *front, int val);
int menu();
int main() {
// Create a list to test.
struct ll *mylist = NULL;
int ans, num, year;
char model, color, tag, make;
// Loop through the options until the user quits.
ans = menu();
while (ans != 5) {
// Insert a node.
if (ans == 1) {
printf("Which value would you like to insert?\n");
scanf("%d", &num);
printf("What is the make of the vehicle?\n");
scanf("%s", make);
printf("%s is the make\n", make);
mylist = insert(mylist, num, make);
}
// Delete a node.
if (ans == 2) {
printf("Which value would you like to delete?\n");
scanf("%d", &num);
mylist = delete(mylist, num);
}
// Search for a value.
if (ans == 3) {
printf("Which value would you like to find?\n");
scanf("%d", &num);
// Print out the appropriate message.
if (search(mylist, num))
printf("%d is in the tree.\n", num);
else
printf("%d is not in the tree.\n", num);
}
// Print out the list.
if (ans==4) {
printf("Here are the values in the list: ");
print(mylist);
}
ans = menu(); // Prompt the user for their choices.
}
dellist(mylist); // Free the memory used for the list.
system("PAUSE");
return 0;
}
// Pre-condition: front points to the head of a linked list structure.
// Post-condition: each value in the list will be printed, in order.
void print(struct ll *front) {
while (front !=NULL) {
printf("%d ", front->data);
printf("%s ", front->make);
front = front -> next;
}
printf("\n");
}
// Pre-condition: front points to the head of a linked list structure and
// and num is the value to be inserted into that list.
// Post-condition: num will be inserted into the list pointed to by front,
// in numerical order and a pointer to the front of the
// adjusted list will be returned.
struct ll* insert(struct ll *front, int num, char make) {
struct ll *iter;
// Create the new node to add.
struct ll* temp = (struct ll*)malloc(sizeof(struct ll));
temp->data = num;
temp->make = make;
temp->next = NULL;
// Inserting into an empty list.
if (front == NULL)
return temp;
// Inserted element goes at the front of the list.
if (temp->data < front->data) {
temp->next = front;
return temp;
}
// Iterate iter to point to the node BEFORE where the node to insert
// should go.
iter = front;
while (iter->next != NULL && temp->data > iter->next->data)
iter = iter->next;
temp->next = iter->next; // Attach the new node's next pointer.
iter->next = temp; // Patch the previous node to the new node.
return front;
}
// Pre-condition: front points to the head of a linked list structure.
// Post-condition: if val is stored in the list, 1 will be returned,
// otherwise 0 will be.
int search(struct ll *front, int val) {
while (front != NULL) {
if (front->data == val)
return 1;
front = front->next;
}
return 0;
}
// Pre-condition: front points to the head of a linked list structure.
// Post-condition: The first node that contains the value num will be
// removed from the list. A pointer to the front of the
// list will be returned. If no such value is stored,
// the list will remain unchanged.
struct ll* delete(struct ll *front, int num) {
struct ll *temp, *del;
temp = front;
// Only need to delete if the list is not null.
if (temp != NULL) {
// Take care of the case where first node needs to be deleted.
if (temp->data == num) {
del = temp -> next;
free(temp);
return del;
}
// Otherwise, loop until you find the node to delete and do so.
while (temp->next != NULL) {
if (temp ->next->data == num) {
del = temp -> next;
temp->next = temp ->