#include
#include
using namespace std;
typedef int status;
typedef int elemType;
typedef struct lNode
{
elemType num;
struct lNode *next;
}lNode,*linkNode;
linkNode initListNode();
void echNode(linkNode L);
linkNode insertNode(linkNode L);
void creatList(linkNode L,int n);
void echList(linkNode L);
linkNode delNode(linkNode L,int n);
linkNode searchNode(linkNode head,int n);
int main()
{
linkNode l,m;
l=initListNode();
printf("After Initialization:\n");
echNode(l);
printf("\nCreat and echo List:\n");
creatList(l,4);
echList(l);
printf("\nFind 2nd elemet:\n");
m=searchNode(l,2);
printf("Find result:\n");
echNode(m);
printf("\nDelt nodes and echo List\n");
m=delNode(l,1);
m=delNode(m,1);
echList(m);
}
linkNode initListNode()
{
linkNode L;
L=(linkNode)malloc(sizeof(lNode));
L->num=0;
L->next=NULL;
return L;
}
void creatList(linkNode L,int n)
{
int i;
for(i=0;i
{
L=insertNode(L);
L->num=i+1;//num value
}
}
void echNode(linkNode L)
{
printf("Node num is %d\n",L->num);
}
linkNode insertNode(linkNode L)
{
linkNode tmpp;
tmpp=(linkNode)malloc(sizeof(lNode));
//tmpp->num=9;
tmpp->next=NULL;
L->next=tmpp;
return tmpp;
}
void echList(linkNode L)
{
linkNode tmpp;
tmpp=L;
do
{
echNode(tmpp);
tmpp=tmpp->next;
}
while(tmpp);
}
linkNode searchNode(linkNode head,int n)
{
linkNode tmpp;
tmpp=head;
//printf("\nSearch %dnd element!!\n",n);
while(--n)
{
if(tmpp->next)
{
tmpp=tmpp->next;
}
else
{
//cout<<"Over max quantity of Nodes of the List";
exit(0) ;
}
}
//printf("The num is %d.\n\n",tmpp->num);
return tmpp;
}
linkNode delNode(linkNode head,int n)
{
linkNode tmpp,nodep;
if(n>1)
{
tmpp=searchNode(head,n-1);
nodep=tmpp->next;
tmpp->next=tmpp->next->next;
free(nodep);
return head;
}
else if(n=1)
{
tmpp=head->next;
free(head);
return tmpp;
}
}