您的位置:首页 > 其它

单链表的基本功能 与扩展功能的实现

2012-07-21 12:34 357 查看
/*
* List.h
*
*  Created on: 2012-7-20
*      Author: Mr.Li */
 */
 
#ifndef LIST_H_
#define LIST_H_

#include<stdio.h>
#include<stdlib.h>
#define TURE 1
#define FALSE 0
typedef struct link{
 int data;
 struct link *next;
}Link,*pLink;
void  CreateLink(pLink H); //声明创建单链表函数
void  PrintLink(pLink H); //声明打印单链表函数
pLink GetLink(pLink H,int x); //声明给定位置找单链表中的具体节点的函数
void  LocateLink(pLink H,int x); //声明给定值寻找在单链表中的位置
void  DeleteLink(pLink H,int x); //声明删除具体位置的节点
int   LengthLink(pLink H); //声明求单链表的长度函数
void  Insert(pLink H,int x,int y); //声明在具体的位置插入某个数
int   IsEmpty(pLink H); //声明判断单链表是否为空
void  FREE(pLink H); //声明释放单链表函数

--------------------------------------------- 单链表的扩展操作--------------------------------------------------------
pLink CatLink(pLink H,pLink S); //声明连接两个单链表函数
void CopyLink(pLink H,pLink S); //声明复制一个单链表的函数
void ResetLink(pLink H); //声明倒置单链表的函数
pLink CutLink(pLink H,int addr); //声明单链表分割函数
 
#endif /* LIST_H_ */
/*
* main.c
*
* Created on: 2012-7-20
* Author: Mr.Li*/
#include"List.h"
int main()
{
#if 1
pLink H= (pLink)malloc(sizeof(Link));
H->next=NULL;
CreateLink(H); //create a link;
PrintLink(H);

#endif

#if 0
int n; //give the add to get the value;
printf("please input the locate:");
scanf("%d",&n);
pLink H2;
H2=GetLink(H,n);
printf("the locate %d of value is %d\n",n,H2->data);
#endif

#if 0
printf("\nplease input the value:"); //give the value to get the locate
int value;
scanf("%d",&value);
LocateLink(H,value);
#endif

#if 0
int address; //delete one connect
printf("\nplease input the address you want to delete:");
scanf("%d",&address);
DeleteLink(H,address);
printf("\nafer delete as you want the new list is :");
PrintLink(H);
#endif

#if 0
int leng=0;
leng=LengthLink(H); //get the length of the list
printf("the length of the list is %d\n",leng);
#endif

#if 0
address=0;value=0; //Insert a word
printf("\nplease input the value you want to insert:");
scanf("%d",&value);
printf("\nplease input the address you want to insert:");
scanf("%d",&address);
Insert(H,value,address);
printf("\nafter do as you want the new list is:");
PrintLink(H);
#endif
#if 0
printf("it is ok now");
value = IsEmpty(H);//clear the list
if(value == 1)
printf("\nthe list is not empty\n");
else
printf("\nthe list is empty\n");
#endif

#if 0
FREE(H);
printf("\nit is ok now");
#endif
//PrintLink(H);

/*-----------------单链表的连接-----------------*/
#if 1
pLink S = (pLink)malloc(sizeof(Link));
S->next = NULL;
CreateLink(S);
CatLink(H,S);
PrintLink(H);
#endif

/*-----------------单链表的复制-----------------*/
#if 0
pLink S = (pLink)malloc(sizeof(Link));
S->next = NULL;
CopyLink(H,S);
PrintLink(S);
#endif

/*-----------------单链表的倒置-----------------*/
#if 0
ResetLink(H);
PrintLink(H);
#endif

/*-----------------单链表的分割-----------------*/
#if 0
int addr;
printf("\nplease input the addr you cut the list:");
scanf("%d",&addr);

if(addr == 1){
printf("\nAfter cut the list,new list: NULL\n");
}
else{
CutLink(H,addr);
printf("\nAfter cut the list,new list:\n");
PrintLink(H);
}
#endif
return 0;
}
/*
* List.c
*
*  Created on: 2012-7-20
*      Author: Mr.Li
*/

