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 147 148
|
#include "stdafx.h"
#define MAX 5
typedef struct List {
int L[MAX];
int lastCount;
} List;
List L;
int menu();
void insertData();
void deleteData();
void displayData();
int isEmpty();
int isFull();
int locateData();
void makeNull();
void save();
void load();
void exit();
int main(){
int x, opt;
load();
while(opt!=4) {
opt = menu();
switch(opt){
case 1:
printf("Insert data");
scanf("%d", &x);
insertData(x);
break;
case 2:
printf("Delete data");
scanf("%d", &x);
deleteData(x);
break;
case 3:
displayData();
break;
case 4:
save();
return 0;
default:
printf("Invalid option.");
break;
}
return 0;
}
}
int menu(){
int opt;
int i;
char *n[] = {"Insert data", "Delete data", "Display data", "Display data", "Exit"};
printf("Menu");
for(i=0; i<=4; i++){
printf("%d - %s", i+1, n[i]);
}
printf("Input option: ");
scanf("%d", &opt);
return(opt);
}
void makeNull(){
L.lastCount = -1;
}
int isEmpty(){
return(L.lastCount == MAX-1);
}
int locateData(int x){
int i;
for(i=0; i<=L.lastCount; i++){
return(i);
}
return (-1);
}
void insertData(int x){
if(isFull()){
printf("List full.");
} else {
L.lastCount++;
L.L[lastCount] = x;
}
}
void deleteData(int x){
int i, p;
if(isEmpty()){
printf("List empty.");
} else {
p = locateData(x);
if(p=0){
printf("Not found.");
} else {
for(i=p; i<=L.lastCount; i++){
L.L[i] = L.L[i++];
}
L.lastCount--;
}
}
}
void displayData(){
int i;
for(i=0; i<=L.lastCount; i++){
printf("%d", L.L[i]);
}
}
void save(){
int i;
FILE *fp;
fp = fopen("file.dbf", "w+");
for(i=0; i<=L.lastCount; i++){
fprintf(fp, "%d \n", L.L[i]);
}
fclose(fp);
}
void load(){
int y;
FILE *fp;
makeNull();
fp = fopen("file.dbf", "r+");
while(!feof(fp)){
fscanf(fp, "%d \n", &y);
insertData(y);
}
fclose(fp);
}
|