Posts

Showing posts from 2016

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();
     }
 …

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 << &quo…

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",&…

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=f…

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…

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 ANT…

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…

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()==…

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======================================================&q…

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 << "…

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
      …

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-----…

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 possib…

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&qu…

Full Modul Kuliah "Teknik Informatika" Universitas Pamulang - UNPAM