The program is like this so far:
Code:
struct node
{
int dataitem;
int link;
};
void initList( struct node [], int *, int);
void findNode( struct node [], int*);
int ReadData( struct node [], int*);
void Add2List(struct node[], int , int);
int _tmain(int argc, _TCHAR* argv[])
{
struct node linkList[21];
int start, current, newnode;
initList(linkList, &start, 21);
printf("Enter and integer value : ");
ReadData(linkList,&newnode);
Add2List(linkList, start, newnode);
printf("Enter and integer value : ");
ReadData(linkList,&newnode);
Add2List(linkList, start, newnode);
current = start;
while (current != -1)
{
printf("the data is %d and the link value is %d\n\n",
linkList[current].dataitem,
linkList[current].link);
current= linkList[current].link;
}
return 0;
}
void initList( struct node linkList[],int *ptr, int listSize)
{
*ptr = 0;
linkList[*ptr].link = -1;
linkList[*ptr].dataitem = 0;
for (int current = 1; current < listSize; current++)
linkList[current].link = 0;
}
void findNode(struct node LinkList[], int *next)
{
int current = 1;
while (LinkList[current].link != 0)
{
current++;
}
*next = current;
}
int ReadData(struct node linkList[], int *NewNode)
{
findNode(linkList, NewNode);
scanf("%d", &linkList[*NewNode].dataitem);
return 0;
}
void Add2List(struct node linkList[], int start, int newnode)
{
int current = start;
while (linkList[current].link != -1)
current = linkList[current].link;
linkList[current].link = newnode;
linkList[newnode].link = -1;
linkList[start].dataitem += 1;
}
void DeleteNode(struct node linkList[], int start, int oldData)
{
int current, oldNode;
current = SearchList( linkList, start, oldData);
oldNode = linkList[current].link;
linkList[current].link = linkList[oldNode].link;
linkList[oldNode].link = 0;
linkList[start].dataitem -= 1;
}
int SearchList(struct node linkList[], int start, int oldData)
{
int current = start;
while (linkList[linkList[current].link].dataitem != oldData)
current = linkList[current].link;
return current;
}