Nama            :  Dwi Nuraini             (12110364)
                        Manda Irwansyah     (12110339)
                        Rina Pancari             (12110403)
                        Wani Melani             (12110351)
                        Yayuk Sulistiawati    (12110288)
Kelas            :  TI –P 1204
Mata Kuliah : STRUKTUR DATA
Dosen           : Yasir Hasan, S.Kom.
 

1.    Program STACK dengan metode Notasi Polish dalam Pascal dan C++.

a. Menggunakan bahasa C++

- Listing program :
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#define size 5

struct stack
{

int a[size];
int top;
};

typedef struct stack STACK;

void push(STACK *p,int value) /* PUSH OPERATION */
{
if(p->top==size-1)
cout<<"STACK IS FULL ";
else
p->a[++p->top]=value;
}

int pop(STACK *p) /* POP OPERATION */
{
if (p->top==-1)
{
cout<<"STACK IS EMPTY";

return -1;
}
else
return p->a[p->top--];
}


void display (STACK *p) /*DISPLAY OPERATION */
{ int i;
if(p->top==-1)
cout<<"\n STACK IS EMPTY\n";
else
cout<<"\nSTACK is\n";
for (i=p->top;i>=0; --i)
cout<<p->a[i]<<"\n";
}


void main()
{

STACK s ;
int x,c,i;
s.top=-1;
do
{
cout<<"\n1: To PUSH\n";
cout<<"2: To POP\n";
cout<<"3: To Display\n";
cout<<"4: To Destroy\n";
cout<<"5: To Exit\n";
cout<<"\n\n Enter Choice\n";cin>>c;

switch(c)
{
case 1: cout<<"\nEnter Element: ";cin>>x;
push (&s,x);
break;
case 2: x=pop(&s);
if(x!=-1)
cout<<"\nElement deleted="<<x;
break;
case 3: display(&s);
break;
case 4: if(s.top==-1)
printf("\nSTACK IS EMPTY\n");
else
printf("\nSTACK is destroying\n"); /*

STACK DESTROY */
for (i=s.top;i>=0; --i)
printf("element destroyed are %d\n",pop(&s));
s.top=-1;
} printf("\nSTACK DESTROYED\n");
}while(c!=5);
}/*end main */


b. Menggunakan bahasa Pascal

-Listing program:
uses crt;

const lowbound = 0;
      upbound  = 5;

 type pstack = ^stack;

     stack  = record
       info  : byte;
       x,y   : byte;
       next  : pstack;
     end;

     tumpukan = object
       cur,head,tail : pstack;
       counter,len   : byte;
       posx,posy     : byte;
       ada,isempty   : boolean; {ada = terbentuk}

       procedure inisialisasi;
       procedure menu;
       procedure keterangan;
       procedure create;
       procedure push;
       procedure pop;
     end;

procedure tumpukan.inisialisasi;
begin
     counter := 0; len := 20; posx := 40; posy := 24;
     ada := false; isempty := true;
end;

procedure tumpukan.keterangan;
var noel,top,i : byte;
begin
     noel := counter; if noel = 0 then isempty := true else isempty := false;

     gotoxy(2,15); write('Isempty :      '); gotoxy(12,15); write(isempty);
     gotoxy(2,16); write('Noel    : ',noel);
     gotoxy(2,17); write('Top     : ');
     if isempty then
     begin
          gotoxy(12,17); clreol;
     end
        else
     begin
          gotoxy(12,17); clreol;
          gotoxy(12,17); for i := 1 to cur^.info do write('-')
     end;
end;

procedure tumpukan.create;
var tekan : char;
begin
     if ada then
     begin
          gotoxy(2,12); write('Stack sudah terbentuk');
     end
        else
     begin
          new(cur); head := cur; tail := cur; tail^.next := nil;

          gotoxy(2,12); write('Stack terbentuk');
          ada := true;
     end;
     keterangan;

     gotoxy(2,13); write('Press Return to continue');
     repeat tekan := readkey until tekan = #13;
end;

procedure tumpukan.push;
var i : byte;
    tekan : char;
begin
     if not ada then
     begin
          gotoxy(2,12); write('Stack belum terbentuk');
          gotoxy(2,13); write('Press Return to continue');
          repeat tekan := readkey until tekan = #13;
     end
        else
     begin
          if counter = upbound then
          begin
               gotoxy(2,12); write('Stack Overflow');
               gotoxy(2,13); write('Press Return to continue');
               repeat tekan := readkey until tekan = #13;
          end
             else
          begin
               new(cur); cur^.next := head; head := cur;
               cur^.x := posx; cur^.y := posy; cur^.info := len;

               gotoxy(cur^.x,cur^.y); for i := 1 to cur^.info do write('-');

               if counter <> upbound then
               begin
                    inc(posx,2); dec(posy,2); dec(len,4);
               end;

               inc(counter);
          end;
          keterangan;
     end;
end;

procedure tumpukan.pop;
var i : byte;
    tekan : char;
begin
     if not ada then
     begin
          gotoxy(2,12); write('Stack belum terbentuk');
          gotoxy(2,13); write('Press Return to continue');
          repeat tekan := readkey until tekan = #13;
     end
        else
     begin
          if counter = lowbound then
          begin
               gotoxy(2,12); write('Stack Underflow');
               gotoxy(2,13); write('Press return to continue');
               repeat tekan := readkey until tekan = #13;
          end
             else
          begin
               gotoxy(cur^.x,cur^.y); for i := 1 to cur^.info do write(' ');
               if counter <> lowbound then
               begin
                    dec(posx,2); inc(posy,2); inc(len,4);
               end;

               dec(counter);

               head := cur^.next; dispose(cur); cur := head;

          end;
          keterangan;
     end;
