So far Ive gotten this far: Can anyone help me one like the insert function and tell me if im even close to being right on this project? IM in desperate need!!!
Code:
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
struct record
{
char name[50];
char ssn[12];
char age[4];
char occupation[50];
struct record *next;
struct record *prior;
};
struct record *start; /* pointer to first entry in list */
struct record *last; /* pointer to last entry */
struct record *find(char *);
Class LL {
private:
record *head;
record *tail;
public:
LL();
int insert (record *);
record *search(char[]);
int delete_item(char[]);
void traverse[];
};
LL:: ( LL )
{
head = NULL;
tail = NULL;
};
int LL:insert( )
void LL:traverse( )
void LL: delete_item(struct record **start, struct record **last)
{
struct record *info;
char s[80];
inputs("Enter name: ", s, 30);
info = find(s);
if(info) {
if(*start==info) {
*start=info->next;
if(*start) (*start)->prior = NULL;
else *last = NULL;
}
else {
info->prior->next = info->next;
if(info!=*last)
info->next->prior = info->prior;
else
*last = info->prior;
}
free(info); /* return memory to system */
}
}
/* Look for a name in the list. */
void search(void)
{
char name[50];
struct record *info;
printf("Enter name to find: ");
gets(name);
info = find(name);
if(!info) printf("Not Found\n");
else display(info);
}
/* Load the record file. */
void load()
{
struct record *info;
FILE *fp;
fp = fopen("assign6", "rb");
if(!fp) {
printf("Cannot open file.\n");
exit(1);
}
/* free any previously allocated memory */
while(start) {
info = start->next;
free(info);
start = info;
}
/* reset top and bottom pointers */
start = last = NULL;
printf("\nLoading File\n");
while(!feof(fp)) {
info = (struct record *) malloc(sizeof(struct record));
if(!info) {
printf("Out of Memory");
return;
}
if(1 != fread(info, sizeof(struct record), 1, fp)) break;
dls_store(info, &start, &last);
}
fclose(fp);
}
/* Save the file to disk. */
void save(void)
{
struct record *info;
FILE *fp;
fp = fopen("assign6", "wb");
if(!fp) {
printf("Cannot open file.\n");
exit(1);
}
printf("\nSaving File\n");
info = start;
while(info) {
fwrite(info, sizeof(struct record), 1, fp);
info = info->next; /* get next address */
}
fclose(fp);
}
int main(int argc, char* argv[])
{
start = last = NULL; /* initialize start and end pointers */
for(; {
switch(menu()) {
case 1: display();
break;
case 2: add_rec();
break;
case 3: DeleteNode();
break;
case 4: exit(0);
}
}
return 0;
}
int menu ()
{
int item;
// display menu and return output
cout << "===============================" << endl;
cout << " Please Select An Option " << endl << endl;
cout << " 1. Display a record" << endl;
cout << " 2. Add a record" << endl;
cout << " 3. delete a record" << endl;
cout << " 4. Exit" << endl;
cin >> item;
cin.ignore ();
// return item
return item;
}