Showing posts with label DDC2423. Show all posts
Showing posts with label DDC2423. Show all posts

Wednesday, August 24, 2011

Linked List : aturcara tanpa guna class (int data)


aturcara ni ditulis guna Turbo C++, kalau nak guna Visual tukarlah apa2 yang patut yer...

#include
#include

struct NODE
{
   int  data;
   NODE *link;
};

struct HEAD
{
   int count;
   NODE *pos;
   NODE *head;
};

void CreateList(HEAD **list)
{
   (*list)->head = NULL;
   (*list)->pos = NULL;
   (*list)->count = 0;
}

int InsertNode(HEAD **list, NODE *pPre, int dataIn)
{
   NODE *pNew = new NODE();
   if (pNew == NULL)
      return 0;
   pNew->data = dataIn;
   pNew->link = NULL;
   if (pPre == NULL)
   {
      pNew->link = (*list)->head;   // insert depan or first node
      (*list)->head = pNew;
   }
   else
   {
      pNew->link = pPre->link;
      pPre->link = pNew;
   }
   (*list)->count++;
   return 1;
}

int emptyList(HEAD *list)
{
   return (list->count==0);
}
 

int fullList()
{
   NODE *temp=new NODE();

   if (temp == NULL )
   {
      free(temp);
      return 0;
   }
   return 1;
}

void DeleteNode(HEAD *list, NODE *pPre, NODE *pLoc, int *dataOut)
{
   *dataOut = pLoc->data;
   if (pPre == NULL)
      list->head = pLoc->link;
   else
      pPre->link = pLoc->link;
   list->count--;
   free (pLoc);
   return;
}

int SearchList(HEAD *list, NODE **pPre, NODE **pLoc, int key)
{
   *pPre = NULL;
   *pLoc = list->head;
   if (list->count == 0)
      return 0;
   while ((*pLoc) != NULL && key != (*pLoc)->data  )
   {
      *pPre = *pLoc;
      *pLoc = (*pLoc)->link;
   }
   if ((*pLoc)==NULL)
      return 0;
   else if (key == (*pLoc)->data)
      return 1;
   else
      return 0;
}

int ListCount(HEAD *list)
{
   return list->count;
}


int retrieveNode(HEAD *list,int key, int &dataOut)
{
   int found;
   NODE *pPre;
   NODE *pLoc;

   found = SearchList(list,&pPre, &pLoc, key);
   if (found)
      dataOut = pLoc->data;
   return found;
}

int removeNode(HEAD *list,int dltKey, int *dataOut)
{
   int found;
   NODE *pPre;
   NODE *pLoc;

   found = SearchList(list, &pPre, &pLoc, dltKey);
   if (found)
      DeleteNode(list,pPre, pLoc, dataOut);
   return found;
}

int traverseList(HEAD *list, int fromWhere, int &dataOut)
{
   int success;

   if (fromWhere == 0)
   {
      if (list->count==0)
         success = 0;
      else
      {
         list->pos = list->head;
         dataOut = list->pos->data;
         success = 1;
      }
   }
   else
   {
      if (list->pos->link == NULL)
         success = NULL;
      else
      {
         list->pos = list->pos->link;
         dataOut = list->pos->data;
         success = 1;
      }
   }
   return success;
}
void main()
{
   HEAD *pList=new HEAD();
   int isData;
   int data,nom;

   CreateList(&pList);
   cout<<"program cubaan linked list\n\n";
   cout<<"******** masuk data ****************\n\n ";
   cout<<"masukkan 1 data integer = (enter 999 to stop) -> ";
   cin>>data;
   while (data != 999)
   {
      if (InsertNode(&pList,NULL,data))
         cout<<"\ndata telah ditambah"<
      else
      cout<<"error - tak boleh insert nod lagi"<
      cout<<"masukkan 1 data integer = (enter 999 to stop) -> ";
      cin>>data;
   }
   cout<<"************ print data *******************\n\n";
   for(int ulang=0; ulang
   {
      isData = traverseList(pList,ulang, nom);
      if (isData)
         cout<<(ulang + 1)<< ". " << nom<
      else
      cout<<"there is no data in the list";
   }
   cout<<"*************** cari data *******************\n\n";
   cout<<"sila masukkan 1 nilai untuk operasi carian = ";
   cin>>data;
   if (retrieveNode(pList,data, data))
      cout<<"data " << data << " ada dalam senarai\n ";
   else
      cout<<" data " << data << " tidak wujud di dalam senarai\n";
   cout<<"*************** delete data *******************\n\n";
   cout<<"sila masukkan 1 nilai untuk operasi delete = ";
   cin>>data;
   if (removeNode(pList,data,&data))
      cout<<"data " <<< " telah didelete"<
   else
      cout<<"tiada data"<
   cout<<"*************** print data *******************\n\n";
   for(ulang=0; ulang
   {
      isData = traverseList(pList,ulang, nom);
      if (isData)
         cout<<(ulang + 1)<< ". " << nom<
   }
}

Linked List : Latihan T/F and soalan pendek


Soalan T/F : Jawab Betul atau salah
1.The last node in a linked list is not necessarily NULL (T/F)
2.    Any item can be achieved regardless of its position in the list (T/F)
3.    Head node will keep the same content as the information of other nodes in the list (T/F)
4.    A __________________ is an aggregation of atomic and composite data types into a set with defined relationships.
5.    A ________________ node is a data structure that contains metadata about the list, such as a count, a head pointer to the first node and a rear pointer to the last node.
6.    The nodes in the linked list are called ______________ structures, which each instance of the structure contains a pointer to another instance of the same structural type.
7.    Linear list can be divided into two categories, ____________________ and restricted.
8.    The pointer to the beginning of the list is known as a _________________.


Soalan Pendek 
 
9.    Give two examples for each atomic and composite data types in C programming language.

10.  Give two statements related to data structure definition?


11.  What does it means by the concept of Abstract Data Type?

Monday, July 11, 2011

DDC 2423 - Slides

bawah ni adalah slide yang digunakan di dalam pembelajaran subjek struktur data

DDC 2423 : Chapter 1 : Intro
DDC 2423 : Chapter 2 : List
DDC 2423 : Chapter 3 : Stack
DDC 2423 : Chapter 4 : Queue
DDC 2423 : Chapter 5 : Recursion
DDC 2423 : Chapter 6 : Tree
DDC 2423 : Chapter 7 : Search
DDC 2423 : Chapter 8 : Graphs
DDC 2423 : Chapter 9 : Sorting

bawah ni, link kepada soalan latihan atau assignment, atau bicara tentang algo
chapter 1
chapter 1
chapter 3
chapter 4
chapter 5
chapter 6
chapter 7
chapter 8
chapter 9

E-Novel