#include
using namespace std;
struct node{
int data;
node *next,*prev;
};
node *newnode;
node *current;
void insert_node_start(){
node *newnode;
newnode = new node;
cout << "Data: ";
cin >> newnode->data;
newnode->next = NULL;
newnode->prev = NULL;
if(current == NULL){
current = newnode;
current->next = NULL;
current->prev = NULL;
current = newnode;
}else{
newnode->next = current;
current->prev = newnode;
current = newnode;
}
cout << "Data was include\n";
}
void insert_node_end(){
node *newnode;
newnode = new node;
cout << "Data: ";
cin >> newnode->data;
newnode->next = NULL;
newnode->prev = NULL;
if(current == NULL){
current = newnode;
current->next = NULL;
current->prev = NULL;
current = newnode;
}else{
node *temp = current;
while (temp->next != NULL) temp = temp->next;
node *temp2;
temp2 = new node;
temp2->data = newnode->data;
temp2->next = NULL;
temp2->prev = temp;
temp->next = temp2;
}
cout<<"Data was Include\n";
}
void show_all(){
node *bantu;
bantu = current;
if(current != NULL){
while(bantu!=current->prev){
cout << bantu->data << " ";
bantu = bantu->next;
}
cout << "\n";
}else{
cout << "Data is Empty";
}
}
void del_start(){
node *hapus;
int n;
if (current != NULL){
if(current->next != NULL){
hapus = current;
n = hapus->data;
current = current->next;
current->prev = NULL;
delete hapus;
}else{
n = current->data;
current = NULL;
}
cout << n << " was deleted\n";
}else{
cout << "is Empty";
}
}
void del_end(){
node *hapus;
node *temp,*temp2;
int n;
if (current != NULL){
if(current->next != NULL){
hapus = current;
n = hapus->data;
current = current->next;
current->prev = NULL;
delete hapus;
}else{
temp = current;
while (temp->prev!=NULL){
temp2 = temp;
temp = temp->next;
}
delete temp;
temp2->next=NULL;
}
cout << n << " was deleted\n";
}else{
cout << "is Empty\n";
}
}
void move_current_forward(){
if(current->next==NULL){
cout << "Current pointing to last node.\n";
}else{
current=current->next;
}
}
void move_current_backward(){
if (current->prev==NULL){
cout << "Current pointing to start node.\n";
}else{
current=current->prev;
}
}
void add_after_curr(){
node *newnode,*newnode2;
newnode=new node;
cout << "Data: ";
cin >> newnode->data;
if (current->next==NULL){
current->next=newnode;
newnode->next=NULL;
}else{
newnode2=current->next;
current->next=newnode;
newnode->next=newnode2;
}
}
void remove_after_curr(){
node *data;
if(current->next==NULL){
cout << "Current pointing to last node.\n";
}else{
data=current->next;
current->next=data->next;
delete data;
}
}
void change_data_curr(){
cout << "Data: ";
cin >> current->data;
}
void show_current(){
cout << "\nData: " << current->data << "\n";
}
int main()
{
int choose;
cout << "|==================================|" <<endl;
cout << "| Double Link List |" <<endl;
cout << "|==================================|" <<endl;
do
{
cout << "1. Exit program.\n";
cout << "2. Add node at the start list.\n";
cout << "3. Add node at the end list.\n";
cout << "4. Show all data in the list.\n";
cout << "5. Remove node at the start list.\n";
cout << "6. Remove node at the end list.\n";
cout << "7. Move current pointer forward 1 node.\n";
cout << "8. Move current pointer backward 1 node.\n";
cout << "9. Add after current.\n";
cout << "10. Remove after curent.\n";
cout << "11. Change current.\n";
cout << "12. Show current.\n";
cout << "Choose the option using index number: ";
cin >> choose;
switch (choose)
{
case 2:insert_node_start();break;
case 3:insert_node_end();break;
case 4:show_all();break;
case 5:del_start();break;
case 6:del_end();break;
case 7:move_current_forward();break;
case 8:move_current_backward();break;
case 9:add_after_curr();break;
case 10:remove_after_curr();break;
case 11:change_data_curr();break;
case 12:show_current();break;
}
}
while (choose!=1);
}
0 comments:
Posting Komentar