Selection Sort

#include<stdio.h>
#include<conio.h>

int main(){

  int size,i,j,temp,list[100];
 
  printf("Enter the size of the List: ");
  scanf("%d",&size);

  printf("Enter %d integer values: ",size);
  for(i=0; i<size; i++)
      scanf("%d",&list[i]);
 
  //Selection sort logic

  for(i=0; i<size; i++){
      for(j=i+1; j<size; j++){
           if(list[i] > list[j])
       {
               temp=list[i];
               list[i]=list[j];
               list[j]=temp;
           }
      }
  }

  printf("List after sorting is: ");
  for(i=0; i<size; i++)
      printf(" %d",list[i]);

  getch();
}

Insertion Sort

#include<stdio.h>
#include<conio.h>

int main(){

   int size, i, j, temp, list[100];

   printf("Enter the size of the list: ");
   scanf("%d", &size);

   printf("Enter %d integer values: ", size);
   for (i = 0; i < size; i++)
      scanf("%d", &list[i]);
     
   //Insertion sort logic
   for (i = 1; i < size; i++) {
      temp = list[i];
      j = i - 1;
      while ((temp < list[j]) && (j >= 0)) {
         list[j + 1] = list[j];
         j = j - 1;
      }
      list[j + 1] = temp;
   }

   printf("List after Sorting is: ");
   for (i = 0; i < size; i++)
      printf(" %d", list[i]);

  getch();
}

Linier Search

#include<stdio.h>
#include<conio.h>

int main(){
  int list[20],size,i,sElement;

  printf("Enter size of the list: ");
  scanf("%d",&size);

  printf("Enter any %d integer values: ",size);
  for(i = 0; i < size; i++)
    scanf("%d",&list[i]);

  printf("Enter the element to be Search: ");
  scanf("%d",&sElement);
 
  // Linear Search Logic
  for(i = 0; i < size; i++)
  {
     if(sElement == list[i])
     {
        printf("Element is found at %d index", i);
        break;
     }
  }
  if(i == size)
     printf("Given element is not found in the list!!!");
  getch();
}

Binary Search

#include<stdio.h>
#include<conio.h>

int main()
{
   int first, last, middle, size, i, sElement, list[100];
  
   printf("Enter the size of the list: ");
   scanf("%d",&size);

   printf("Enter %d integer values in Assending order\n", size);

   for (i = 0; i < size; i++)
      scanf("%d",&list[i]);

   printf("Enter value to be search: ");
   scanf("%d", &sElement);

   first = 0;
   last = size - 1;
   middle = (first+last)/2;

   while (first <= last) {
      if (list[middle] < sElement)
         first = middle + 1;   
      else if (list[middle] == sElement) {
         printf("Element found at index %d.\n",middle);
         break;
      }
      else
         last = middle - 1;

      middle = (first + last)/2;
   }
   if (first > last)
      printf("Element Not found in the list.");
   getch(); 
}

Double Linked List

#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

int pil; void pilih();
void buat_baru();
void tambah_belakang();
void tambah_depan();
void hapus_belakang();
void hapus_depan();
void tampil();

struct node
{
     char nama [20];
     int umur;
     float tinggi;
     node *prev, *next;
};

node *baru, *head=NULL, *tail=NULL,*hapus,*bantu;

int main()
 {
     do
     {
         system("cls");
         cout<<"MENU DOUBLE LINKEDLIST"<<endl;
         cout<<"1. Tambah Depan"<<endl;
         cout<<"2. Tambah Belakang"<<endl;
         cout<<"3. Hapus Depan"<<endl;
         cout<<"4. Hapus Belakang"<<endl;
         cout<<"5. Tampilkan"<<endl;
         cout<<"6. Selesai"<<endl;
         cout<<"Pilihan Anda : ";
         cin>>pil;
         system("cls");
         pilih();
     }
 while(pil!=6);
 }

void pilih()
 {
     if(pil==1)
         tambah_depan();
     else if(pil==2)
         tambah_belakang();
     else if(pil==3)
         hapus_depan();
     else if(pil==4)
         hapus_belakang();
     else if(pil==5)
         tampil();
     else
         cout<<"selesai";
 }

void buat_baru()
{  
     baru = new(node);
     cout<<"input nama     : ";cin>>baru->nama;
     cout<<"input umur     : ";cin>>baru->umur;
     cout<<"input tinggi   : ";cin>>baru->tinggi;
     baru->prev=NULL;
     baru->next=NULL;
}

void tambah_belakang()
{
     buat_baru();
     if(head==NULL)
      {
         head=baru;
         tail=baru;
      }
     else
     {
         tail->next=baru;
         baru->prev=tail;
         tail=baru;
     }
     cout<<endl<<endl;
     tampil();
}

void tambah_depan()
 {
     buat_baru();
     if(head==NULL)
     {
         head=baru;
         tail=baru;
     }
     else
     {
         baru->next=head;
         head->prev=baru;
         head=baru;
     }
     cout<<endl<<endl;
     tampil();
 }

void hapus_depan()
 {
     if (head==NULL)
         cout<<"Kosong";
     else if (head->next==NULL)
     {
       hapus=head;
       head=NULL;
       tail=NULL;
       delete hapus;
     }
     else
     {
         hapus=head;
         head=hapus->next;
         head->prev=NULL;
         delete hapus;
     }
     cout<<endl<<endl;
     tampil();
 }

