**************************链表****************************
#单链表,静态链表,循环链表,双向链表。
#***单链表:
单个结点 :分为数据域和指针域
头结点:并不是所有链表都有头结点、头结点数据域不保存数据。
头指针:保存头结点地址的指针称为头指针。
struct student
{ //数据域
char name[20];
int age;
char sex;
//指针域
struct student *next
};
插入结点步骤:
1.移动指针(指向要插入结点的前一个结点地址)
2.分配空间
3.操作指针
头文件
#ifndef _LinkList_h
#define _LinkList_h
#define SUCCESS 11111
#define FAILURE 22222
#define TRUE 33333
#define FALSE 44444
typedef int ElemType;
struct Node
{
ElemType data; // 数据域
struct Node *next;// 指针域
};
typedef struct Node node;
int LinkInit(node **l);
int LinkInsrt(node *l,int n,ElemType e );
int LinkTraverse(node* l, void(*p)(ElemType));
int LinkReverse(node *l);
int LinkLength(node* l);
int LinkEmpty(node *l);
int GetElem(node *l,int p,ElemType *e);
int LinkLocateElem(node *l,ElemType e,int(*p)(ElemType,ElemType));
int LinkListDetele(node *l,int p,ElemType *e);
int LinkClear(node *l);
int LinkDestory(node **l);
#endif
功能函数
#include<stdio.h>
#include"LinkList.h"
#include<stdlib.h>
int LinkInit(node **l)
{
*l = (node*)malloc(sizeof(node)*1);// 给头结点分配空间,l 是头指针。
if( NULL == *l)
{
return FAILURE;
}
(*l)->next = NULL;
return SUCCESS;
}
int LinkInsrt(node *l,int n,ElemType e )//n为插入的位置,从第一个后面开始插入
{
node *p = l,*q;
q = (node*)malloc(sizeof(node)*1);
int k = 1;//用于计数
if( NULL == l)
{
return FAILURE;
}
while ( k < n && NULL != p)
{
p = p->next;
k++;
}
//循环结束时, k必然等于n
if( k > n && NULL == p)
{
return FAILURE;
}
q ->data = e; //数据域
q ->next = p->next;
p ->next = q;
return SUCCESS;
}
int LinkTraverse(node* l, void(*p)(ElemType))
{
if( NULL == l)
{
return FAILURE;
}
node* q = l;
while( q->next )
{
q = q->next;
p(q ->data);
}
printf("\n");
return SUCCESS;
}
int LinkReverse(node *l)
{
if( NULL == l )
{
return FAILURE;
}
node* p = l->next;
l->next = NULL;
while( p != NULL )
{
node* q = p;
p = p->next;
q->next = l->next;
l->next = q;
}
return SUCCESS;
}
int LinkLength(node* l)
{
if( NULL == l)
{
return FAILURE;
}
int ret = 0;
node* p = l->next;
while(p)
{
ret++;
p = p->next;
}
return ret;
}
int LinkEmpty(node *l)
{
int ret;
ret = ( NULL == l->next)?TRUE:FALSE;
return ret;
}
int GetElem(node *l,int p,ElemType *e)
{
if( NULL == l || p < 1)
{
return FAILURE;
}
node* q = l;
int i;
for( i = 0; i < p && q != NULL ; i++)//循环p次,同时满足q不为空,
//因为如果p过大,则会直接移动到最后一个 (NULL) 。
{
q = q->next;
}
if( !q ) // 如果q不是空指针
{
return FAILURE;
}
*e = q->data; // q已经指向第p个结点;
return SUCCESS;
}
int LinkLocateElem(node *l,ElemType e,int(*p)(ElemType,ElemType))
{
if( NULL == l)
{
return FAILURE;
}
node* q = l;
int location = 0;
while( q -> next )
{
q = q->next;
location++;
if ( TRUE == p(e,q->data) )
{
return location;
}
}
return FAILURE;
}
int LinkListDetele(node *l,int p,ElemType *e)
{
int k = 1;
node* q = l;
node* n;//用于先保存要删除的指针地址
if( NULL == l)
{
return FAILURE;
}
while ( k < p && NULL != q)
{
q = q->next;
k++;
}
//循环结束时, k必然等于n
if( k > p && NULL == q)
{
return FAILURE;
}
n = q -> next; //先把 要被删除的地址存入n
*e = n -> data; //保存 要被删除的地址里的值
q ->next = n -> next; //将指针指向下下个地址
free ( n ) ; //释放掉被删除原先的地址
return SUCCESS;
}
int LinkClear(node *l)
{
node* p = l;
if( NULL == l )
{
return FAILURE;
}
while( p )
{
l->next = p->next;
free( p );
p = l->next;
}
return SUCCESS;
}
int LinkDestory(node **l)
{
if( NULL == l )
{
return FAILURE;
}
free(*l);
(*l) = NULL;
return SUCCESS;
}
主函数
#include<stdio.h>
#include"LinkList.h"
#include<time.h>
#include<stdlib.h>
void print(ElemType e)
{
printf("%d ",e);
}
int equal(ElemType e1,ElemType e2)
{
int ret;
ret = ( e1 == e2 )? TRUE:FALSE;
return ret;
}
int main()
{
node *first = NULL; //first 为头指针。
int ret,i;
srand(time(NULL));
ret = LinkInit(&first);
if( ret == SUCCESS)
{
printf("Init success!\n");
}
else if( FAILURE == ret)
{
printf("Init failure!\n");
}
for( i = 0; i < 10 ; i++)
{
ret = LinkInsrt(first, i /*+1*/,rand()%10 );
if( SUCCESS == ret)
{
printf("insrt success!\n");
}
if( FAILURE == ret)
{
printf("failure!\n");
}
}
ret = LinkTraverse(first,print);
if( SUCCESS == ret)
{
printf("traverse success!\n");
}
if( FAILURE == ret)
{
printf("traverse failure!\n");
}
ret = LinkReverse(first);
if( SUCCESS == ret )
{
printf("reverse success!\n");
}
if( FAILURE == ret )
{
printf("reverse failure!\n");
}
ret = LinkTraverse(first,print);
if( SUCCESS == ret)
{
printf("traverse success!\n");
}
if( FAILURE == ret)
{
printf("traverse failure!\n");
}
ret = LinkLength(first);
if( FAILURE == ret )
{
printf("no length!\n");
}
else
{
printf("the length is %d\n",ret);
}
ret = LinkEmpty(first);
if( ret == TRUE )
{
printf("list is empty!\n");
}
else if( ret == FALSE)
{
printf("list isn't empty!\n");
}
int p = 3;
ElemType e;
ret = GetElem(first,p,&e);
if( FAILURE == ret )
{
printf("Get Elem Failure!\n");
}
else
{
printf("the %dth elem is the number:'%d'\n",p,e);
}
ElemType x = 4;
ret = LinkLocateElem(first,x,equal);
if( FAILURE == ret )
{
printf("the number:'%d' has'nt been found!\n",x);
}
else
{
printf("the number '%d' is the %dth number\n",x,ret);
}
int y = 5;
ret = LinkListDetele(first,y,&e);
if( FAILURE == ret)
{
printf("detele failure!\n");
}
else
{
printf("detele success!(the %dth number has been deteled)\n",y);
}
ret = LinkTraverse(first,print);
if( SUCCESS == ret)
{
printf("traverse success!\n");
}
if( FAILURE == ret)
{
printf("traverse failure!\n");
}
ret = LinkClear(first);
if( SUCCESS == ret )
{
printf("clear success!\n");
}
else if( FAILURE == ret )
{
printf("clear failure!\n");
}
ret = LinkDestory(&first);
if( FAILURE == ret )
{
printf("destory failure!\n");
}
else if( SUCCESS == ret )
{
printf("destory success!\n");
}
for( i = 0; i < 10 ; i++)
{
ret = LinkInsrt(first, i+1,rand()%10 );
if( SUCCESS == ret)
{
printf("insrt success!\n");
}
if( FAILURE == ret)
{
printf("failure!\n");
}
}
return 0;
}
我得多敲几次。