链表的操作
typedef struct node
{
int value;
struct node * next;
} Node;
typedef Node * NodeList;
typedef Node NodeArry[MAX];
int Print_List(NodeList L)
{
if(!L)
return 0;
NodeList p;
int i = 0;
p = L->next;
printf("Head = %p Head->next = %p\n",L,L->next);
while(p)
{
printf("Node_%-3d= %p value = %-4d next = %p\n", i,p, p->value,p->next);
p = p->next;
i++;
}
return 1;
}
int Get_Element(NodeList L, int index)
{
if(!L || index < 0)
return NULL;
int i=0;
NodeList p=L->next;
while(i < index && p)
{
i++;
p = p->next;
}
if(p && i==index)
return p->value;
else
return NULL;
}
NodeList Init_List()
{
NodeList L = (NodeList)malloc(sizeof(Node));
L->next = NULL;
return L;
}
NodeList Creat_Random(int length)
{
if(length < 1)
return NULL;
NodeList p, q;
srand(time(0));
NodeList L = (NodeList)malloc(sizeof(Node));
q = L;
while(length--)
{
p = (NodeList)malloc(sizeof(Node));
p->value = rand() % 200;
q->next = p;
q = p;
}
q->next = NULL;
return L;
}
NodeList Merge_Lists(NodeList *L1, NodeList *L2)
{
if(!(*L1 && *L2))
return 0;
NodeList p=*L1,q=*L2;
//p=p->next;
while(p->next)
p = p->next;
p->next = q->next;
p = *L1;
*L1 = NULL;
*L2 = NULL;
return p;
}
int Insert_Element(NodeList L, int index, int value)
{
int i = 0;
NodeList p = L, q;
if(index < 0)
return 0;
while(i++ < index && p)
p = p->next;
if(p)
{
q = (NodeList)malloc(sizeof(NodeList));
q->next = p->next;
q->value = value;
p->next = q;
return 1;
}
else
return 0;
}
int Remove_Element(NodeList L, int index)
{
int i = 0;
NodeList p = L, q = p;
if(index < 0)
return 0;
while(i++ < index && p->next)
p = p->next;
if(p->next && i>= index)
{
q = p->next;
p->next = q->next;
free(q);
return 1;
}
else
return 0;
}
int Append_Element(NodeList L, int value)
{
if(!L)
return 0;
NodeList p = L;
while(p->next)
p = p->next;
p->next = (NodeList)malloc(sizeof(NodeList));
p->next->next=NULL;
p->next->value = value;
return 1;
}
int is_in_list(NodeList L, int cheak_value)
{
if(!L)
return 0;
NodeList p = L->next;
while(p)
{
if(p->value == cheak_value)
return 1;
p = p->next;
}
return 0;
}
int is_Empty(NodeList L)
{
return L->next?1:0;
}
int Find_Element(NodeList L, int find_value)
{
if(!L) return -2;
NodeList p=L->next;
int i,index=0;
while(p)
{
if(p->value == find_value)
return index;
index++;
p = p->next;
}
return -1;
}
int Invert(NodeList L)
{
if(!L)
return 0;
NodeList p=L->next;
NodeList p1=p->next;
NodeList temp;
while(p1 != NULL)
{
temp = p1->next;
p1->next = p;
p = p1;
p1 = temp;
}
L->next->next = NULL;
L->next=p;
return 1;
}
int Len_List(NodeList L)
{
if(!L)
return -1;
NodeList p=L->next;
int len=0;
while(p)
{
len++;
p=p->next;
}
return len;
}
int Clear_All_Element(NodeList L)
{
if(!L)
return 0;
NodeList p,q;
p = L->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
L->next = NULL;
return 1;
}
int Delete_List(NodeList * L)
{
if(*L==NULL)
return 0;
NodeList p=*L,temp;
while(p)
{
temp = p->next;
free(p);
p = temp;
}
*L=NULL;
return 1;
}