#include"list.h"
#include<stdio.h>
{
link head,cur;
int input;
head=(link)malloc(sizeof(node));//申请头结点
if(head==NULL)
{
printf("malloc error\n");
exit(0);
}
head->next=NULL;
printf("please input data:\n");
scanf("%d",&input);
while(input)
{
cur=(link)malloc(sizeof(node));//为新的节点申请空间
cur->data=input;
//头插法,让新来的节点有所指向
cur->next=head->next;
head->next=cur;
scanf("%d",&input);
}
return head;
}
void print_Link(link head)//打印链表
{
head=head->next;
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf("\n");
}
link insert_Link(link head)//插入节点
{
link cur;
int input;
printf("please input what you want to insert :\n");
scanf("%d",&input);
while(input)
{
cur=(link)malloc(sizeof(node));
cur->data=input;
cur->next=head->next;
head->next=cur;
scanf("%d",&input);
}
return head;
}
link search(link head)//查找节点
{
int find;
printf("please input what you want to find:\n");
scanf("%d",&find);
head=head->next;
while(head!=NULL)
{
if(head->data==find)
return head;
head=head->next;
}
return NULL;
}
link del_link(link head,link pfind)//删除节点
{
link p;
p=head;
while(p->next!=pfind)//前一个节点的后继等于pfind,把pfind 的后继赋给起一个节点的后继
{
p=p->next;
}
p->next=pfind->next;
free(pfind);
return head;
}
int count_link(link head)//统计链表长度
{
int count=0;
while(head->next!=NULL)
{
count++;
head=head->next;
}
return count ;
}
link sort_link(link head,int size)//冒泡排序,每趟都从首节点开始
{
int j,i;
link p,q,list,temp;
for(i=0;i<size-1;i++)
{
list=head;
p=list->next;
q=p->next;
for(j=0;j<size-i-1;j++)
{
if(p->data>q->data)
{
list->next=q;
p->next=q->next;
q->next=p;
//交换指针所指,P,Q 的位置发生变化,利用TEMP交换,保持原有的顺序
temp=p;
p=q;
q=temp;
}
list=list->next;
p=p->next;
q=q->next;
}
}
return head;
}
void destory_Link(link head)//销毁链表,使两个指针指向链表,一个后移,一个销毁
{
link p;
while(head!=NULL)
{
p=head;
head=head->next;
free(p);
}
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}
link create_Link()//创建链表{
link head,cur;
int input;
head=(link)malloc(sizeof(node));//申请头结点
if(head==NULL)
{
printf("malloc error\n");
exit(0);
}
head->next=NULL;
printf("please input data:\n");
scanf("%d",&input);
while(input)
{
cur=(link)malloc(sizeof(node));//为新的节点申请空间
cur->data=input;
//头插法,让新来的节点有所指向
cur->next=head->next;
head->next=cur;
scanf("%d",&input);
}
return head;
}
void print_Link(link head)//打印链表
{
head=head->next;
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf("\n");
}
link insert_Link(link head)//插入节点
{
link cur;
int input;
printf("please input what you want to insert :\n");
scanf("%d",&input);
while(input)
{
cur=(link)malloc(sizeof(node));
cur->data=input;
cur->next=head->next;
head->next=cur;
scanf("%d",&input);
}
return head;
}
link search(link head)//查找节点
{
int find;
printf("please input what you want to find:\n");
scanf("%d",&find);
head=head->next;
while(head!=NULL)
{
if(head->data==find)
return head;
head=head->next;
}
return NULL;
}
link del_link(link head,link pfind)//删除节点
{
link p;
p=head;
while(p->next!=pfind)//前一个节点的后继等于pfind,把pfind 的后继赋给起一个节点的后继
{
p=p->next;
}
p->next=pfind->next;
free(pfind);
return head;
}
int count_link(link head)//统计链表长度
{
int count=0;
while(head->next!=NULL)
{
count++;
head=head->next;
}
return count ;
}
link sort_link(link head,int size)//冒泡排序,每趟都从首节点开始
{
int j,i;
link p,q,list,temp;
for(i=0;i<size-1;i++)
{
list=head;
p=list->next;
q=p->next;
for(j=0;j<size-i-1;j++)
{
if(p->data>q->data)
{
list->next=q;
p->next=q->next;
q->next=p;
//交换指针所指,P,Q 的位置发生变化,利用TEMP交换,保持原有的顺序
temp=p;
p=q;
q=temp;
}
list=list->next;
p=p->next;
q=q->next;
}
}
return head;
}
void destory_Link(link head)//销毁链表,使两个指针指向链表,一个后移,一个销毁
{
link p;
while(head!=NULL)
{
p=head;
head=head->next;
free(p);
}
}