@renkejun1942 求一个链表逆序的泛型函数~
@renkejun1942最近做到广搜时打算在队尾用一个指针链接到队头来标记其遍历状态~以便于回溯搜索~不过这样打印出来的结果会是逆序的~现在有必要进行把的到数据进行逆序输出~当然也有其它处理方法例如交换搜索起点和终点~但这样究竟还是有很大的局限性~当然我自己也可以尝试敲敲~不过最近编程和学习还有别的任务就没啥时间去弄了~~如果有时间麻烦能不能弄个链表逆序的泛型函数~拜托了
~~~
程序代码:
List *
ReverseList( List *RootP )
{
Node *current;
Node *next;
for( current = NULL; NULL != RootP; RootP = next )
{
next = RootP->link;
RootP->Link = current;
current = RootP;
}
return current;
}
[此贴子已经被作者于2017-4-24 22:01编辑过]

[此贴子已经被作者于2017-4-24 22:28编辑过]

程序代码://测试:
#include <stdio.h>
#include "GenericityList.h"
int
COM( void *a, void *b );
void
PRINT( void * );
struct a
{
int a;
double b;
char c;
};
int
main( void )
{
List Root;
struct a b;
int ix;
Root = NULL;
for( ix = 0; ix < 20; ++ix )
{
b.a = 'a' + ix;
b.b = 'a' + ix;
b.c = 'a' - ix;
Insert( &Root, &b, sizeof( struct a ), COM );
}
Print( Root, PRINT );
return 0;
}
int
COM( void *a, void *b )
{
return 1;
}
void
PRINT( void *a )
{
printf("%d %c %lf",( int )( ( ( struct a * )a )->a ),( char )( ( ( struct a * )a )->b ),( double )( ( ( struct a * )a )->c ) );
}
程序代码://接口:
#ifndef _GENERICITY_LIST_
#define _GENERICITY_LIST_
struct List{
void *Element;
struct List *Link;
};
typedef struct List *List;
void
Insert( List *RootPP, void *Value, int size , int (*compare)( void *, void * ) );
void
Print( List RootP, void (*)( void * ) );
#endif
程序代码://实现:
#include "GenericityList.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
void
Insert( List *RootPP, void *Value, int size , int (*compare)( void *, void * ) )
{
List Next;
List NewCell;
while( NULL != ( Next = *RootPP ) && 0 < compare( Next->Element, Value ) )
RootPP = &Next->Link;
NewCell = ( List )malloc( sizeof( struct List ) );
assert( NULL != NewCell );
NewCell->Element = malloc( size );
assert( NULL != NewCell->Element );
memmove( NewCell->Element, Value, size );
NewCell->Link = Next;
*RootPP = NewCell;
}
void
Print( List RootP, void (*print)( void * ) )
{
while( NULL != RootP )
{
print( RootP->Element );
RootP = RootP->Link;
printf( "\n" );
}
}
[此贴子已经被作者于2017-4-24 23:43编辑过]

程序代码:#include<stdlib.h>
#include<string.h>
#include<assert.h>
//PTYPE 链表指针类型 NAME 函数名称 LEN 链表容量大小 NEXT链表指针名称
#define MAKE(PTYPE,NAME,LEN,NEXT) \
\
void Creat_Node_##NAME(PTYPE* p) \
{ \
*p=(PTYPE)malloc(LEN); \
assert(*p!=NULL); \
memset(*p,0,LEN); \
} \
\
void Del_One_##NAME(PTYPE* p) \
{ \
if (*p==NULL) \
return ; \
\
free(*p); \
*p=NULL; \
} \
void Del_Link_##NAME(PTYPE* p) \
{ \
PTYPE t=*p; \
while (t=t->##NEXT) \
{ \
Del_One_##NAME(p); \
*p=t; \
} \
\
*p=NULL; \
}
程序代码:#include"Link.h"
#include<stdio.h>
typedef struct Node
{
int n;
struct Node* next;
}Node,*PNode;
MAKE(PNode,S,sizeof(Node),next)
PNode Creat_S(PNode p);
void Print(PNode p);
int main()
{
PNode head=NULL;
Creat_Node_S(&head);
head=Creat_S(head);
Print(head);
Del_Link_S(&head);
return 0;
}
PNode Creat_S(PNode head)
{
int n=0;
PNode p=NULL;
PNode t=NULL;
if (head==NULL)
Creat_Node_S(&head);
p=head;
while (p->next)
p=p->next;
while (scanf("%d",&n)!=EOF)
{
Creat_Node_S(&t);
t->n=n;
p=p->next=t;
}
return head;
}
void Print(PNode p)
{
if (p==NULL||p->next==NULL)
{
puts("这是一张空表!");
return ;
}
while (p=p->next)
printf("%-3d",p->n);
puts("");
}

