#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;
}
}
}
vicsong93
0
Newbie Poster
Recommended Answers
Jump to PostI 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 …
All 2 Replies
Reply to this topic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.