-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBANK_project_using_linkedList.c
146 lines (129 loc) · 3.45 KB
/
BANK_project_using_linkedList.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Account
{
int accountNumber;
char customerName[100];
double balance;
char accountType[20];
struct Account *next;
} Account;
Account *createAccount(int accountNumber, const char *customerName, double balance, const char *accountType)
{
Account *newAccount = (Account *)malloc(sizeof(Account));
newAccount->accountNumber = accountNumber;
strcpy(newAccount->customerName, customerName);
newAccount->balance = balance;
strcpy(newAccount->accountType, accountType);
newAccount->next = NULL;
return newAccount;
}
void addAccount(Account **bank, int accountNumber, const char *customerName, double balance, const char *accountType)
{
Account *newAccount = createAccount(accountNumber, customerName, balance, accountType);
if (*bank == NULL)
{
*bank = newAccount;
}
else
{
Account *current = *bank;
while (current->next != NULL)
{
current = current->next;
}
current->next = newAccount;
}
printf("Account added successfully.\n");
}
Account *findAccount(Account *bank, int accountNumber)
{
Account *current = bank;
while (current != NULL)
{
if (current->accountNumber == accountNumber)
{
return current;
}
current = current->next;
}
return NULL;
}
void deleteAccount(Account **bank, int accountNumber)
{
Account *current = *bank;
Account *prev = NULL;
while (current != NULL)
{
if (current->accountNumber == accountNumber)
{
if (prev == NULL)
{
*bank = current->next;
}
else
{
prev->next = current->next;
}
free(current);
printf("Account deleted successfully.\n");
return;
}
prev = current;
current = current->next;
}
printf("Account not found.\n");
}
void displayAccounts(Account *bank)
{
if (bank == NULL)
{
printf("No accounts found.\n");
}
else
{
printf("Account List:\n");
Account *current = bank;
while (current != NULL)
{
printf("Account Number: %d\n", current->accountNumber);
printf("Customer Name: %s\n", current->customerName);
printf("Balance: %.2f\n", current->balance);
printf("Account Type: %s\n", current->accountType);
printf("-----------------\n");
current = current->next;
}
}
}
void freeBank(Account *bank)
{
Account *current = bank;
while (current != NULL)
{
Account *temp = current;
current = current->next;
free(temp);
}
}
int main()
{
Account *bank = NULL;
addAccount(&bank, 1001, "John Doe", 5000, "Savings");
addAccount(&bank, 1002, "Jane Smith", 10000, "Current");
addAccount(&bank, 1003, "Bob Johnson", 2000, "Savings");
displayAccounts(bank);
Account *foundAccount = findAccount(bank, 1002);
if (foundAccount != NULL)
{
printf("Account found: %s\n", foundAccount->customerName);
}
else
{
printf("Account not found.\n");
}
deleteAccount(&bank, 1002);
displayAccounts(bank);
freeBank(bank);
return 0;
}