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<
}
}
0 comments:
Post a Comment