C++ : I need help with this assignment on LINKED LIST. The list should be create
ID: 3865816 • Letter: C
Question
C++ : I need help with this assignment on LINKED LIST. The list should be created using a struct in C++
You will des gn an antire contods lidt bo leap trock a romes ond plor a, Delino a class con odluk dra can stone a none and rack a names ond ohors nume upto 3 phane numbes Cus on awoy Use construtor auto the phane numbu nhaic the mamber variablas d Add a nes contah Ask dhe usen to ontor the rama and upho 3 phone numben i) Delate a contod by ) Seandn the lat by nome and punt the nome & phane numbers having dhe use enter e nome on ht one peson. proghom shauls have a manu ike ts ond t Add contach s. GuExplanation / Answer
The code for the above description is as follows:
PhneBuk.c
#include "iostream.h"
#include "conio.h"
#include "string.h"
#include "iomanip.h"
#include "fstream.h"
#include "process.h"
const int RIGHT = 1;
const int WRONG = 0;
struct phneBuk
{
char nme[15],address[15],phne[12];
};
struct LList
{
struct phneBuk phn;
struct LList *nxt;
};
typedef struct LList node;
node *front = NULL,*n;
void init(node *NOB)
{
cout << " Name : ";
cin >> NOB -> phn.nme;
cout << " Address : ";
cin >> NOB -> phn.address;
cout << " Phone No. : ";
cin >> NOB -> phn.phne;
}
int khaali(node *n)
{
if(strcmp(n -> phn.nme,"") == 0 && strcmp(n -> phn.address,"") == 0 && strcmp(n -> phn.nme,"") == 0)
return 1;
else
return 0;
}
void cr()
{
n = new node;
strcpy(n -> phn.nme,"");
strcpy(n -> phn.address,"");
strcpy(n -> phn.phne,"");
n -> nxt = NULL;
if(front == NULL)
front = n;
}
int ins()
{
ofstream of;
of.open("d:\Dbase.txt",ios::app);
if(of.fail())
{
cout << "Error : Couldn't open file.";
getch();
exit(0);
}
if(front == NULL)
cr();
if(front -> nxt == NULL)
if(khaali(front))
{
init(n);
of.write((char *) &front,sizeof(front));
return 1;
}
char ntmp[15];
cr();
init(n);
strcpy(ntmp,n -> phn.nme);
node *tempor = front,*location = front;
if(strcmpi(ntmp,front -> phn.nme) < 0)
{
n -> nxt = front;
front = n;
}
else
{
while(tempor -> nxt != NULL)
{
tempor = tempor -> nxt;
if(strcmpi(ntmp,tempor -> phn.nme) > 0)
location = tempor;
}
n -> nxt = location -> nxt;
location -> nxt = n;
}
return 1;
}
void find(char *n)
{
node *tempor = front;
int searched = WRONG;
if(front == NULL)
searched = WRONG;
else if(strcmp(n,tempor -> phn.nme) == 0)
searched = RIGHT;
else
{
while(tempor -> nxt != NULL)
{
tempor = tempor -> nxt;
if(strcmp(n,tempor -> phn.nme) == 0)
{
searched = RIGHT;
break;
}
}
}
if(searched)
{
cout << " Name : " << tempor -> phn.nme;
cout << " Address : " << tempor -> phn.address;
cout << " Phone NO. : " << tempor -> phn.phne;
}
else
{
cout << " " << setw(44) << "Data not searched";
cout << " " << setw(53) << "Press any key to continue...";
}
}
int rem(char *n)
{
if(front == NULL)
return 0;
node *tempor = front,*location = front;
if(strcmp(n,tempor -> phn.nme) == 0)
{
if(tempor -> nxt == NULL)
{
front = NULL;
delete tempor;
}
else
{
front = tempor -> nxt;
delete tempor;
}
return 1;
}
else
{
while(tempor -> nxt != NULL)
{
tempor = tempor -> nxt;
if(tempor == NULL)
return 0;
if(strcmpi(n,tempor -> phn.nme) == 0)
{
if(tempor -> nxt != NULL)
{
tempor = location -> nxt;
location -> nxt = tempor -> nxt;
delete tempor;
}
else
location -> nxt = NULL;
return 1;
}
location = location -> nxt;
}
}
return 0;
}
int alter(char *n)
{
node *tempor = front;
if(front == NULL)
return 0;
if(strcmpi(n,tempor -> phn.nme) == 0)
{
cout << " ***** Previous Data *****";
cout << " Name : " << tempor -> phn.nme;
cout << " Address : " << tempor -> phn.address;
cout << " Phone NO. : " << tempor -> phn.phne;
cout << " ***** Enter n Data ***** ";
init(front);
return 1;
}
else
{
while(tempor -> nxt != NULL)
{
tempor = tempor -> nxt;
if(tempor == NULL)
return 0;
if(strcmpi(n,tempor -> phn.nme) == 0)
{
cout << " ***** Previous Data *****";
cout << " Name : " << tempor -> phn.nme;
cout << " Address : " << tempor -> phn.address;
cout << " Phone NO. : " << tempor -> phn.phne;
cout << " ***** Enter n Data ***** ";
init(tempor);
return 1;
}
}
}
return 0;
}
void printing()
{
node *tempor = front;
if(front == NULL)
{
cout << " " << setw(46) << "Database is khaali";
cout << " " << setw(53) << "Press any key to continue...";
return;
}
if(!khaali(front))
{
cout << " Name : " << tempor -> phn.nme;
cout << " Address : " << tempor -> phn.address;
cout << " Phone NO. : " << tempor -> phn.phne << endl << endl;
}
while(tempor -> nxt != NULL)
{
tempor = tempor -> nxt;
cout << " Name : " << tempor -> phn.nme;
cout << " Address : " << tempor -> phn.address;
cout << " Phone NO. : " << tempor -> phn.phne << endl;
}
}
int dispMenu()
{
cout << " **********************************" << endl;
cout << " * ---------------------- *" << endl;
cout << " * TELEPHONE DIARY *" << endl;
cout << " * ---------------------- *" << endl;
cout << " **********************************" << endl << endl;
cout << " -----------------------------------" << endl;
cout << " 1. Add ";
cout << " 2. Search ";
cout << " 3. Delete ";
cout << " 4. Modify ";
cout << " 5. View ";
cout << " 0. Exit ";
cout << " -----------------------------------" << endl << endl;
int ch;
cout << setw(45) << "Enter Choice : ";
cin >> ch;
return ch;
}
int main()
{
char nme[15];
do
{
clrscr();
int ch = dispMenu();
int jeet;
cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ";
switch(ch)
{
case 1:
jeet = ins();
if(jeet)
cout << " " << setw(51) << "Data Inserted Successfully";
cout << " " << setw(53) << "Press any key to continue...";
break;
case 2:
cout << " Enter Name : ";
cin >> nme;
find(nme);
break;
case 3:
cout << " Enter Name : ";
cin >> nme;
jeet = rem(nme);
if(jeet)
cout << " " << setw(50) << "Data Deleted Successfully";
else
cout << " " << setw(48) << "Data Doesn't Exists";
cout << " " << setw(53) << "Press any key to continue...";
break;
case 4:
cout << " Enter Name : ";
cin >> nme;
jeet = alter(nme);
if(jeet)
cout << " " << setw(50) << "Data Modified Successfully";
else
cout << " " << setw(48) << "Data Doesn't Exists";
cout << " " << setw(53) << "Press any key to continue...";
break;
case 5:
printing();
break;
case 0:
clrscr();
cout << " ";
cout << setw(46) << "TELEPHONE DIARY ";
cout << setw(47) << "(Using Linked List)";
cout << " " << setw(51) << "A Program by Ankit Pokhrel";
cout << " " << setw(51) << "Press any key to halt...";
getch();
return 0;
default:
cout << endl << setw(58) << "Please select appropriate option ";
cout << setw(55) << "Press any key to continue...";
}
getch();
}while(1);
}
Please rate the answer if it helped.......Thankyou
Hope it helps.....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.