MATERI-MATERI MATA KULIAH SISTEM INFORMASI STMIK MDP

Pasang Iklan Gratis Disini !!!

ads ads ads ads

Selasa, 31 Mei 2011

Deklarasi Node

. Selasa, 31 Mei 2011

CONTOH 1:
#include "iostream"
using namespace std;
void main () {
    struct node        //deklarasi node
    {
         int data;
             node *next;
    };

    node *head=NULL; // create head
    node *newNode ;  // create a temporary node
    int nilai=10;
    // add@front, nilai = 10
    newNode = (node*)malloc(sizeof(newNode)); //allocate space
    newNode ->data = nilai;   //store data (first field)
    newNode ->next = head;    //store the address of the point
    head = newNode;
    nilai=20;
    //add@front, nilai = 20
    newNode = (node*)malloc(sizeof(newNode));  //allocate space
    newNode ->data = nilai;   //store data (first field)
    newNode ->next = head;    //store the address of the point
    head = newNode;
    //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' to
    }

    system ("pause");
}

CONTOH 2:
#include "iostream"
using namespace std;
void main () {
    struct node        //deklarasi node
    {
         int data;
             node *next;
    };

    node *head=NULL; // create head
    node *newNode ;  // create a temporary node

    int nilai;
    char ulang='y';
    while (ulang=='y')
    {
    cout<<"Masukkan Sebuah Nilai : ";cin>>nilai;
    // add@front
    newNode = (node*)malloc(sizeof(newNode)); //allocate space
    newNode ->data = nilai;   //store data (first field)
    newNode ->next = head;    //store the address of the point
    head = newNode;
    //display the information stored in linked list

    node*temp;
    temp=head; //transfer the address of 'temp' to 'head'
    cout<<" Isi List Sekarang : ";
    while (temp!=NULL)
    {
        cout<< temp->data<<"  "; // show the data in the linked list
        temp = temp->next; //transfer the address of 'temp->next' to 'temp-
        }
        cout<<"\n Ulangi Cari (Y/N) : ";cin>>ulang;
    }

    system ("pause");
}

CONTOH 3:
#include "iostream"
using namespace std;
void main () {
    struct node        //deklarasi node
    {
         int data;
             node *next;
    };

    node *head=NULL; // create head
    node *newNode ;  // create a temporary node

    int nilai;
    char ulang='y';
    node*temp;
    while (ulang=='y')
{
    cout<<"Masukkan Sebuah Nilai : ";cin>>nilai;
    // add@front
    newNode = (node*)malloc(sizeof(node)); //allocate space
    newNode ->data = nilai;   //store data (first field)
    newNode ->next = head;    //store the address of the point
    head = newNode;
    //display the information stored in linked list

    temp=head; //transfer the address of 'temp' to 'head'
    cout<<" Isi List Sekarang : ";
    while (temp!=NULL)
    {
        cout<< temp->data<<"  "; // show the data in the linked list
        temp = temp->next; //transfer the address of 'temp->next' to 'temp-
        }
        cout<<"\n Ulangi Cari (Y/N) : ";cin>>ulang;
    }
    //DELETE@front        //create a temporary
    cout<< "Hapus Sebuah Node di depan \n";
    temp=(node*)malloc(sizeof(node)); //allocate space for node
    temp=head;       // transfer the address of 'head' to 'temp'
    head=temp->next; // transfer the address of 'temp->next' to 'temp->'
    free(temp);
    temp=head;
    cout<<"Isi List Sekarang : ";
    while (temp!=NULL)
    {
        cout<<temp->data<<"  "; // show the data in the linked list
        temp = temp->next; //transfer the address of 'temp->next' to 'temp-
        }
    system ("pause");
}

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