My code compiles perfectly when I want to insert, display and delete using linked lists. However I have added a function "sum" which is supposed to add up the integer values stored in the linked lists but it is not working. Please tell me what I am doing wrong. I have commented out the sum function but you can see it. Thanks
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
} NODE;
typedef NODE *NODEptr;
void insert (NODEptr *sPtr, int value);// prototype
void delete (NODEptr *sPtr, int value);// prototype
void printList (NODEptr currentPtr);
//void sumList (NODEptr sPtr);
int main(void)
{
NODEptr startPtr = NULL; /* initially there are no nodes */
int item; /* character entered by user */
int choice; /* user's choice */
printf("Enter your choice: 1 insert; 2 delete; 3 exit\n");
printf("? ");
scanf("%d", &choice);
while (choice != 3) {
if (choice == 1) {
printf ("Enter a number to insert: ");
scanf("\n%d", &item);
if(item <= 0){
fprintf(stderr,"\nImpossible resistor value\n");}
else{
insert (&startPtr, item);
printList (startPtr);
// sum(startPtr);
}
}
else if (choice == 2) {
if (startPtr == NULL) {
printf("List is empty.\n\n");
}
else {
//printList (startPtr);
printf("Enter a character to delete: ");
scanf("\n%d", &item);
delete (&startPtr, item);
printList (startPtr);
}
}
else {
printf("Invalid Choice.\n\n");
}
printf("Enter your choice: 1 insert; 2 delete; 3 exit\n");
printf("? ");
scanf("%d", &choice);
}
return 0;
}
void printList (NODEptr currentPtr)
{
/* if list is empty */
if (currentPtr == NULL) {
printf("List is empty. \n\n");
}
else {
printf("The list is: \n");
while (currentPtr != NULL) {
printf ("%d --> ", currentPtr -> data );
currentPtr = currentPtr -> next;
}
printf("NULL\n\n");
}
}
void insert (NODEptr *sPtr, int value)
{
NODEptr newPtr; /* pointer to new node */
NODEptr previousPtr; /* pointer to previous node in list */
NODEptr currentPtr; /* pointer to current node in list */
newPtr = (NODEptr) malloc (sizeof(NODE)); /* create a new node */
if (newPtr == NULL) {
fprintf(stderr, "No memory available. Insertion failed. \n");
return;
}
newPtr -> data = value;
newPtr -> next = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
/*loop to find the current location in the list */
while (currentPtr != NULL && currentPtr->data < value ) {
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
/* insert new node at beginning of list */
if (previousPtr == NULL) {
newPtr->next = *sPtr;
*sPtr = newPtr;
}
else { /*insert new node between previousPtr and currentPtr */
previousPtr->next = newPtr;
newPtr->next = currentPtr;
}
}
void delete (NODEptr *sPtr, int value)
{
NODEptr tempPtr; /* temporary node pointer */
NODEptr previousPtr; /* pointer to previous node in list */
NODEptr currentPtr; /* pointer to current node in list */
/* delete the first node */
if (value == (*sPtr) -> data) {
tempPtr = *sPtr;
*sPtr = (*sPtr) -> next; /*delete the node */
free (tempPtr); /* free the delete node */
}
else {
previousPtr = *sPtr;
currentPtr = (*sPtr) ->next;
/*loop to find the correct location in the list */
while (currentPtr != NULL && currentPtr -> data != value ) {
previousPtr = currentPtr;
currentPtr = currentPtr -> next;
}
if (currentPtr == NULL) {
printf( "%d not found.\n", value);
return;
}
/*delete node at currentPtr */
tempPtr = currentPtr;
previousPtr -> next = currentPtr -> next;
free (tempPtr);
}
}
/*void sumList (NODEptr currentPtr)
{
int sum =0;
if (currentPtr == NULL) {
sum = sum + currentPtr;
currentPtr = currentPtr ->next;
}
printf("the sum is %d\n", sum);
}
*/