-
#include <stdio.h>
-
#include <stdlib.h>
-
-
/**/
-
typedef struct _list_node_{
-
int val;
-
struct _list_node_ *next_node;
-
}list_node_t;
-
-
-
list_node_t *Creat_head_node()
-
{
-
list_node_t *head_node = (list_node_t *)malloc(sizeof(list_node_t));
-
if(head_node == NULL)
-
{
-
printf("malloc Failed\n");
-
return NULL;
-
}
-
-
/**/
-
head_node->next_node = NULL;
-
return head_node;
-
}
-
-
/**/
-
list_node_t *Creat_node(int val)
-
{
-
list_node_t *new_node = (list_node_t *)malloc(sizeof(list_node_t));
-
if(new_node == NULL)
-
{
-
printf("malloc Failed\n");
-
return NULL;
-
}
-
-
new_node->val = val;
-
new_node->next_node = NULL;
-
return new_node;
-
}
-
-
/*insert list in head*/
-
int Insert_list_head(list_node_t *pHead, int val)
-
{
-
list_node_t *pNew_node = NULL;
-
-
/**/
-
pNew_node = Creat_node(val);
-
if(pNew_node == NULL)
-
{
-
printf("Creat node Failed\n");
-
return -1;
-
}
-
pNew_node->next_node = pHead->next_node;
-
pHead->next_node = pNew_node;
-
-
return 0;
-
}
-
-
/**/
-
int Delete_node(list_node_t *pHead, int val)
-
{
-
list_node_t *p = NULL;
-
list_node_t *q = NULL;
-
-
q = pHead;
-
p = pHead->next_node;
-
while(p != NULL)
-
{
-
if(p->val == val)
-
{
-
printf("delete a node\n");
-
q->next_node = p->next_node;
-
free(p);
-
p = q->next_node;
-
continue;
-
}
-
-
q = p;
-
p = p->next_node;
-
}
-
-
return 0;
-
}
-
-
/*1:is empty 0:is not empty*/
-
int Is_empty_list(list_node_t *pHead)
-
{
-
if(pHead->next_node == NULL)
-
{
-
return 1;
-
}
-
else
-
{
-
return 0;
-
}
-
}
-
-
/**/
-
int Get_length_of_list(list_node_t *pHead)
-
{
-
list_node_t *pCur_node = NULL;
-
int length = 0;
-
-
/**/
-
pCur_node = pHead->next_node;
-
while(pCur_node != NULL)
-
{
-
length ++;
-
pCur_node = pCur_node->next_node;
-
}
-
-
return length;
-
}
-
-
/**/
-
void Print_list(list_node_t *pHead)
-
{
-
list_node_t *pCur_node = NULL;
-
-
/**/
-
pCur_node = pHead->next_node;
-
while(pCur_node != NULL)
-
{
-
printf("%d ", pCur_node->val);
-
pCur_node = pCur_node->next_node;
-
}
-
printf("\n");
-
}
-
-
-
/**/
-
int main(void)
-
{
-
list_node_t *pHead = NULL;
-
int length = -1;
-
-
/**/
-
pHead = Creat_head_node();
-
if(pHead == NULL)
-
{
-
printf("Creat_head_node Failed\n");
-
return -1;
-
}
-
-
/**/
-
Insert_list_head(pHead, 7);
-
Insert_list_head(pHead, 5);
-
Insert_list_head(pHead, 48);
-
Insert_list_head(pHead, 46);
-
Insert_list_head(pHead, 46);
-
Insert_list_head(pHead, 46);
-
Insert_list_head(pHead, 48);
-
Insert_list_head(pHead, 46);
-
length = Get_length_of_list(pHead);
-
printf("length:%d\n", length);
-
Print_list(pHead);
-
-
/**/
-
Delete_node(pHead, 46);
-
Print_list(pHead);
-
-
return 0;
-
}
执行结果:
46 48 46 46 46 48 5 7
delete a node
delete a node
delete a node
delete a node
48 48 5 7