69,369
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct link_node
{
datatype info;
struct link_node * next;
}node;
node * init_link_list()//初始化单链表
{
return NULL;
}
void print_link_list(node *head)//打印单链表各个节点值
{
node *p;
p=head;
if(!p)
{
printf("link list is empty!\n");
exit(1);
}
else
{
printf("link list every value is :\n");
while(p)
{
printf("%5d",p->info);
p=p->next;
}
printf("\n");
}
}
node *find_num_link_list(node * head,datatype x)//查找值为x的节点
{
node *p;
p=head;
if(!p)
{
printf("link list is empty!\n");
exit(1);
}
while(p && p->info!=x)
p=p->next;
return p;
}
node *find_pos_link_list(node *head,int i)//查找第i个节点
{
int j=1;
node *p;
p=head;
if(i<1)
{
printf("there is no %d position!\n",i);
exit(1);
}
while(p && i!=j)
{
j++;
p=p->next;
}
return p;
}
node *insert_in_front_link_list(node *head,datatype x)//插入一个值为x的节点作为单链表的第一个节点
{
node *q;
q=(node*)malloc(sizeof(node));
q->info=x;
q->next=head;
head=q;
return head;
}
node *insert_x_after_y_link_list(node *head,datatype y,datatype x)//在值为y的节点后插入一个值为x的节点
{
node *p,*q;
p=find_num_link_list(head,y);
if(!p)
{
printf("con not find %d\n",y);
exit(1);
}
q=(node*)malloc(sizeof(node));
q->info=x;
q->next=p->next;
p->next=q;
return head;
}
node *insert_x_after_i_link_list(node *head,int i,datatype x)//在第i个节点后插入值为x的节点
{
node *p,*q;
p=find_pos_link_list(head,i);
if(!p)
{
printf("there is no %d position\n",i);
exit(1);
}
q=(node*)malloc(sizeof(node));
q->info=x;
q->next=p->next;
p->next=q;
return head;
}
node *delete_num_link_list(node *head,datatype x)//删除值为x的节点
{
node *pre=NULL,*p;
if(!head)
{
printf("link list is empty!\n");
exit(1);
}
p=head;
while(p && p->info!=x)
{
pre=p;
p=p->next;
}
if(!pre && p->info==x)
{
head=head->next;
}
else if(p!=NULL)
{
pre->next=p->next;
}
else
{
printf("error!\n");
exit(1);
}
free(p);
return head;
}
node * delete_pos_link_list(node * head,int i)//删除第i个节点
{
node *p,*q=NULL;
if(i==1)
{
p=head;
head=head->next;
}
else
{
p=find_pos_link_list(head,i);
if(!p)
{
printf("con not find %d\n",i);
exit(1);
}
else
{
q=find_pos_link_list(head,i-1);
q->next=p->next;
}
}
free(p);
return head;
}
int main()
{
node *list;
list=init_link_list();
list=insert_in_front_link_list(list,1);
list=insert_in_front_link_list(list,2);
list=insert_in_front_link_list(list,3);
list=insert_in_front_link_list(list,4);
list=insert_in_front_link_list(list,5);
list=insert_in_front_link_list(list,6);
print_link_list(list);
insert_x_after_y_link_list(list,1,9);
print_link_list(list);
insert_x_after_y_link_list(list,6,59);
print_link_list(list);
insert_x_after_i_link_list(list,3,100);
print_link_list(list);
insert_x_after_i_link_list(list,7,220);
print_link_list(list);
delete_num_link_list(list,100);
print_link_list(list);
delete_pos_link_list(list,5);
print_link_list(list);
delete_pos_link_list(list,1);
print_link_list(list);
return 0;
}
void delete_pos_link_list(node *& head,int i)//ɾ³ýµÚi¸ö½Úµã
{
node *p,*q=NULL;
if(i==1)
{
p=head;
head=head->next;
}
else
{
p=find_pos_link_list(head,i);
if(!p)
{
printf("con not find %d\n",i);
exit(1);
}
else
{
q=find_pos_link_list(head,i-1);
q->next=p->next;
}
}
free(p);
// return head;
}