Rabu, 30 September 2015

Selasa, 17 Juli 2012

STUCK C++

TUGAS MODUL 7.1 STUCK

INPUT :
//============================================================================
// Name        : 1_stuck.cpp
// Author      : agust
// Version     :
// Copyright   :
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
#define MAX_STACK 10
struct STACK
{
int top;
   char data[10][10];
   };STACK tumpuk;
void inisialisasi()
{
tumpuk.top = -1;
}
int IsFull()
{
if(tumpuk.top ==MAX_STACK-1) return 1; else return 0;
   }
int IsEmpty()
{
if(tumpuk.top == -1) return 1; else return 0;
   }
   void Push (char d [10])
{
tumpuk.top++;
   strcpy(tumpuk.data[tumpuk.top],d);
   }
void Pop()
{
cout<<"data yang terambil = "<<tumpuk.data[tumpuk.top]<<endl;
tumpuk.top--;
}
void TampilStack()
{
for(int i =tumpuk.top;i>=0;i--)
{
cout<<"data : "<<tumpuk.data[i]<<endl;
   }
   }
void Clear()
{
tumpuk.top=-1;
   }
int main(){
int pil;
   inisialisasi();
   char dt[10];
   do{
    cout<<"\n1. push\n";
      cout<<"2. pop\n";
      cout<<"3. print\n";
      cout<<"4. clear\n";
      cout<<"5. exit\n";
      cout<<"pilihan : "; cin>>pil;
      switch(pil){
      case 1: if (IsFull() !=1){
          cout<<"data = ";cin>>dt;
                  Push(dt);
                  }
                  else cout<<"\n Stact penuh!\n";
                  break;
         case 2 : if (IsEmpty() != 1)
          Pop();
                  else
                  cout<<"\n Stack kosong!\n";
                  break;
         case 3 : if (IsEmpty() !=1)
          TampilStack();
                  else
                  cout<<"Stact kosong!\n";
                  break;
         case 4 : Clear();
         cout<<"\n Stack sudah kosong !\n ";
         break;
         }
         //getch();
         } while (pil !=5);
   return main();
   return 0;
}

OUTPUT :
Gambar 1. Tampilan Code

Gambar 2. Hasil Output

LINKED LIST C++

TUGAS MODUL 6.2 LINKED LIST 

input :
//============================================================================
// Name        : 2_linkedList.cpp
// Author      : agust
// Version     :
// Copyright   :
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdlib.h>
#include <malloc.h>
//#include <conio.h>

using namespace std;

#define nil NULL
#define info(P) P->info
#define next(P) P->next
#define first(L) (L)

typedef int infotype;
typedef struct telmlist *alamat;
typedef struct telmlist
{
infotype info;
   alamat next;
}elmtlist;

typedef alamat list;;

void buatsenarai (list*L)
{
first(*L) = nil;
}
list nodbaru(int m)
{
list n;
   n = (list) malloc(sizeof(elmtlist));
   if (n!=NULL)
   {
    info(n) =m;
      next(n) =nil;
   }
   return n;
}
void sisipsenarai (list *L,list t, list p)
{
if (p==nil)
{
    t->next=*L;
      *L=t;
   }
   else
   {
    t->next=p->next;
      p->next=t;
   }
}

void cetaksenarai (list L)
{
list ps;

for (ps=L; ps!=nil; ps=ps->next)
{
cout<<" "<<info(ps)<<" -->";
   }
   cout<<"NULL\n";
}
int main()
{
list pel;
   list n;
   int i,k,nilai;

   buatsenarai(&pel);
   cout<<"masukkan banyak data = ";
   cin>>k;
   for (i=1;i<=k;i++)
   {
   cout<<"masukkan data senarai ke-"<<i<<" = ";
   cin>>nilai;
   n=nodbaru(nilai);
   sisipsenarai (&pel,n,NULL);
}

cetaksenarai(pel);
return 0;
}

output :




TUGAS MODUL 6.1 LINKED LIST

input : 
//============================================================================
// Name        : 1_LinkedList.cpp
// Author      : agust
// Version     :
// Copyright   :
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int main()
{
int i;

struct ListEntry
{
int number;
struct ListEntry *next;
}   start, *node;

start.next=NULL;
node=&start;
for (i=1; i<=10; i++)
{
node->next=(struct ListEntry *) malloc(sizeof(struct ListEntry));
   node=node->next;
   node->number=i;
   node->next=NULL;
}

node=start.next;

while (node)
{
   cout<<node->number;
   node=node->next;
   }
return 0;
}

output :


Selasa, 05 Juni 2012