Tampilkan postingan dengan label Struktur Data. Tampilkan semua postingan
Tampilkan postingan dengan label Struktur Data. Tampilkan semua postingan

Struktur data bahasa c++


Nama               : Richard Nainggolan
Nim                 : 14221026
M.Kuliah         : Struktur DATA (C/C++)
P.Studi            : T.Informatika
UAS STRUKTUR DATA
 
1.SINGLE LINGKED LIST NON CIRCULAR
#include <iostream>
#include <conio>
struct TNode
{
int nim;
TNode *next;
};
TNode *head, *tail;
void init(){
head = NULL;
tail = NULL;
}
int isEmpty(){
 if(tail == NULL) return 1;
 else return 0;
}
void insertDepan(int databaru){
  TNode *baru;
  baru = new TNode;
  baru->nim = databaru;
  baru->next = NULL;
  if(isEmpty()==1){
head=tail=baru;
tail->next=NULL;
 }
  else {
 baru->next = head;
 head = baru;
  }
  cout<<"Data masuk\n";
}

void insertBelakang(int databaru){
 TNode *baru;
 baru = new TNode;
 baru->nim = databaru;
 baru->next = NULL;
 if(isEmpty()==1){
 head=baru;
 tail=baru;
 tail->next = NULL;
 }
 else {
  tail->next = baru;
  tail=baru;
 }
 cout<<"Data masuk\n";
}

void tampil(){
 TNode *bantu;
 bantu = head;
 if(isEmpty()==0){
  while(bantu!=NULL){
   cout<<bantu->nim<<" ";
   bantu=bantu->next;
  }
     } else cout<<"Masih kosong\n";
  }
  void hapusSemua(){
                TNode *bantu,*hapus;
                bantu = head;
                while(bantu!=NULL){
                                hapus = bantu;
                                bantu = bantu->next;
                                delete hapus;
                }
                head = NULL;
}



void hapusDepan(){
 TNode *hapus;
 int d;
 if (isEmpty()==0){
  if(head!=tail){
   hapus = head;
   d = hapus->nim;
   head = head->next;
   delete hapus;
  } else {
   d = tail->nim;
   head=tail=NULL;
  }
  cout<<d<<"terhapus";
 } else cout<<"Masih kosong\n";
}
void hapusBelakang(){
 TNode *bantu,*hapus;
 int d;
 if (isEmpty()==0){
  bantu = head;
  if(head!=tail){
   while(bantu->next!=tail){
    bantu = bantu->next;
   }
   hapus = tail;
   tail=bantu;
   d = hapus->nim;
   delete hapus;
   tail->next = NULL;
  }else {
   d = tail->nim;
   head=tail=NULL;
  }
  cout<<d<<" terhapus\n";
 } else cout<<"Masih kosong\n";
}
void inserttengah(int databaru){
 TNode *baru;
 baru = new TNode;
 baru->nim = databaru;
 baru->next = NULL;
 if(isEmpty()==1){
 head=baru;
 head->next = NULL;
 }
 else {
  tail->next = baru;
  tail=baru;
 }
 cout<<"Data masuk\n";
}


void main()
{
int pil,databaru;
cout<<"Single Linked List Non Circular "<<endl;
cout<<"Nama    : Richard Nainggolan "<<endl;
cout<<"Nim     : 14221026  "<<endl;
cout<<"Jurusan : Teknik Informatika "<<endl;
cout<<"Sekolah Tinggi Ilmu Komputer Medan "<<endl;
do
{
cout<<"\n";
cout<<"\n********************************************************************************";
cout<<"\n1. Insert Depan";
cout<<"\n2. Insert Belakang";
cout<<"\n3. Delete Depan";
cout<<"\n4. Delete Belakang";
cout<<"\n5. Delete Semua";
cout<<"\n6. Tampil Data";
cout<<"\n7. Insert Tengah";
cout<<"\n\nSilahkan Masukan Pilihan Anda :";cin>>pil;
cout<<"\n";
switch (pil)
{
case 1:
{
cout<<"Masukkan Data = ";
cin>>databaru;
insertDepan(databaru);
break;
}
case 2:
{
cout<<"Masukkan Data = ";
cin>>databaru;
insertBelakang(databaru);
break;
}
case 3:
{
hapusDepan();
break;
}
case 4:
{
hapusBelakang();
break;
}
case 5:
{
 hapusSemua();
 break;

}

case 6:
{
tampil();
break;
}
default :
{
cout<<"\n Maaf, Tidak ada dalam pilihan";
}
}
}
while(pil>=1 && pil<= 6);
}
2. SINGLE LINGKED LIST CIRCULAR


