Linked List C implement (1)
////////////////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<malloc.h>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
typedef struct List_Node{
int info;
struct List_Node *next;
}node;//结点结构体
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/******************************/
/* 尾插法建立带头结点的单链表 */
/******************************/
node* Creat_Node()
{
node *head,*pre,*p;
int x;
head=(node*)malloc(sizeof(node));;
head->next=NULL;
pre=head;
printf("输入各结点的值,以0结束:");
while(EOF!=(scanf("%d",&x))&&x!=0)
{
p=(node*)malloc(sizeof(node));
p->info=x;
p->next=pre->next;
pre->next=p;
pre=pre->next;
}
return head;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/******************************/
/* 头插法建立带头结点的单链表 */
/******************************/
node* Build_Node()
{
node *head,*p;
int x;
head=(node*)malloc(sizeof(node));;
head->next=NULL;
printf("输入各结点的值,以0结束:");
while(EOF!=(scanf("%d",&x))&&x!=0)
{
p=(node*)malloc(sizeof(node));
p->info=x;
p->next=head->next;
head->next=p;
}
return head;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/******************************/
/* 打印单链表 */
/******************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Print_Node(node *head)
{
node *p=head->next;
printf("输出该链表:");
while(p)
{
printf("%-5d--->",p->info);
p=p->next;
}
if(p==NULL)
{
printf("^\n\n\n");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include"Head_Node.h"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int Count_Node(node *head)
{
node *p=head->next;
int num=0;
while(p!=NULL)
{
num++;
p=p->next;
}
return num;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
{
node *head;
head=Creat_Node();
Print_Node(head);
printf("结点个数为:%d\n",Count_Node(head));
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include"head_node.h"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/**********************************/
/* 删除重复 */
/**********************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Delete_Repeat_Node(node *head)
{
node *p,*pre,*s;
pre=head->next;
p=pre->next;
while(p)
{
s=p->next;
while(s&&s->info!=p->info)
{
s=s->next;
}
if(s)
{
pre->next=p->next;
free(p);
p=pre->next;
}
else
{
pre=p;
p=p->next;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
{
node *head;
head=Creat_Node();
Print_Node(head);
Delete_Repeat_Node(head);
Print_Node(head);
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include"Head_Node.h"
/************************************/
/* 在Y前插入X */
/************************************/
void Before_y_Insert_x(node* head,int y,int x)
{
node *pre,*p,*s;
pre=head;
p=pre->next;
while(p&&p->info!=y)
{
pre=p;
p=p->next;
}
if(p==NULL)
{
printf("error!%d不在该链表中\n",y);
}
else
{
s=(node*)malloc(sizeof(node));
s->info=x;
s->next=p;
pre->next=s;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
{
node *head;
int x,y;
head=Creat_Node();
printf("在y前插入x,输入y,x:");
scanf("%d%d",&y,&x);
Print_Node(head);
Before_y_Insert_x(head,y,x);
Print_Node(head);
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)