void hapus_belakang()
 {
     if (head==NULL)
         cout<<"Kosong";
     else if (head->next==NULL)
     {
       hapus=head;
       head=NULL;
       tail=NULL;
       delete hapus;
     }
     else
     {
      hapus=tail;
      tail=hapus->prev;
      tail->next=NULL;
      delete hapus;
     }
     cout<<endl<<endl;
     tampil();
}

void tampil()
 {
      if (head==NULL)
           cout<<"Kosong";
      else
      {
          bantu=head;
          while(bantu!=NULL)
          {
             cout<<"  nama   : "<<bantu->nama;
             cout<<"  umur   : "<<bantu->umur;
             cout<<"  tinggi : "<<bantu->tinggi<<endl;
             bantu=bantu->next;
          }
      }
      getch();
}

Linked List

#include <iostream>
#include <conio.h>
using namespace std;

struct node
{
  char nama[20];
  int umur;
  float tinggi;
  node *next;
};


node *awal_ptr = NULL;
node *posisi;         //digunakan untuk membaca sepanjang list
int option = 0;

void tambah_awal_list()
{
  node *baru;
  baru = new node;
  cout << "Masukkan Nama     : ";
  cin >> baru->nama;
  cout << "Masukkan Umur     : ";
  cin >> baru->umur;
  cout << "Masukkan tingggi  : ";
  cin >> baru->tinggi;
  baru->next = NULL;
  if(awal_ptr == NULL)
  {
    awal_ptr=baru;
    awal_ptr->next = NULL;
  }
  else
  {
    baru->next = awal_ptr;
    awal_ptr = baru;
  }
}

void menambah_node_di_akhir()
  {
  node *temp, *temp2;   // Temporary pointers
  // menciptakan node baru
  temp = new node;
  cout << "Masukkan Nama     : ";
  cin >> temp->nama;  cout << "Masukkan Umur     : ";
  cin >>   temp->umur;  cout << "Masukkan tingggi  : ";
  cin >> temp->tinggi;  temp->next = NULL;

  // Set up link pada node
  if (awal_ptr == NULL)
  {
    awal_ptr = temp;
    posisi = awal_ptr;
  }
  else
  {
    temp2 = awal_ptr;
    // node tidak NULL – list tidak kosong
    while (temp2->next != NULL)
    {
      temp2 = temp2->next;
    // Memindahkan pada next link dalam rantai
    }
  temp2->next = temp;
  }
}

void display_list()
{
  node *temp;
  temp = awal_ptr;
  cout << endl;
  if (temp == NULL)
    cout << "List kosong!" << endl;
  else
  {
    while (temp != NULL)
    {  // Menampilkan detail data
     cout << "nama : " << temp->nama << "  ";
     cout << "umur : " << temp->umur << "  ";
     cout << "tinggi : " << temp->tinggi;
     if (temp == posisi)
        cout << "     <<<< posisi node";
     cout << endl;
     temp = temp->next;
    }
    cout << "Akhir list!" << endl;
  }
}

void hapus_awal_node()
{
  node *temp;
  temp = awal_ptr;
  awal_ptr = awal_ptr->next;
  delete temp;
}

void hapus_akhir_node()
{
  node *temp1, *temp2;
  if (awal_ptr == NULL)
    cout << "List kosong!" << endl;
  else
  {
    temp1 = awal_ptr;
    if (temp1->next == NULL)
    {
      delete temp1;
      awal_ptr = NULL;
    }
    else
    {
      while (temp1->next != NULL)
      {
        temp2 = temp1;
        temp1 = temp1->next;
      }
      delete temp1;
      temp2->next = NULL;
    }
  }
}

void pindah_posisi_sebelumnya()
{
  if (posisi->next == NULL)
  cout << "Kamu berada pada akhir list." << endl;
  else
  posisi = posisi->next;
}

void pindah_posisi_berikutnya()
{
  if (posisi == awal_ptr)
    cout << "Kamu berada pada awal list" << endl;
  else
  {
    node *previous;     // deklarasi pointer
    previous = awal_ptr;
    while (previous->next != posisi)
    {
      previous = previous->next;
    }
    posisi = previous;
  }
}

void tambah_tengah_list()
{
  node *baru, *bantu;
  int posisi_sisip;
  if(awal_ptr != NULL)
  {
    cout<<"Akan disisip setelah Data Ke ? : ";
    cin>>posisi_sisip;
    bantu=awal_ptr;
    baru =new node;
    for(int i=1;i<posisi_sisip-1;i++) {
      if(bantu->next != NULL)
        bantu=bantu->next;
      else
        break;
    }
  cout << "Masukkan Nama     : ";
  cin >> baru->nama;
  cout << "Masukkan Umur     : ";
  cin >> baru->umur;
  cout << "Masukkan tingggi  : ";
  cin >> baru->tinggi;
  baru->next=bantu->next;
  bantu->next=baru;
  }
  else
  {
    cout<<"Belum ada data !! silahkan isi data dulu....";
    getch();
  }
}
void Hapus_tengah_list()
{
  int banyakdata,posisi_hapus,poshapus;
  node *hapus, *bantu;
  if(awal_ptr != NULL)
  {
    cout<<" Akan dihapus pada data ke : ";
    cin>>posisi_hapus;
    banyakdata=1;
    bantu=awal_ptr;
    while(bantu->next != NULL)
    {
      bantu=bantu->next;
      banyakdata++;
    }
    if((posisi_hapus<1)||(posisi_hapus>banyakdata))
    {
      cout<<"Belum ada data !! masukkan Data dula aja...\n";
    }
    else
    {
      bantu=awal_ptr;
      poshapus=1;
      while(poshapus<(posisi_hapus-1))
      {
        bantu=bantu->next;
        poshapus++;
      }
      hapus=bantu->next;
      bantu->next=hapus->next;
      delete hapus;
    }
 }
 else
    cout<<"Data Masih kosong, tidak bisa hapus data dari tengah! ";
 getch();
}


