Friday, October 24, 2008

Algorithm for Array Linked List (new)

#include< stdio.h>
#include< conio.h>
#include< stdlib.h>
#define Max_Array 50

struct array_list {

int info, next;
};

struct array_list node[Max_Array];

//Define as global variable
int avail;

/*Initially, all nodes are unused, since no lists have yet been
formed. Therefore, they must all be placed on the available list.
If the global variable avail is used to point to the available list,
we may initially organize that list as follows */
void available() {
avail=0;
for (int i=avail; i > Max_Array -1; i++)
node[i].next=i+1;
node[Max_Array-1].next=-1;
}

/*Getnode is a function that removes a node from the available list and returns
a pointer to it */
int getnode() {
int p;
if (avail==-1) {
printf("Overflow\n");
exit(1);
}
p=avail;
avail=node[avail].next;
return (p);
}/* End of getnode */

/*Freenode is function that accepts a pointer to a node and return that node to
the available list */

int freenode(int p) {
node[p].next=avail;
avail=p;
return;
} /* end of freenode */

/*Besides the primitive operations, routine insafter(insert after) is a function
that accepts a pointer p to a node and an item x as parameters. it first ensures
that p is not full and then insert x into a nodefollowing the node pointed to by p */
int insafter(int p, int x) {
int q;
if (p==-1) {
printf("Void insertion\n");
return;
}
q=getnode();
node[q].info=x;
node[q].next=node[p].next;
node[p].next=q;
return;
} /*end insafter */

/*Routine delafter(p,px), called by the statement delafter(p, &x), deletes the
node following node(p) and stores its contents in x. */
int delafter(int p, int *px) {
int q;
if ((p==-1)||(node[p].next==-1)) {
printf("Void deletion\n");
return;
}
q=node[p].next;
*px=node[q].info;
node[p].next=node[q].next;
freenode(q);
return;
} /*end of delafter */
/* Before calling insafter we must be sure that p is not null. Before calling
delafter we must be sure that neither p nor node[9].next is null */

/* For main function, you may try by getting from circular queue, try it then
see what happen */

//Have a good day

1 comment:

Sen Rithy said...

teacher when i try to run this code in C++ it show 5 error.
It said "return;" should have return a value.Please help