#include
#include
typedef int datatype;
typedef struct _node_
{
datatype data;
struct _node_ *next;
} linknode, *linklist;
linklist CreateEmptyLinklist_1()
{
linklist h;
h = (linklist)malloc(sizeof(linknode));
h->next = NULL;
return h;
}
void CreateEmptyLinklist_2(linklist *h)
{
*h = (linklist)malloc(sizeof(linknode));
(*h)->next = NULL;
return;
}
int LengthLinklist(linklist h)
{
int length = 0;
h = h->next;
while (h != NULL)
{
length++;
h = h->next;
}
return length;
}
int EmptyLinklist(linklist h)
{
return (NULL == h->next);
}
void VisitLinklist(linklist h)
{
h = h->next;
while (h != NULL)
{
printf("%d ", h->data);
h = h->next;
}
return;
}
int LocateLinklist(linklist h, datatype x)
{
int pos = 0;
h = h->next;
while (h != NULL)
{
if (h->data == x) return pos;
pos++;
h = h->next;
}
return -1;
}
int InsertLinklist_1(linklist h, datatype x, int pos)
{
linklist p;
if ((pos < 0) || (pos > LengthLinklist(h))) return -1;
while ( pos-- ) h = h->next;
p = (linklist)malloc(sizeof(linknode));
p->data = x;
p->next = h->next;
h->next = p;
return;
}
void InsertLinklist_2(linklist h, datatype x)
{
linklist p = h, q;
q = (linklist)malloc(sizeof(linknode));
q->data = x;
while ((p->next != NULL) && (p->next->data < x)) p = p->next;
q->next = p->next;
p->next = q;
return;
}
int DeleteLinklist_1(linklist h, int pos)
{
linklist q;
if ((pos < 0) || (pos >= LengthLinklist(h))) return -1;
while ( pos-- ) h = h->next;
q = h->next;
h->next = q->next;
free(q);
return 0;
}
void DeleteLinklist_2(linklist h, datatype x)
{
linklist q = h->next;
while (NULL != q)
{
if (q->data == x)
{
h->next = q->next;
free(q);
}
else
{
h = q;
}
q = h->next;
}
return;
}
void ClearLinklist(linklist h)
{
linklist p = h->next, q;
while (p != NULL)
{
q = p;
p = p->next;
free(q);
}
h->next = NULL;
return;
}
int main()
{
int i;
linklist h, p, q;
h = CreateEmptyLinklist_1();
//CreateEmptyLinklist_2(&h);
p = h;
for (i=0; i<10; i++)
{
q = (linklist)malloc(sizeof(linknode));
q->data = i;
q->next = NULL;
p->next = q;
p = q;
}
p = h->next;
while (p != NULL)
{
printf("%d " ,p->data);
p = p->next;
}
printf("\n");
return 0;
}
阅读(556) | 评论(0) | 转发(1) |