#include <stdio.h>
#include <stdlib.h>
#define MAN_OK 1
#define MAN_ERR 0
struct node
{
int num;
struct node * next;
};
typedef struct node Node;
typedef struct node * link;
void create_link(link * head)
{
*head = (link)malloc(sizeof(Node)) ;
(*head)->next = NULL;
}
int is_malloc_ok(link * new_node)
{
if(*new_node == NULL)
{
return MAN_ERR;
}
else
{
return MAN_OK;
}
}
void create_node(link * new_node,int i)
{
* new_node = (link)malloc(sizeof(Node));
is_malloc_ok(new_node);
while(*new_node == NULL)
{
*new_node = (link)malloc(sizeof(Node));
}
(*new_node)->num = i;
}
void insert_node_head(link head, link new_node)
{
new_node->next = head->next;
head->next = new_node;
}
void insert_node_tail(link head, link new_node)
{
link p;
p=head;
while(p->next != NULL)
{
p=p->next;
}
p->next = new_node;
new_node->next = NULL;
}
void insert_node_mid(link head, link new_node,int num_loc)
{
link p, q;
p=q=head->next;
if( head->next == NULL)
{
printf("link is empty!\n");
return ;
}
while(p->num != num_loc && p->next != NULL)
{
q=p;
p=p->next;
}
if( p->next == NULL && p->num != num_loc)
{
p->next = new_node;
new_node->next = NULL;
}
else
{
if(p == head->next )
{
new_node->next = head->next;
head->next = new_node;
}
else
{
q->next = new_node;
new_node->next = p;
}
}
}
void detele_node(link head, int num_val)
{
link p ,q;
q = p = head->next;
if( head->next == NULL)
{
printf("link is empty!\n");
return ;
}
while(p->num != num_val && p->next != NULL )
{
q = p;
p = p->next;
}
if(p->next == NULL && p->num != num_val)
{
printf("no such node!\n");
}
else
{
if( p == head->next)
{
head->next = p->next;
free(p);
}
else
{
q->next = p->next;
free(p);
}
}
}
void display_node(link head)
{
link p;
p=head->next;
if( p == NULL)
{
printf(“link is empty!\n”);
}
else
{
while( p->next != NULL)
{
printf(“num=%d\n”,p->num);
p=p->next;
}
}
}
void release_link(link * head)
{
link p;
p = *head;
if(p == NULL)
{
printf("link is empty!\n");
}
else
{
while(p->next != NULL)
{
*head = (*head)->next;
free(p);
p=*head;
}
}
}
int main()
{
link head = NULL ;
link new_node = NULL;
int i;
int num_val, num_loc;
create_link(&head);
for(i=0;i<10;i++)
{
create_node(&new_node,i);
insert_node_head(head,new_node);
//insert_node_tail(head,new_node);
}
/* printf(“please input loc:”);
scanf("%d",&num_loc);
printf(“please input val:”);
scanf("%d",&num_val);
create_node(&new_node,num_val);
insert_node_mid(head,new_node,num_loc);*/
printf("please input delete val:");
scanf("%d",&num_val);
create_node(&new_node,num_val);
detele_node(head,num_val);
display_node(head);
printf("\n\n");
release_link(&head);
display_node(head);
return 0;
}
2.无表头节点链表的倒序
void fun_link(link * head)
{
link p1, p2, p3;
p1=p2=p3=*head;
if(p1 == NULL)
{
printf("link is empty!\n");
return ;
}
if(p1->next == NULL)
{
return ;
}
p2 = (*head)->next;
if(p2->next == NULL)
{
p2->next = p1;
p1->next = NULL;
*head = p2;
return ;
}
p3 = p2->next;
while(p3->next != NULL)
{
p2->next = p1;
p1=p2;
p2=p3;
p3=p3->next;
}
p2->next = p1;
p3->next = p2;
(*head)->next = NULL ;
*head = p3;
}