#include <iostream>
#include <conio>
struct TNode
{
int nim;
TNode *next;
};
TNode *head, *tail;
void init(){
head = NULL;
tail = NULL;
}
int isEmpty(){
 if(tail == NULL) return 1;
 else return 0;
}
void insertDepan(int databaru){
  TNode *baru;
  baru = new TNode;
  baru->nim = databaru;
  baru->next = baru;
  if(isEmpty()==1){
head=tail=baru;
head->next=head;
tail->next=tail;
 }
  else {
 baru->next = head;
 head = baru;
 tail->next=head;
  }
  cout<<"Data masuk\n";
}

void insertBelakang(int databaru){
 TNode *baru;
 baru = new TNode;
 baru->nim = databaru;
 baru->next = baru;
 if(isEmpty()==1){
 head=baru;
 tail=baru;
 head->next=head;
 tail->next = tail;
 }
 else {
  tail->next = baru;
  tail=baru;
  tail->next=head;
 }
 cout<<"Data masuk\n";
}

void tampil(){
 TNode *bantu;
 bantu = head;
 if(isEmpty()==0){
            do{
   cout<<bantu->nim<<" ";
   bantu=bantu->next;
   }
   while(bantu!=tail->next);
   cout<<"\n";

     } else cout<<"Masih kosong\n";
  }
  void hapusSemua(){
            TNode *bantu,*hapus;
            if (isEmpty()==0){
   bantu=head;
   while (bantu->next!=head){
   hapus=bantu;
   bantu=bantu->next;
   delete hapus;
   }
   head=NULL;
   tail=NULL;
   cout<<"Data Nim Semua Terhapus";
   }

   else cout<<"Masih Kosong";

   }



void hapusDepan(){
 TNode *hapus;
 if (isEmpty()==0){
 int d;
 hapus=head;
 d=head->nim;
 if(head!=tail){
 hapus=head;
 head=head->next;
 tail->next=head;
 delete hapus;
 }else{
 head=NULL;
 tail=NULL;
  }
  cout<<d<<"terhapus";
 } else cout<<"Masih kosong\n";
}
void hapusBelakang(){
 TNode *bantu,*hapus;
 int d;
 if (isEmpty()==0){
  if(head==tail){
            d=tail->nim;
   head=NULL;
   tail=NULL;
   }else{
   bantu=head;
   while(bantu->next!=tail){
   bantu=bantu->next;
   }
   hapus=tail;
   tail=bantu;
   d=hapus->nim;
   tail->next=head;
   delete hapus;
  }
  cout<<d<<" terhapus\n";
 } else cout<<"Masih kosong\n";
}
void main()
{
int pil,databaru;
cout<<"Single Linked List Circular "<<endl;
cout<<"Nama    : Richard Nainggolan "<<endl;
cout<<"Nim     : 14221026  "<<endl;
cout<<"Jurusan : Teknik Informatika "<<endl;
cout<<"Sekolah Tinggi Ilmu Komputer Medan "<<endl;
do
{
cout<<"\n";
cout<<"\n********************************************************************************";
cout<<"\n1. Insert Depan";
cout<<"\n2. Insert Belakang";
cout<<"\n3. Delete Depan";
cout<<"\n4. Delete Belakang";
cout<<"\n5. Delete Semua";
cout<<"\n6. Tampil Data";
cout<<"\n7. Inset Tengah";
cout<<"\n\nSilahkan Masukan Pilihan Anda :";cin>>pil;
cout<<"\n";
switch (pil)
{
case 1:
{
cout<<"Masukkan Data NIM = ";
cin>>databaru;
insertDepan(databaru);
break;
}
case 2:
{
cout<<"Masukkan Data NIM = ";
cin>>databaru;
insertBelakang(databaru);
break;
}
case 3:
{
hapusDepan();
break;
}
case 4:
{
hapusBelakang();
break;
}
case 5:
{
 hapusSemua();
 break;

}

case 6:
{
tampil();
break;
}
default :
{
cout<<"\n Maaf, Tidak ada dalam pilihan";
}
}
}
while(pil>=1 && pil<= 6);
}

Kirimkan Donasinya :D, jangan copi-copi aja kerjaanya :)

Struktur Data Tugas STACK

1)Merubah notasi infix ke postfix dengan menggunakan STACK
Gambarnya





#include <iostream> #include <sstream> #include <stack> #include <limits> #include <string> using namespace std;
int priority(char a) {
    int temp;
    if (a == '^')
        temp = 1;
    else if (a == '*' || a == '/')
        temp = 2;
    else if (a == '+' || a == '-')
        temp = 3;
    return temp;
}