end;

procedure tumpukan.menu;
var pil : char;
begin
     clrscr;
     gotoxy(2,2); write('-- STACK --');
     gotoxy(3,4); write('1. Create');
     gotoxy(3,5); write('2. Push');
     gotoxy(3,6); write('3. Pop');
     gotoxy(3,7); write('4. Exit');
     gotoxy(2,9); write('Pilihan : ');
     repeat
           gotoxy(2,12); clreol;
           gotoxy(2,13); clreol;
           gotoxy(12,9); {posisi kursor}

           repeat pil := readkey until pil in['1'..'4'];
           gotoxy(12,9); write(pil);

           case pil of
                  '1' : create;
                  '2' : push;
                  '3' : pop;
           end;
     until pil = '4';
end;

var akses : tumpukan;
begin
     with akses do
     begin
          inisialisasi;
          menu;
     end;
end.


2.      Program  metode Binary Tree Search yang mengimplementasikan penggunaan metode Notasi Polish menggunakan bahasa C++

a.       Menggunakan bahasa C++
-Listing program :
#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
    int data;
    struct TreeNode *leftChildNode;
    struct TreeNode *rightChildNode;
};

typedef struct TreeNode node;
node *rootNode = NULL;

void insertNode(int i, node *n) {
    if(n == NULL) {
                          n = (node*)malloc(sizeof(node));
        n->leftChildNode = NULL;
        n->rightChildNode = NULL;
        n->data = i;
    }
    else
             if(n->data == i)
        printf("\nThis value already exists in the tree!");
    else
    if(i > n->data)
        insertNode(i, n->rightChildNode);
    else
                          insertNode(i, n->leftChildNode);
    }

void searchNode(int i, node *n) {
    if(n == NULL)
        printf("\nValue does not exist in tree!");
             else
    if(n->data == i)
        printf("\nValue found!");
    else
    if(i > n->data)
        searchNode(i, n->rightChildNode);
             else
        searchNode(i, n->leftChildNode);
    }

void deleteNode(int i, node *n) {
    if(n == NULL)
                          printf("\nValue does not exist in tree!");
    else
    if(n->data == i) {
        if(n->leftChildNode == NULL)
            n = n->rightChildNode;
        else
                          if(n->rightChildNode == NULL)
            n = n->leftChildNode;
        else {
            node *temp = n->rightChildNode;
            while(temp->leftChildNode != NULL)
                temp = temp->leftChildNode;
                                                n = temp;
        }
    }
    else
    if(i > n->data)
        deleteNode(i, n->rightChildNode);
             else
        deleteNode(i, n->leftChildNode);
    }

void displayPreOrder(node *n) {
    if(n != NULL) {
                          printf("%d ", n->data);
        displayPreOrder(n->leftChildNode);
        displayPreOrder(n->rightChildNode);
    }
}

void displayPostOrder(node *n) {
    if(n != NULL) {
        displayPostOrder(n->leftChildNode);
        displayPostOrder(n->rightChildNode);
        printf("%d ", n->data);
             }
}

void displayInOrder(node *n) {
    if(n != NULL) {
                          displayInOrder(n->leftChildNode);
        printf("%d ", n->data);
        displayInOrder(n->rightChildNode);
    }
}

int main(void) {
    int ch, num, num1;
    do {
        printf("\nSelect a choice from the menu below.");
                          printf("\n1. Insert a node.");
        printf("\n2. Search for a node.");
        printf("\n3. Delete a node.");
        printf("\n4. Display the Binary Search Tree.");
        printf("\nChoice: ");
                          scanf("%d", &ch);
        switch(ch) {
            case 1: printf("\nEnter an element: ");
                    scanf("%d", &num);
                                                                          //printf("YESYES");
                    insertNode(num, rootNode);
                    break;

                                                case 2: printf("\nEnter the element to be searched for: ");
                    scanf("%d", &num);
                    searchNode(num, rootNode);
                    break;

            case 3: printf("\nEnter the element to be deleted: ");
                    scanf("%d", &num);
                    deleteNode(num, rootNode);
                                                                          break;

            case 4: printf("\nSelect an order for the elements to be display in.");
                    printf("\n1. Pre-order.");
                                                                          printf("\n2. Post-order.");
                    printf("\n3. In-order.");
                                                                          printf("\nChoice: ");
                    scanf("%d", &num1);
                                                                          switch(num1) {
                        case 1: printf("\nPre-order Display: ");
                                                                                                                          displayPreOrder(rootNode);
                                break;

                        case 2: printf("\nPost-order Display: ");
                                                                                                                          displayPostOrder(rootNode);
                                break;

                        case 3: printf("\nIn-order Display: ");
                                                                                                                          displayInOrder(rootNode);
                                break;

                        default: exit(0);
                    }
                                                                          break;

            default: exit(0);
                                                }
        //printf("%d", rootNode->data);
                          printf("\nIf you want to return to the menu, press 1.");
                          printf("\nChoice: ");
                          scanf("%d", &num);
             } while(num == 1);

             return 0;
}


0 Response to " "

Post a Comment