MATERI-MATERI MATA KULIAH SISTEM INFORMASI STMIK MDP

Pasang Iklan Gratis Disini !!!

ads ads ads ads

Selasa, 07 Juni 2011

Double Ended Queue

. Selasa, 07 Juni 2011

      #SISTEM ANTRIAN#
------------------------------
No. Antrian : <1>
Nama        : <input>
------------------------------
Isi Antrian : <Output>
------------------------------
Ulangi lagi (y/t): <input>
//tambah antrean (add queue)
===============================

head|0  --> A|O  --> B|0

                                  ^
                                   |
tail|0  ----------------->
// main menu
=====================

Keluarkan Antrian (y/t) = <input>
--------------------------------
isi Antrian : <output>
--------------------------------
ulangi lagi (y/t) : <input>
//hapus antrian
======================
           #CODING#
struct node
{
    int data;
    node *next;
};
node *head =NULL;
node *newNode;
char lagi =='y';
int nilai;
cout<<" *****TAMBAH SIMPUL*****\n";
//add@front
while (lagi=='y')
newNode = (node*) malloc (sizeof(node))
cout<<"Masukkan nilai : ";cin>>nilai;
newNode->data=nilai;
newNode->next=head;
head=newNode;
cout<<"-------------------\n ulangi y/t: ";cin>>lagi;
cout<<" Isi List : ";
//display the information stored in linked list

node *temp;
temp=head; //transfer the address of 'temp' to 'head'
while (temp!=NULL)
{
    cout<< temp->data<<"  "; //show the data in the linked list
    temp = temp->next; // transfer the address of 'temp->next
}
}
cout<<endl;

delete@end
node *currNode;
currNode = (node*)malloc(sizeof(node));
node * preNode;
preNode = (node*)malloc(sizeof(node));
//sequential searching
currNode = head;
while (currNode->next!=NULL)
{
    preNode = currNode;
    currNode = currNode->next;
}
preNode->next=NULL; //previous node of the last node is null
free (currNode);


contoh 2 :
#include "iostream"
using namespace std;
void main ()
{
struct node
{
    string nama;
    node *text;
};
node *head=NULL, *tail=NULL;
node *newNode;
char lagi ='y';
int no=1;
cout<<" *****TAMBAH Antrian*****\n;
//add@back
while(lagi=='y')
newNode = (node*)malloc(sizeof(node));
cout<<" No. Antrian   : "<<no++;
cout<<" Masukkan Nama : ";cin>>nama;
newNode->nama = nama;
if (head==NULL) //jika list kosong
{newNode ->next=head;
head = newNode;
tail = newNode;
}
else
{
    newNode->next=tail;
    tail->next=newNode;
    tail=newNode;
}

SEMOGA BERMANFAAT

0 komentar:

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Posting Komentar

Tuliskan Komentar Disini..

Related Posts Plugin for WordPress, Blogger...
 
{Sistem Informasi} is proudly powered by Blogger.com | Template by : Sukriyadi | sukriyadi.blogspot.com