here is the correct solution:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char code[25]; // course code
}course_t;
typedef struct node_s
{
course_t item; // data
struct node_s *next; // pointer to the next node
} node_t;
// Pointer to a node:
typedef node_t* nodeptr;
/*function prototypes*/
nodeptr getnode(void);
void add_begin(nodeptr *head, course_t item);
void add_end(nodeptr *head, course_t item );
int del_data(nodeptr *head, char course_code[]);
void instructions (void);
void PrintList (nodeptr * ptr);
/*************************************************************************************/
void instructions (void)
{
printf("Enter you choice:\n"
" 1 - to insert an element into the beginning of the list\n"
" 1 - to insert an element into the beginning of the list\n"
" 1 - to delete an element you specify\n"
" 1 - to print the list\n"
" 1 - exit\n");
}
//----------------------------------------------------------
// Getting a new node: allocates memory for a new node
//----------------------------------------------------------
nodeptr getnode(void)
{
nodeptr ptr;
ptr = (node_t*) malloc(sizeof(node_t)); // Allocate memory
return(ptr); // Return pointer to allocated memory
} // end getnode
//----------------------------------------------------------
// Adding a node to the beginning of a list.
//----------------------------------------------------------
void add_begin(nodeptr *head, // head of the list
course_t item) // data of the list
{
nodeptr newptr; // pointer for a new node
newptr = getnode(); // get a new node
newptr->item = item; // store data into new node
newptr->next = *head; // newnode will point to whatever head was pointing
*head = newptr; // new node will be the head
} // end add_begin
//----------------------------------------------------------
// Adding a node to the end of a list:
//----------------------------------------------------------
void add_end(nodeptr *head, // head of the list
course_t item ) // data of the list
{
nodeptr newptr, ptr;
newptr = getnode(); // Get a new node
newptr->item = item; // Store data into new node
newptr->next = NULL; // Set next of new node to NULL
if (*head == NULL)
{ // Linked list was empty
*head = newptr; // add to beginning
}
else
{ // Linked List was not empty. Find last node.
ptr = *head; // point to beginning of list
while(ptr->next != NULL) // while it is not the last node,
{ ptr = ptr->next; } // pass to the next node
ptr->next = newptr; // old last node is followed by new node
}
} // end add_end
//----------------------------------------------------------
// Delete a node with a given data
// Returns 1 if a deletion is performed, 0 if no deletion is performed.
//----------------------------------------------------------
int del_data(nodeptr *head, // head of the list
char course_code[]) // course code to be deleted
{
nodeptr ptr, // Ptr to current node
prevptr = NULL; // Ptr to previous node
int ret_val = 0; // Value to be returned.
if(*head != NULL) // If list is not empty, then:
{
ptr = *head; // point to beginning of list
// Traverse list while data not found and not end of list
while(strcmp(course_code, ptr->item.code) != 0 &&
ptr->next != NULL)
{
prevptr = ptr; // Store pointer to previous node
ptr = ptr->next; // Pass to the next node
}
// Check if data is found:
if(strcmp(course_code, ptr->item.code) == 0)
{
ret_val = 1;
if(ptr == *head)
{ // If found node is the first node, then:
del_begin(head); // Delete the first node
}
else
{ // Now, prevptr points to prev. node, and ptr points to found node
prevptr->next = ptr->next; // Set next of previous node
free(ptr); // Free the deleted node
}
}
}
return (ret_val);
} // end del_data
/*************************************************************************************/
void PrintList (nodeptr * ptr) {
if (ptr == NULL) {
printf("List is empty!\n");
}
else {
while (ptr != NULL) {
printf("%d\t", ptr -> item);
ptr = ptr -> *next;
} /* end while */
printf("\n");
} /* end else */
}
/*************************************************************************************/
int main(void) {
nodeptr startPtr = NULL;
int choice;
char item;
instructions (); /*display menu*/
printf("? ");
scanf("%d" , &choice );
/* loop while user does not choose 5 */
while ( choice != 5 ) {
switch ( choice ) {
case 1:
printf( "Enter a character: ");
scanf( "\n%c", &item );
add_begin( &startPtr, item ); /* insert item into list */
break;
case 2:
printf( "Enter a character: ");
scanf( "\n%c", &item );
add_end( &startPtr, item ); /* insert item into list */
break;
case 3:
printf( "Enter a character: ");
scanf( "\n%c", &item );
add_begin( &startPtr, item ); /* insert item into list */
break;
case 4:
printf( "Enter a character: ");
scanf( "\n%c", &item );
add_begin( &startPtr, item ); /* insert item into list */
break;
case 5:
printf( "Enter a character: ");
scanf( "\n%c", &item );
add_begin( &startPtr, item ); /* insert item into list */
break;
return 0;
}/*end switch*/
}/* end while */
} /* end main */