#include"List.h"
#if 1
void CreateLink(pLink H)                          //create the list  创建新链表
{
pLink r = H;

int a;
printf("please input numb:");
scanf("%d",&a);

while(a != -1)
{
pLink new=(pLink)malloc(sizeof(Link));
new->data = a;
new->next = NULL;

r->next= new;
r = r->next;

printf("please input numb:");
scanf("%d",&a);
}
}
#endif

#if 1
void PrintLink(pLink H)                 //print the list       打印链表
{
pLink r = H->next;
while(r->next!=NULL)
{
printf("%d\t",r->data);
r=r->next;
}
printf("%d\t",r->data);
printf("\n");
}
#endif

#if 0                                    //get the length of the list 求单链表的长度
int LengthLink(pLink H)
{
pLink p = H;
int lon = 0;
while(p->next != NULL)
{
p = p->next;
lon++;
}
return lon;
}
#endif

#if 0
pLink GetLink(pLink H,int x)            //give the add  to get value给具体的位置,求在单链表中的值
{
if(x == 0)
return H;
if(x < 0)
return NULL;

int i = 0;
pLink p = H;
while(p->next != NULL&& i< x)
{
p=p->next;
i++;
}
if(i == x)
return p;
else
return NULL;
}

#endif

#if 0
void LocateLink(pLink H,int val)                         //给出具体的值,求在单链表中的位置
{

pLink p = H->next;
int loc=1;
while(p->data!= val && p->next!= NULL)
{
p = p->next;
loc++;
}

if(p->data == val)
printf("\nthe value %d is locate in %d",val,loc);
else
printf("\nthe value is not in this list");
}

#endif

#if 0                                      //delete删除单链表的某个节点
void DeleteLink(pLink H,int x)
{

pLink p ,r,q;
if(x >0 && x > LengthLink(H))
printf("you enter is erro:");
else
{
r=GetLink(H,x-1);
p=GetLink(H,x);

q = p->next;
free(p);
p->next = NULL;
r->next = q;
}
}
#endif

#if 0
void Insert(pLink H,int x,int y)                           //在具体的y位置插入x的值
{
if(y<0 && y> LengthLink(H))
printf("Error");
else{
pLink p,r;
pLink new= (pLink)malloc(sizeof(Link));
new->data = x;
new->next = NULL;
r = GetLink(H,y);
p= GetLink(H,y-1);
p->next = new;
new->next= r;
}
}
#endif

#if 0                                                  //判断单链表是否为空
int IsEmpty(pLink H)
{
if(H->next!= NULL)
return TURE;
else
return FALSE;
}

#endif

#if 0                                                 //释放堆空间
void FREE(pLink H)
{
if(LengthLink(H)==0)
{
free(H);
H->next = NULL;
}
else
{
pLink p,q;
p= H->next;
while(p->next != NULL)
{
q = p->next;
free(p);
p->next = NULL;
p = q;
}
free(p);
p->next = NULL;
free(H);
H->next=NULL;
}
}

#endif

/*----------------单链表的扩展操作---------------*/
/*----------------单链表的连接-----------------*/
#if 1
pLink CatLink(pLink H,pLink S)
{
pLink p = H;
while(p->next){
p = p->next;
}
p->next = S->next;
//free(S);
S->next = NULL;
return H;
}
#endif

/*----------------单链表的复制-----------------*/
#if 0
void CopyLink(pLink H,pLink S)
{
pLink p = H;
pLink r = S;
while(p->next){
pLink k=(pLink)malloc(sizeof(Link));
k->data = p->next->data;
k->next = NULL;
r->next = k;
p = p->next;
r = r->next;
}
}
#endif

/*----------------单链表的倒置-----------------*/
#if 0
void ResetLink(pLink H)
{
pLink p,r,m;
r = H; p= H;
int i,no;
no = LengthLink(H);
for(i=1;i< no;i++)
{
while(p->next->next)
p=p->next;
m= p->next;
p->next = NULL;
m->next=r->next;
r->next = m;
p=H;
r = r->next;
}
}
#endif

/*----------------单链表的分割-----------------*/
#if 0
pLink CutLink(pLink H,int addr){
pLink p,R;
R= (pLink)malloc(sizeof(Link));
R->next = NULL;
p=GetLink(H,addr-1);
R->next = p->next;
p->next = NULL;
FREE(R);
return H;
}
#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息