int main()
{
  awal_ptr = NULL;
  do
  {
      system("cls");
    display_list();
    cout << endl;
    cout << "MENU PILIHAN : " << endl;
    cout << "0. Keluar program." << endl;
    cout << "1. Tambah awal list." << endl;
    cout << "2. Tambah akhir list." << endl;
    cout << "3. Tambah tengah list."<< endl;
    cout << "4. Hapus awal list." << endl;
    cout << "5. Hapus akhir list." << endl;
    cout << "6. Hapus tengah list." << endl;
    cout << "7. Pindah posisi pointer ke berikutnya." << endl;
    cout << "8. Pindah posisi pointer ke sebelumnya." << endl;
    cout << endl << " Pilihan >> ";
    cin >> option;
    system("cls");

  switch (option)
  {
  case 1 : tambah_awal_list();
    break;
  case 2 : menambah_node_di_akhir();
    break;
  case 3 : tambah_tengah_list();
    break;
  case 4 : hapus_awal_node();
    break;
  case 5 : hapus_akhir_node();
    break;
  case 6 : Hapus_tengah_list();
    break;
  case 7 : pindah_posisi_sebelumnya();
    break;
  case 8 : pindah_posisi_berikutnya();
  }

while (option != 0);
}

Double Ended Queue

#include<stdio.h>
#include<conio.h>
#define SIZE 100

void enQueue(int);
int deQueueFront();
int deQueueRear();
void enQueueRear(int);
void enQueueFront(int);
void display();

int queue[SIZE];
int rear = 0, front = 0;

int main()
{
    char ch;
    int choice1, choice2, value;
    printf("\n******* Type of Double Ended Queue *******\n");
     do
     {
          printf("\n1.Input-restricted deque \n");
          printf("2.output-restricted deque \n");
          printf("\nEnter your choice of Queue Type : ");
          scanf("%d",&choice1);
          switch(choice1)
          {
               case 1:
                    printf("\nSelect the Operation\n");
                    printf("1.Insert\n2.Delete from Rear\n3.Delete from Front\n4. Display");
                    do
                    {
                       printf("\nEnter your choice for the operation in c deque: ");
                       scanf("%d",&choice2);
                       switch(choice2)
                       {  
                          case 1: enQueueRear(value);
                                  display();
                                 break;
                             case 2: value = deQueueRear();
                                 printf("\nThe value deleted is %d",value);
                                  display();
                                 break;
                          case 3: value=deQueueFront();
                                     printf("\nThe value deleted is %d",value);
                                  display();
                                     break;
                          case 4: display();
                                  break;
                          default:printf("Wrong choice");
                       }
                       printf("\nDo you want to perform another operation (Y/N): ");
                       ch=getch();
                    }while(ch=='y'||ch=='Y');
                    getch();
                    break;
    
               case 2 :
                   printf("\n---- Select the Operation ----\n");
                   printf("1. Insert at Rear\n2. Insert at Front\n3. Delete\n4. Display");
                   do
                   {
                      printf("\nEnter your choice for the operation: ");
                      scanf("%d",&choice2);
                      switch(choice2)
                      {  
                         case 1: enQueueRear(value);
                                 display();
                                 break;
                         case 2: enQueueFront(value);
                                 display();
                                 break;
                         case 3: value = deQueueFront();
                                 printf("\nThe value deleted is %d",value);
                                 display();
                                 break;
                         case 4: display();
                                 break;
                         default:printf("Wrong choice");
                       }
                       printf("\nDo you want to perform another operation (Y/N): ");
                       ch=getch();
                    } while(ch=='y'||ch=='Y');
                    getch();
                    break ;
            }
            printf("\nDo you want to continue(y/n):");
            ch=getch();
      }while(ch=='y'||ch=='Y');
}

void enQueueRear(int value)
{  
     char ch;
     if(front == SIZE/2)
      {
            printf("\nQueue is full!!! Insertion is not possible!!! ");
            return;
      }
      do
      {
            printf("\nEnter the value to be inserted:");
            scanf("%d",&value);
            queue[front] = value;
            front++;
            printf("Do you want to continue insertion Y/N");
            ch=getch();
      }while(ch=='y');
}

void enQueueFront(int value)
{  
     char ch;
     if(front==SIZE/2)
      {
            printf("\nQueue is full!!! Insertion is not possible!!!");
            return;
      }
      do
      {
            printf("\nEnter the value to be inserted:");
            scanf("%d",&value);
            rear--;
            queue[rear] = value;
            printf("Do you want to continue insertion Y/N");
            ch = getch();
      }
      while(ch == 'y');
}
int deQueueRear()
{
     int deleted;
     if(front == rear)
     {
            printf("\nQueue is Empty!!! Deletion is not possible!!!");
            return 0;
     }
     front--;
     deleted = queue[front+1];
     return deleted;
}
int deQueueFront()
{
     int deleted;
     if(front == rear)
     {
            printf("\nQueue is Empty!!! Deletion is not possible!!!");
            return 0;
     }
     rear++;
     deleted = queue[rear-1];
     return deleted;
}

void display()
{
     int i;
     if(front == rear)
        printf("\nQueue is Empty!!! Deletion is not possible!!!");
     else{
        printf("\nThe Queue elements are:");
        for(i=rear; i < front; i++)
        {
           printf("%d\t ",queue[i]);
        }
     }
}

Circular Queue Dengan Linked List

#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;
class circlst                 //Class Declaration
{
   private:
       struct node
           { int info;             //Node Structure
         node *next;
           }*ptr,*first,*last,*track;
   public:
       circlst()                       //Constructor
       {ptr=first=last=track=NULL;}
       void insrt(int);
       void delnde();
       void displst();      //Memeber Functions
};
void circlst::insrt(int n)
{
  ptr=new node;
     if(ptr==NULL)
    { cout<<"\nNode Cannot Be Created!!!";
      getch();
      exit(0);
    }
   ptr->info=n;
   ptr->next=ptr;            //Function To Insert Node

   if(first==NULL)
      first=last=ptr;
   else
      { last->next=ptr;
    ptr->next=first;
    last=ptr;
      }
}
void circlst::delnde()
{   if(first==NULL)
      { cout<<"\nThe List Is Empty!!!";
    getch();
    exit(0);
      }                           //Function To Delete Node
  first=first->next;
  last->next=first;
  cout<<"\n\nNode Deleted Succesfully!!!";
}
void circlst::displst()
{ track=first;
  if(first==NULL)
     cout<<"\nThe List Is Empty!!!";
  while((track->next)!=first)
    {                                  //Function To Delete Node
      track=track->next;
      cout<<track->info<<" ";
    }
}
int main()                     //Main
{
  circlst ob;
  char choice;
  int data,ch;
  do
  {
    system ("cls");
    cout<<"\n\n\t\tCREATION OF A CIRCULAR LINKED LIST";
    cout<<"\n\t\t---------------------------------";
    cout<<"\n\n1.INSERT ELEMENT";
    cout<<"\n2.DISPLAY LIST";
    cout<<"\n3.DELETE ELEMENT";         //menu
    cout<<"\n4.EXIT";
    cout<<"\n\nWhich Operation Do You Want To Perform : ";
    cin>>ch;
     switch(ch)
          {
         case 1: cout<<"\nEnter the Data To Be Inserted : ";
             cin>>data;
             ob.insrt(data);
             break;
         case 2: ob.displst();
             break;             //calling functions
         case 3: ob.delnde();
             break;
         case 4: exit(1);
         default: cout<<"\nPleasae Enter A valid Choice(1-4)!!!";
           }
    cout<<"\n\nDo you Want to Continue(Y/N) : ";
    cin>>choice;
   }while(choice=='Y' || choice=='y');
}                   //end of program

Circular Queue Dengan Array

#include<iostream>
#include<conio.h>
#include<process.h>
#define SIZE 10
using namespace std;
static int front=0;
static int end=-1;
class queue
{
  private:
      int ar[SIZE];
  public:
      void insert(int item);
      void delque();
      void viewque();
};
void queue::insert(int item)
{
   if(end==SIZE-1)
      cout<<"\nThe Queue is Full!!!";
   else
     { ar[++end]=item;
       cout<<"\nElement succesfully inserted in the Queue!!!";
     }
}
void queue::delque()
{
  if(end<0)
    cout<<"\nQueue Under flow!!!";
  else
   { front++;
     cout<<"\nElement sucessfully deleted from the Queue!!!";
   }
}
void queue::viewque()
{ if(end<0)
    cout<<"\nThe Queue is Empty it cannot be Viewed!!!";
  else
    for(int i=front;i<=end;i++)
      cout<<ar[i]<<" ";
}
int main()
{
  system ("cls");
  char choice;
  int ch,num;
  queue ob;
  do
  {
  system ("cls");
  cout<<"\n\n\t\t\tQ U E U E   O P E R A T I O N S";
  cout<<"\n\t\t\t--------------------------------";
  cout<<"\n\n1.INSERT";
  cout<<"\n2.DELETE";
  cout<<"\n3.DISPLAY";
  cout<<"\n4.EXIT";
  cout<<"\n\nEnter your choice : ";
  cin>>ch;
   switch(ch)
   {
     case 1: cout<<"\nEnter the Element you want to Push : ";
         cin>>num;
         ob.insert(num);
          break;
     case 2: ob.delque(); break;
     case 3: ob.viewque(); break;
     case 4: exit(0);
     default: cout<<"\nPlease Enter a Valid Choice(1-4)!!!";
   }
  cout<<"\nDo you want to Continue(Y/N) : ";
  cin>>choice;
  }while(choice=='y' || choice=='Y');

 getch();
}

Circular Queue

#include<iostream>
#include<cstdlib>
#include<conio.h>
#define n 10
void INSERT(void);
void DELETE(void);
void CETAKLAYAR(void);
void inisialisasi(void);
int PIL,F,R,COUNTER;
char PILIHAN[1],HURUF;
char Q[n];
using namespace std;
int main()
{
    inisialisasi();
    do
    {

        cout<<"                          CIRCULAR QUEUE                             "<<endl;
        cout<<"_____________________________________________________________________"<<endl;
        cout<<" PROGRAM ANIMASI QUEUE                                               "<<endl;
        cout<<"====================================================================="<<endl;
        cout<<" 1.INSERT                                                            "<<endl;
        cout<<" 2.DELETE                                                            "<<endl;
        cout<<" 3.CETAK ANTRIAN                                                     "<<endl;
        cout<<" 4.KELUAR                                                            "<<endl;
        cout<<" * CATATAN : BATAS INPUT = 10 HURUF                                   "<<endl;
        cout<<"_____________________________________________________________________"<<endl;
        cout<<endl;
        cout<<" SILAHKAN MASUKKAN PILIHAN : ";cin>>PILIHAN;
        PIL=atoi(PILIHAN);
        switch(PIL)
        {
            case 1:
                INSERT();
                break;
            case 2:
                DELETE();
                break;
            case 3:
                CETAKLAYAR();
                break;
            default :
            cout<<"TERIMA KASIH"<<endl;
            break;
        }
        cout<<"Press any key to continue"<<endl;
        getch();
        system("cls");
    }
    while (PIL<4);
    return 0;
}

void INSERT(void)
{
    if(COUNTER<n)
    {
        cout<<endl<<"Masukkan 1 huruf : ";
        cin>>HURUF;
        R = (R + 1) % n;
        Q[R] = HURUF;
        COUNTER++;
    }
    else
        cout<<"Antrian Penuh!"<<endl;

}
void CETAKLAYAR(void)
{
    if(COUNTER>0)
    {
        for(int k = 0; k < COUNTER; k++)
        {
            int i = (F+k) % n;
            cout << "Q["<<k<<"]="<<Q[i] << endl;
        }
    }
    else
        cout<<"Queue kosong!"<<endl;

}
void DELETE(void)
{
    if(COUNTER>0)
    {
        HURUF=Q[F];
        F = (F + 1) %n;
        COUNTER--;

        cout<<"Data yang di ambil :"<<HURUF<<endl;
    }
    else
        cout<<"Antrian Kosong!"<<endl;

}
void inisialisasi(void)
{
    F=0;
    R=-1;
    COUNTER=0;
}

Linier Queue

#include<iostream>
#include<conio.h>
#include<process.h>
#define SIZE 10
using namespace std;
static int front=0;
static int end=-1;
class queue
{
  private:
      int ar[SIZE];
  public:
      void insert(int item);
      void delque();
      void viewque();
};
void queue::insert(int item)
{
   if(end==SIZE-1)
      cout<<"\nThe Queue is Full!!!";
   else
     { ar[++end]=item;
       cout<<"\nElement succesfully inserted in the Queue!!!";
     }
}
void queue::delque()
{
  if(end<0)
    cout<<"\nQueue Under flow!!!";
  else
   { front++;
     cout<<"\nElement sucessfully deleted from the Queue!!!";
   }
}
void queue::viewque()
{ if(end<0)
    cout<<"\nThe Queue is Empty it cannot be Viewed!!!";
  else
    for(int i=front;i<=end;i++)
      cout<<ar[i]<<" ";
}
int main()
{
  char choice;
  int ch,num;
  queue ob;
  do
  {
  system("cls");
  cout<<"\n\n\t\t\tQ U E U E   O P E R A T I O N S";
  cout<<"\n\t\t\t--------------------------------";
  cout<<"\n\n1.INSERT";
  cout<<"\n2.DELETE";
  cout<<"\n3.DISPLAY";
  cout<<"\n4.EXIT";
  cout<<"\n\nEnter your choice : ";
  cin>>ch;
  system("cls");
   switch(ch)
   {
     case 1: cout<<"\nEnter the Element you want to Push : ";
         cin>>num;
         ob.insert(num);
          break;
     case 2: ob.delque(); break;
     case 3: ob.viewque(); break;
     case 4: exit(0);
     default: cout<<"\nPlease Enter a Valid Choice(1-4)!!!";
   }
  cout<<"\nDo you want to Continue(Y/N) : ";
  cin>>choice;
  }while(choice=='y' || choice=='Y');

 getch();
}

Queue Pada C++

#include <stdio.h>
#include <conio.h>
#include <iostream>
#define MAX 6
using namespace std;

struct Queue
{
  int data[MAX];
  int head;
  int tail;
};

Queue antrian;
void Create()
{
  antrian.head=antrian.tail=-1;
}

int IsEmpty()
{
  if(antrian.tail==-1)
    return 1;
  else
    return 0;
}
int IsFull()
{
  if(antrian.tail==MAX-1)
    return 1;
  else
    return 0;
}

void Enqueue(int data)
{
  if(IsEmpty()==1)
  {
     antrian.head=antrian.tail=0;
     antrian.data[antrian.tail]=data;
     cout<<antrian.data[antrian.tail];
  }
  else
   //kodisi lainnya jika penuh() sama dengan 0 maka antrian.ekor ditambah 1
  {
     antrian.tail++;
     antrian.data[antrian.tail]=data;
     cout<<antrian.data[antrian.tail];
  }
}

int Dequeue()
{
  int i;
  int e=antrian.data[antrian.head];
  for(i=antrian.head;i<=antrian.tail-1;i++)
  {
    antrian.data[i]=antrian.data[i+1];
  }
  antrian.tail--;
  return e;
}

void Clear()
{
  antrian.head=antrian.tail=-1;
  cout<<"Data Clear";
}

void Tampil()
{
  if (IsEmpty()==0)
    for (int i=antrian.head;i<=antrian.tail; i++)
    cout<<antrian.data[i]<<"  ";
  else
    cout<<"Data Kosong\n";
}

int main()
{
  int pil;
  int data;
  Create();
  do
  {
      system("cls");
    cout<<"\n============MENU PILIHAN============\n";
    cout<<"1. Enqueue\n";
    cout<<"2. Dequeue\n";
    cout<<"3. Tampil\n";
    cout<<"4. Clear\n";
    cout<<"5. Keluar\n";
    cout<<"--------------------------------------\n";
    cout<<"Masukkan Pilihan Anda -> ";
    cin>>pil;
    system("cls");
    switch(pil)
    {
    case 1:
      cout<<"Data : ";cin>>data;
      Enqueue(data);
      break;
    case 2:
      if (IsEmpty()==0)
       cout<<"Elemen yang keluar : "<<Dequeue();
      else
       cout<<"Data kosong"<<endl;
      break;
    case 3:
      Tampil();
      break;
    case 4:
      Clear();
      break;
    case 5:
      break;
    }
    getch();
  }
while(pil!=5);
}

Double Stack

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#define n 10
using namespace std;
char P[]={'>',' ',' ',' ',' ',' '};
int S[n],mov[2],X,Y,pil=0;
int *top1,*top2,*dasar1,*dasar2,*helpI;
//utama
void awal()
{
 top1=&S[-1];
 top2=&S[n];
 dasar1=&S[-1];
 dasar2=&S[n];
 helpI=&S[-1];
}
void push1(int x)
{
 top1=top1+1;
 *top1=x;
}
void push2(int y)
{
 top2=top2-1;
 *top2=y;
}
void pop1()
{
 X=*top1;
 *top1=0;
 top1=top1-1;
}
void pop2()
{
 Y=*top2;
 *top2=0;
 top2=top2+1;
}
int BisaDiisi(int k)
{
 if(top2-top1>k)
   return 1;
 else
   return 0;
}
int BisaDiambil1()
{
 if(top1>dasar1)
   return 1;
 else
   return 0;
}
int BisaDiambil2()
{
 if(top2<dasar2)
   return 1;
 else
   return 0;
}
void tampil()
{
 cout<<"\n================ data menjadi ==================="<<endl;
 while(helpI!=(dasar2-1))
 {
  helpI++;
  cout<<*helpI<<" ";
 }
 cout<<"\n======================================================"<<endl;
 helpI=&S[-1];
}
//tambahan
void tampilMenu()
{
    system("cls");
    cout<<"========================================================"<<endl;
    cout<<"1. isi stack 1 (kiri)"<<endl;
    cout<<"2. isi stack 2 (kanan)"<<endl;
    cout<<"3. isi kedua stack"<<endl;
    cout<<"4. pop stack 1"<<endl;
    cout<<"5. pop stack 2"<<endl;
    cout<<"6. pop kedua stack"<<endl;
    cout<<"========================================================="<<endl;
    cout<<"pilihan anda : ";cin>>pil;
    system("cls"); 
}
int main()
{
 awal();
 do
 {
  tampilMenu();
  cout<<endl;
  //cout<<"nilai pil = "<<pil<<endl;
  switch(pil)
  {
     case 1:
     if(BisaDiisi(1))
     {
           cout<<"masukkan bilangan = ";
           cin>>X;
           push1(X);
     }
     else
     {
           cout<<"maaf tidak ada tempat untuk push";
     }
     break;
    
     case 2:
     if(BisaDiisi(1))
     {
           cout<<"masukkan bilangan = ";
           cin>>Y;
           push2(Y);
     }
     else
     {
           cout<<"maaf tidak ada tempat untuk push";
     }
     break;
   
     case 3:
     if(BisaDiisi(2))
     {
           cout<<"masukkan bilangan 1= ";
           cin>>X;
           push1(X);
           cout<<"masukkan bilangan 2= ";
           cin>>Y;
           push2(Y);
     }
     else
     {
           cout<<"maaf ruang tidak cukup";
     }
     break;
   
     case 4:
     if(BisaDiambil1())
     {
           pop1();
           cout<<"data yang diambil = "<<X<<endl;
     }
     else
     {
           cout<<"maaf stack 1 tidak ada isinya"<<endl;
     }
     break;
   
     case 5:
     if(BisaDiambil2())
     {
           pop2();
           cout<<"angka yang diambil = "<<Y<<endl;
     }
     else
     {
           cout<<"maaf stack 2 tidak ada isinya"<<endl;
     }
     break;
   
     case 6:
     if(BisaDiambil1()&&BisaDiambil2())
     {
           pop1();
           pop2();
           cout<<"isi yang baru di pop = "<<X<<" dan "<<Y<<endl;
     }
     else
     {
           cout<<"maaf salah satu atau kedua stack tidak ada isinya"<<endl;
     }
     break;
  }
    tampil();
    cout<<"\nenter untuk mengulang dan esc untuk keluar !";
    do
    {
       mov[0]=getch();
       if(mov[0]==27)
       exit(0);
    }
    while(mov[0]!=13);
 }
 while(mov[0]==13);
 getch();
}

Implementasi Stack

#include <string>
#include <iostream>
using namespace std;

int top = -1;
char stack[128]; // secara program, jika stack sudah berisi lebih dari 128 item, ERROR.
char x;

void push()
{
    cout << "Masukkan satu karakter ";
    cin >> x;

    top++;
    stack[top] = x;

    cin.clear();
    cin.ignore(1, '\n');
}

void pop()
{
    if(top < 0)
    {
        cout << "Stack kosong" << endl;
        return;
    }

    x = stack[top];
    top--;

    cout << "Karakter yang terakhir masuk adalah " << x << endl;
}

void cetak()
{
    if(top < 0)
    {
        cout << "Stack kosong" << endl;
        return;
    }

    int i = 0;
    for(i = top; i >= 0; i--)
        cout << stack[i] << endl;

    // kalo mau dicetak dari yang pertama masuk pakai script dibawah
    //for(i = 0; i <= top; i++)
    //    cout << stack[i] << endl;
}

int main()
{
    string input = "";
    cout << "Pilihan yang dikenal adalah" << endl;
    cout << "\tpush" << endl;
    cout << "\tpop" << endl;
    cout << "\tcetak stack" << endl;
    cout << "\tquit" << endl;

    while(true)
    {
        cout << "Masukkan pilihan: ";

        getline(cin, input);

        if(input == "push")
        {
            push();
        }
        else if(input == "pop")
        {
            pop();
        }
        else if(input == "cetak stack")
        {
            cetak();
        }
        else if(input == "quit")
        {
            break;
        }
        else
        {
            cout << "Perintah '" << input << "' tidak dikenal" << endl;
        }
    }

    cout << "Program Akan berhenti" << endl;
}

Stack Dengan Pointer

#include <string>
#include <iostream>
#include <conio.h>
#define size 5
using namespace std; 

struct node
{
  int bil;
  struct node *next;
}; 
struct stack
{
  int jumlah;
  struct node *top;
}; 
int cekPenuh(stack *stack)
{
      if(stack->jumlah==size)
      return 1;
      else
      return 0;

int cekKosong(stack *stack)
{
     if(stack->jumlah==0)
     return 1;
     else
     return 0;

void Push(stack *stack)
{
      node *baru;
      if (cekPenuh(stack))
      {
        cout<<"stack full";
        getch();
      }
      else
      {
          baru=new(node);
          cout<<"\nmasukkan nilai yang ingin dipush ke stack: ";
          cin>>baru->bil;
          baru->next=stack->top;
          stack->top=baru;
          stack->jumlah++;
      }


void cetak(stack *stack)
{
      node *bacaPtr;
      bacaPtr=stack->top;
      if(cekKosong(stack))
      {
        cout<<"\nstack kosong";
        getch();
      }
      else
      {
        cout<<"\nTOP\n";
        cout<<"---------\n";
        while(bacaPtr!=NULL)
          {
            cout<<bacaPtr->bil<<endl;
            cout<<"---------"<<endl;            
            bacaPtr=bacaPtr->next;
          }
          getch();
      }
}  
void Pop(stack *stack)
{
      node *hapusPtr;
      hapusPtr=stack->top;
      if (cekKosong(stack))
      {
        cout<<"Stack Kosong";
        getch();
      }
      else
      {
         stack->top=stack->top->next;
        stack->jumlah--;
        delete hapusPtr;
        cout<<"\nproses pop berhasil\n";
        getch();
      }

void Top(stack *stack)
{
     int dataTop;
     if(cekKosong(stack))
     {
       cout<<"\ntop = NULL\n";
       getch();
     }
     else
     {
       dataTop=stack->top->bil;
       cout<<"\ntop = "<<dataTop<<endl;
       getch();
     }
}


  void hapus(stack *stack)
  {
     node *bantuHapus;
     while(stack->top!=NULL)
     {
        bantuHapus=stack->top;
        stack->top=stack->top->next;
        delete bantuHapus;
     }
     stack->jumlah=0;
  }
 int main()
 {
   stack stack;
   stack.jumlah=0;
   stack.top=NULL;
   char pilih;
   do
    {
      system("cls");
      cout<<"MENU STACK"<<endl;
      cout<<"[1]. Kosongkan Stack"<<endl;
      cout<<"[2]. Push"<<endl;
      cout<<"[3]. Pop"<<endl;
      cout<<"[4]. Lihat Top Stack"<<endl;
      cout<<"[5]. Tampilkan stack"<<endl;
      cout<<"[6]. Keluar\n"<<endl;
      cout<<"\npilihan: ";
      cin>>pilih;
      system("cls");
     
      if(pilih=='1')
      hapus(&stack);
      if(pilih=='2')
      {
        Push(&stack);
        cetak(&stack);
      }
      if(pilih=='3')
      {
        Pop(&stack);
        cetak(&stack);
      }
      if(pilih=='4')
      Top(&stack);
      if(pilih=='5')
      cetak(&stack);
    }
while(pilih!='6');
 }

Contoh Ke - 2 Stack Dengan Array

#include<iostream>
#include<conio.h>
#include<process.h>
#define SIZE 10
using namespace std;
static int top=-1;
class stack
{
  private:
      int ar[SIZE];
  public:
      void push(int item);
      void pop();
      void peep();
};
void stack::push(int item)
{
   if(top==SIZE-1)
      cout<<"\nThe Stack is Full!!!";
   else
     { ar[++top]=item;
       cout<<"\nElement succesfully pushed in the Stack!!!";
     }
}
void stack::pop()
{
  if(top<0)
    cout<<"\nStack Under flow!!!";
  else
   { top--;
     cout<<"\nElement sucessfully popped from the Stack!!!";
   }
}
void stack::peep()
{ if(top<0)
    cout<<"\nThe Stack is Empty it cannot be Peeped!!!";
  else
    for(int i=top;i>=0;i--)
      cout<<ar[i]<<" ";
}
int main()
{
  char choice;
  int ch,num;
  stack ob;
  do
  {
  system ("cls");
  cout<<"\n\n\t\t\tS T A C K   O P E R A T I O N S";
  cout<<"\n\t\t\t-------------------------------";
  cout<<"\n\n1.PUSH";
  cout<<"\n2.POP";
  cout<<"\n3.PEEP";
  cout<<"\n4.EXIT";
  cout<<"\n\nEnter your choice : ";
  cin>>ch;
   switch(ch)
   {
     case 1: cout<<"\nEnter the Element you want to Push : ";
         cin>>num;
         ob.push(num);
          break;
     case 2: ob.pop(); break;
     case 3: ob.peep(); break;
     case 4: exit(0);
     default: cout<<"\nPlease Enter a Valid Choice(1-4)!!!";
   }
  cout<<"\nDo you want to Continue(Y/N) : ";
  cin>>choice;
  }while(choice=='y' || choice=='Y');

 getch();
}

Stack Dengan Array

#include<stdio.h>
#include<conio.h>

#define SIZE 10

void push(int);
void pop();
void display();

int stack[SIZE], top = -1;

int main()
{
   int value, choice;
   while(1)
   {
      printf("\n\n***** MENU *****\n");
      printf("1. Push\n2. Pop\n3. Display\n4. Exit");
      printf("\nEnter your choice: ");
      scanf("%d",&choice);
      switch(choice)
      {
     case 1: printf("Enter the value to be insert: ");
         scanf("%d",&value);
         push(value);
         break;
     case 2: pop();
         break;
     case 3: display();
         break;
     case 4:
         break;
      }
   }
}
void push(int value){
   if(top == SIZE-1)
      printf("\nStack is Full!!! Insertion is not possible!!!");
   else{
      top++;
      stack[top] = value;
      printf("\nInsertion success!!!");
   }
}
void pop(){
   if(top == -1)
      printf("\nStack is Empty!!! Deletion is not possible!!!");
   else{
      printf("\nDeleted : %d", stack[top]);
      top--;
   }
}
void display(){
   if(top == -1)
      printf("\nStack is Empty!!!");
   else{
      int i;
      printf("\nStack elements are:\n");
      for(i=top; i>=0; i--)
     printf("%d\n",stack[i]);
   }
}

Stack Pada C++

#include<iostream>
#include<stdio.h>
#include<conio.h>
#define size 50
using namespace std;

struct stack
{
  int elemen[size];
  int top;
};
typedef struct stack STACK;

// operasi push
void push(STACK *p,int value)
{
  if(p->top==size-1)
   cout<<"STACK penuh ";
  else
   p->elemen[++p->top]=value;
}
//operasi pop
int pop(STACK *p) 
{
if (p->top==-1) 
{
  cout<<"STACK kosong";
  return -1;
}
else
return p->elemen[p->top--];
}
//menampilkan stack
void display (STACK *p)
{
  int i;
  if(p->top==-1)
   cout<<"\n STACK kosong\n";
  else
   cout<<"\nIsi STACK adalah : \n";
   for (i=p->top;i>=0; --i)
   cout<<p->elemen[i]<<"\n";
}

int main()
{
  STACK s ;
  int x,c,i;
  s.top=-1;
  do
  {
   system("cls");
   cout<<"MENU PILIHAN";
   cout<<"\n1: Operasi PUSH\n";
   cout<<"2: Operasi POP\n";
   cout<<"3: Tampilkan Stack\n";
   cout<<"4: Hapus Stasck\n";
   cout<<"5: Keluar\n";
   cout<<"\n\n Pilihan anda : ";
   cin>>c;
   system("cls");
   switch(c)  
   {
   case 1: 
     cout<<"\nMasukkan Elemen Stack: ";cin>>x;
     push (&s,x);
     display(&s);
     break;
   case 2:  x=pop(&s);
     if(x!=-1)
     cout<<"\nMenghapus Element = "<<x;
     break;
   case 3: display(&s);
     break;
   case 4:
     if(s.top==-1)
      cout<<endl<<"STACK kosong";
     else
      cout<<endl<<"STACK dihapus"<<endl;
                     //Menghapus STACK
   for (i=s.top;i>=0; --i)
   cout<<"Elemen yang dihapus adalah : "<<pop(&s)<<endl;
   s.top=-1;
   }
  getch();
  }
while(c!=5);
}