#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct account {
    char AccName[100000];
    int Age;
    float AccBalance;
    struct account *Next;
};

typedef struct account BankAcc;

void init(struct Account** headOfList);
void insert(struct account **headOfList, char *name, int age, double balance);
void remove(struct account **headOfList, char *name );
void print(struct account *headOfList );


void main() {
    BankAcc *startPtr = NULL;

    int selection;
    printf("Enter selection and following below: \n");
    printf("1. Insert Account \n2. Delete Account \n3. Print All\n");
    printf("Enter your selection : ");
    scanf("%d",&selection);

    while(selection != 5) {
        switch(selection) {
            case 1:
                char *Username;
                char TmpName[10000];
                int UserAge;
                float UserBalance;



                // Enter Account Name
                printf("Enter Account Name : ");
                scanf("%s",&TmpName);
                Username = TmpName;
                // Enter Age
                printf("Enter Age : ");
                scanf("%d",&UserAge);
                // Enter Account Balance
                printf("Enter Account Balance( RM ) :  ");
                scanf("%f",&UserBalance);

                insert(&startPtr,Username,UserAge,UserBalance);
            break;

            case 2:
                char *del;
                char tmpdelete[100000];

                printf("Enter Account Name : ");
                scanf("%s",&tmpdelete);
                del = tmpdelete;
                remove(&startPtr,del);
            break;

            case 3:
                print(startPtr);
            break;

        }

        printf("------------------------------------------------\n");
        printf("Enter selection and following below: \n");
        printf("1. Insert Account \n2. Delete Account \n3. Print All \n");
        printf("Enter your selection : ");
        scanf("%d",&selection);
    }

}
void init(struct Account** headOfList){
    headOfList = NULL;
}

void insert(struct account **headOfList, char *name, int age, double balance) {
    struct account *newPtr;
    struct account *previousPtr;
    struct account *currentPtr;

    newPtr = (account*) malloc(sizeof(account));

    if(newPtr != NULL) {

        //newPtr->AccName = (char *)malloc(40 * sizeof(char));
        strcpy (newPtr->AccName,name);

        newPtr->Age = age;
        newPtr->AccBalance = balance;
        newPtr->Next = NULL;


        previousPtr = NULL;
        currentPtr = *headOfList;



        while(currentPtr != NULL && balance < currentPtr->AccBalance) {
            previousPtr = currentPtr;
            currentPtr = currentPtr->Next;
        }

        if(previousPtr == NULL) {
            newPtr->Next = *headOfList;
            *headOfList = newPtr;
        } else {
            previousPtr->Next = newPtr;
            newPtr->Next = currentPtr;
        }
    }

    //printf("%s\n",headOfList->AccName);

}


void remove(struct account **headOfList, char *name ){

    struct account *previousPtr;
    struct account *currentPtr;
    struct account *tempPtr;


    if ( name == (*headOfList)->AccName ) { 
        tempPtr = (*headOfList);
        (*headOfList) = (*headOfList)->Next;
        free( tempPtr );

    }
    else {
        previousPtr = (*headOfList);
        currentPtr = (*headOfList)->Next;

        while (currentPtr->Next != NULL && currentPtr->AccName != name ) {
            previousPtr = currentPtr;
            currentPtr = currentPtr;  
        }
        if ( currentPtr->Next != NULL) {
            tempPtr = currentPtr;
            previousPtr->Next = currentPtr;
            previousPtr->Next = currentPtr;
            free( tempPtr );
        }
        else {
            tempPtr = currentPtr;
            previousPtr->Next = NULL;
            free( tempPtr );
        }
    }

}

void print(struct account *headOfList ) {
    if(headOfList == NULL) {
        printf("List is empty. \n");
    } else {
        printf("Account Name\tAge\tAccount Balance( RM )\n");
        printf("________________________________________________\n");

        while(headOfList != NULL) {
            printf("  %s  \t%d\t\t%2.2f\n",headOfList->AccName,headOfList->Age,headOfList->AccBalance);
            headOfList = headOfList->Next;
        }
    }
}

Edited 4 Years Ago by vicsong93: error

I agree with marh; from the look of it, the line should read

        currentPtr = currentPtr -> Next;

A few other comments:

  • Is this C++ code, or C code? While this code should work under most C++ compilers, the languages are not at all the same. This seems to be C rather than C++; for example, you use only the C-style I/O functions, and the headers (<stdlib.h> etc.) you are using are the C style headers, as opposed to the newer C++ headers (<cstdlib> and so forth). Likewise, the declarations of the structure variables are C-style; in C++, you don't need to explicitly have the struct keyword in the variable declarations.

  • What compiler and operating system are you writing this for? If this is meant to be C++, you may be using a much older compiler, which would explain some of what was noted above.

  • You should never use void main(); while the older C standard allows it on non-standard systems (embedded OSes and the like, where there may not be a shell to return to), it is considered non-standard and isn't portable. It is not allowable in C++, and the latest C standard eliminates it IIRC. Unless the specific platform you are on requires something different, you should always use int main() in both C and C++.

I hope this helps.

Edited 4 Years Ago by Schol-R-LEA

This article has been dead for over six months. Start a new discussion instead.