Kamis, 16 Juni 2011

Program Single Linked List

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

#define Nil NULL
#define info(P) P->info
#define next(P) P->next
#define First(L) (L)

typedef int InfoType;
typedef struct telmlist *address;
typedef struct telmlist
{
InfoType info;
   address next;
}elmtlist;

typedef address list;

void CiptaSenarai(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"<<endl;
}

int main()
{
list pel;
   list n;
   int i,k,nilai;

   CiptaSenarai(&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);
   getch();
   return 0;
}

Hasilnya inputkan data sendiri ya....!! hohaheh

Tidak ada komentar: