Professional Documents
Culture Documents
h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
/*
****************************************************************
CODE WRITTEN BY NADIA MIJA, MARCH 27, 2003, BUCHAREST,ROMANIA
****************************************************************
You can use this code for any purpuses except publication or selling.
****************************************************************
I ALSO HAVE WRITTEN A FEW FUNCTIONS WHICH ARE DEPENDENT TO THE TNODE
STRUCTURE; THEY NEED TO BE REWRITTEN FOR EVERY
APPLICATION
- n/N for no
3) void PrintNode(TNODE *p) //function specific to the application --
shows the information of the p node
PLEASE ALSO READ THE COMMENTS IN THE CODE FOR MORE INFORMATION
****************************************************************
*/
TNODE *first, *last; //pointers to the first and last element of the
linked list
if(LoadNode(p)!=1)
{
FreeNode(p);
break;
}
p->next=0;
if (first==0) //this list was empty since now
first=last=p;
else
{
last->next=p;
last=p;
}
}
}
char opt;
printf("\nNew node?");
opt=getche();
opt=toupper(opt);
if(opt!='N')
{
puts("\nPlease insert data for the current node:");
printf("\nlkey:\t");
if (scanf("%d",&(p->lkey))!=1) return 0; //could not
read lkey value for current node
TNODE* InsertBeforeFirst()
//general function
//returns the node inserted
//or 0 for failed insertion
{
TNODE *p;
int n=sizeof(TNODE);
if (((p=(TNODE*)malloc(n))!=0) && (LoadNode(p)==1)) //a new node
has been succesfully allocated and loaded
{
if (first==0) //list was empty
{
p->next=0;
first=last=p;
}
else
{
p->next=first;
first=p;
}
return p;
}
if(p==0) //not enough memory
printf("\nNot enough memory");
else //the node could not be loaded
FreeNode(p);
return 0; //there is no node inserted before first -- insertion
failed
}
//find q, q1
q1=0;
q=first;
while(q)
{
if(q->lkey == key) break; //key node found
q1=q; //keep on searching for key node
q=q->next;
}
if(q==0)
{
printf("\nThere is no node having such a key or the list
is empty");//this case also includes the case of empty list
return 0;//there is no node having such a key --
insertion can't be made
}
{
TNODE *p, *q;
//p=the new node to insert
//q=key
int n=sizeof(TNODE);
//find q
q=first;
while(q)
{
if(q->lkey == key) break; //key node found
q=q->next; //keep on searching for key node
}
if(q==0)
{
printf("\nThere is no node having such a key or the list
is empty");//this case also includes the case of empty list
return 0;//there is no node having such a key --
insertion can't be made
}
TNODE* InsertAfterLast()
//general function
//returns the node inserted
//or 0 for failed insertion
{
TNODE *p;
int n=sizeof(TNODE);
if (((p=(TNODE*)malloc(n))!=0) && (LoadNode(p)==1)) //a new node
has been succesfully allocated and loaded
{
p->next=0;
if (first==0) //list was empty
first=last=p;
else
{
last->next=p;
last=p;
}
return p;
}
if(p==0) //not enough memory
printf("\nNot enough memory");
else //the node could not be loaded
FreeNode(p);
return 0; //there is no node inserted after last -- insertion
failed
}
void RemoveFirst()
//general function
//removes the first node of the list; pre and post-conditions: none
{
TNODE *p;
p=first;
first=first->next;
FreeNode(p);
}
void RemoveLast()
//general function
//removes the last node of the list; pre and post-conditions: none
{
TNODE *p, *q;
}
void RemoveByKey(int key)
{
TNODE *p, *q;
//p - the key node
//q=the node before the key node
q->next=p->next;
FreeNode(p);
}
void DeleteList()
{
TNODE *p;
p=first;
while(p)
{
first=first->next;
FreeNode(p);
p=first;
}
last=0;
}
void main()
{
CreateList();//this is an example of using these fonctions
ViewAllList();
InsertAfterLast();
ViewAllList();
RemoveFirst();
ViewAllList();
InsertAfterKey(1);//by example
ViewAllList();
RemoveByKey(1);
ViewAllList();
DeleteList();
ViewAllList();