#include <iostream.h> // execute input and outpu process.
#include <cstring.h> // defines several functions to manipulate C strings and arrays.
#include<conio.h>
struct data
{
char name[30];
string id;
data *next; // pointer next=point to next node.
};
// declare operation of list.
void insert();
void display();
data *head; // head is a pointer which will point to first node.
cout << " CHOICE : ";
cin >> p; // enter menu nu.
clrscr();
if (p==1)
{
cout << " ..:: INSERT CUSTOMER DATA ::.. " << endl;
cout << " ================================= " << endl;
insert(); // call function insert.
}
else if (p==2)
{
cout << " ..:: DISPLAY CUSTOMER DATA ::.. " << endl;
cout << " ================================= " << endl;
display(); // call function display.
}
else (p==3)
{
cout << " ..:: EXIT ::.. " << endl;
cout << " ============== " << endl;
}
cout << "\n\n RETURN TO MENU [Y/T] : ";
cin >> respond;
clrscr();
}
}
// function to insert data.
void insert()
{
data *newptr;
data *cur; // create new pointer for struct node.
cur = head; // new value inserted (cur)-nilai head.
newptr = new data;
cin.ignore();
// assign value for node
cout << " INSERT NAME : ";
cin.getline (newptr -> name,30); // new pointer point to new value inserted (name).
newptr -> next = NULL; // new value point to NULL.
cout << " INSERT ID NO : ";
cin >> newptr -> id; // new pointer point to new value inserted (id).
newptr -> next = NULL; // new value point to NULL.
// link and head.
if (head == NULL) // first, if list is empty.
{
head = newptr; // head will point to element which newptr point to.
}
else // or else, insert at front.
{
// move the cur pointer.
while (cur!= NULL && newptr -> name > cur -> name) // when cur is not NULL, new pointer which point to nama
// value will give current pointer to move the nama value.
{
cur = cur -> next; //current value point to next node.
}
// insert at front
newptr -> next = head; //new pointer point to next node and equal to head.
head = newptr; //the value of head is equal to value of new pointer.
}
}
// funtion to display data in the list.
void dislay()
{
data *cur; //declare cur pointer
cur = head; //new value inserted (cur) is equal to value of head
while(cur != NULL) //while current is not NULL, the loop will print value
{
cout << "\n CUSTOMER NAME : " <<cur -> name<< endl; //output
cout << " CUSTOMER ID : " <<cur -> id << endl;
cur = cur -> next; //current value point to next node.
}
}