int main() {
    string infix;
    cout << "Masukan Infix : ";
    getline(cin, infix);

    stack<char> opr_stack;

    stringstream postfix;

    for (unsigned i = 0; i < infix.length(); i++) {
        if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^') {
            while (!opr_stack.empty() && priority(opr_stack.top()) <= priority(infix[i])) {
            postfix << opr_stack.top();
            opr_stack.pop();
            }
        opr_stack.push(infix[i]);
        } else if (infix[i] == '(') {
            opr_stack.push(infix[i]);
        } else if (infix[i] == ')') {
            while (opr_stack.top() != '(') {
            postfix << opr_stack.top();
            opr_stack.pop();
            }
            opr_stack.pop();
        } else {
            postfix << infix[i];
        }
    }

    while (!opr_stack.empty()) {
        postfix << opr_stack.top();
        opr_stack.pop();
    }

    cout << "Postfix : " << postfix.str() << endl;

    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    return 0;
}



2)Menghitung/mengevaluasi notasi postfix dengan menggunakan STACK
#include <stdio.h>
#include "stackChar.h"
#include "stackFloat.h"
#include <string.h>
#include <stdlib.h>

int prior(char x)
{
    switch(x)
    {
        case ')': return 0;
        case '(': return 0;
        case '+': return 1;
        case '-': return 1;
        case '*': return 2;
        case '/': return 2;  
    }
}

int prioritas(char a, char b){
    return (prior(a)>=prior(b));
}

char *konversi(char *infix){
    int i,index=0,panjang;
    char *postfix,tmpchar;
    stackChar tmp;
   
    createStackC(&tmp);
    postfix=(char *)malloc(sizeof(char)*100);
   
    panjang=strlen(infix);
    for(i=0;i<panjang;i++){
        switch(infix[i]){
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                postfix[index]=infix[i];
                index++;
                break;
            case '+':
            case '-':
            case '*':
            case '/':
                postfix[index]=' ';
                index++;           
                if(isEmptyC(tmp)){
                    pushValueC(&tmp,infix[i]);
                }else if(!prioritas(info(top(tmp)),infix[i])){
                    pushValueC(&tmp,infix[i]);
                }else{
                    while(!isEmptyC(tmp)&&prioritas(info(top(tmp)),infix[i])){
                        popValueC(&tmp,&tmpchar);
                        postfix[index]=tmpchar;
                        index++;
                    }               
                    pushValueC(&tmp,infix[i]);
                }
                break;
            case ')':
                while(!isEmptyC(tmp)&&info(top(tmp))!='('){
                        popValueC(&tmp,&tmpchar);
                        postfix[index]=tmpchar;
                        index++;                   
                }
                popValueC(&tmp,&tmpchar);
                break;
            case '(':
                pushValueC(&tmp,infix[i]);
                break;
        }
       
    }
    while(!isEmptyC(tmp)){
            popValueC(&tmp,&tmpchar);
            postfix[index]=tmpchar;
            index++;                 
    }   
    postfix[index]='\0';
    return postfix;
}

float calculatePostfix(char *postfix){
    int i,panjang;
    float hasil=0,tmpfloat,tmpfloat1,tmpfloat2;
    stackFloat tmp;
   
    createStackF(&tmp);   
    panjang=strlen(postfix);
    for(i=0;i<panjang;i++){
        switch(postfix[i]){
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                tmpfloat+=((int)postfix[i]-48)*1.0;  
                if(postfix[i+1]>='0'&&postfix[i+1]<='9'){
                    tmpfloat*=10;
                }else{
                    pushValueF(&tmp,tmpfloat);
                    tmpfloat=0;
                }
                break;
            case '+':
                popValueF(&tmp,&tmpfloat2);
                popValueF(&tmp,&tmpfloat1);
                tmpfloat1+=tmpfloat2;
                pushValueF(&tmp,tmpfloat1);
                break;
            case '-':
                popValueF(&tmp,&tmpfloat2);
                popValueF(&tmp,&tmpfloat1);
                tmpfloat1-=tmpfloat2;
                pushValueF(&tmp,tmpfloat1);           
                break;
            case '*':
                popValueF(&tmp,&tmpfloat2);
                popValueF(&tmp,&tmpfloat1);
                tmpfloat1*=tmpfloat2;
                pushValueF(&tmp,tmpfloat1);           
                break;
            case '/':
                popValueF(&tmp,&tmpfloat2);
                popValueF(&tmp,&tmpfloat1);
                tmpfloat1/=tmpfloat2;
                pushValueF(&tmp,tmpfloat1);           
                break;       
        }   
    }
    popValueF(&tmp,&hasil);
    return hasil;
}
int main()
{
    char input[100];
    char *tpostfix;
    float hasil=0;
   
    printf("Masukkan infix : ");
    scanf("%s",input);
    tpostfix=konversi(input);
    printf("notasi postfix : %s \n",tpostfix);
    hasil=calculatePostfix(tpostfix);
    printf("hasil perhitungan : %.2f\n",hasil);
    return 0;
}