|
源代码网推荐
源代码网整理以下双向链表的基本运算: 源代码网推荐1、查找 源代码网推荐假若我们要在一个带表头的双向循环链表中查找数据域为一特定值的某个结点时,我们同样从表头结点往后依次比较各结点数据域的值,若正是该特定值,则返回指向结点的指针,否则继续往后查,直到表尾。 源代码网推荐下例就是应用双向循环链表查找算法的一个程序。 源代码网推荐#include <stdio.h> 源代码网推荐#include <malloc.h> 源代码网推荐#define n 10 源代码网整理以下typedef struct node 源代码网推荐{ 源代码网推荐char name[20]; 源代码网推荐struct node *llink,*rlink; 源代码网推荐}stud; 源代码网整理以下stud * creat(int n) 源代码网推荐{ 源代码网推荐stud *p,*h,*s; 源代码网推荐int i; 源代码网推荐if((h=(stud *)malloc(sizeof(stud)))==null) 源代码网推荐{ 源代码网推荐printf("不能分配内存空间!"); 源代码网推荐exit(0); 源代码网推荐} 源代码网推荐h->name[0]="\0"; 源代码网推荐h->llink=null; 源代码网推荐h->rlink=null; 源代码网推荐p=h; 源代码网推荐for(i=0;i<n;i++) 源代码网推荐{ 源代码网推荐if((s= (stud *) malloc(sizeof(stud)))==null) 源代码网推荐{ 源代码网推荐printf("不能分配内存空间!"); 源代码网推荐exit(0); 源代码网推荐} 源代码网推荐p->rlink=s; 源代码网推荐printf("请输入第%d个人的姓名",i+1); 源代码网推荐scanf("%s",s->name); 源代码网推荐s->llink=p; 源代码网推荐s->rlink=null; 源代码网推荐p=s; 源代码网推荐} 源代码网推荐h->llink=s; 源代码网推荐p->rlink=h; 源代码网推荐return(h); 源代码网推荐} 源代码网整理以下stud * search(stud *h,char *x) 源代码网推荐{ 源代码网推荐stud *p; 源代码网推荐char *y; 源代码网推荐p=h->rlink; 源代码网推荐while(p!=h) 源代码网推荐{ 源代码网推荐y=p->name; 源代码网推荐if(strcmp(y,x)==0) 源代码网推荐return(p); 源代码网推荐else p=p->rlink; 源代码网推荐} 源代码网推荐printf("没有查找到该数据!"); 源代码网推荐} 源代码网整理以下void print(stud *h) 源代码网推荐{ 源代码网推荐int n; 源代码网推荐stud *p; 源代码网推荐p=h->rlink; 源代码网推荐printf("数据信息为:\n"); 源代码网推荐while(p!=h) 源代码网推荐{ 源代码网推荐printf("%s ",&*(p->name)); 源代码网推荐p=p->rlink; 源代码网推荐} 源代码网推荐printf("\n"); 源代码网推荐} 源代码网整理以下main() 源代码网推荐{ 源代码网推荐int number; 源代码网推荐char studname[20]; 源代码网推荐stud *head,*searchpoint; 源代码网推荐number=n; 源代码网推荐clrscr(); 源代码网推荐head=creat(number); 源代码网推荐print(head); 源代码网推荐printf("请输入你要查找的人的姓名:"); 源代码网推荐scanf("%s",studname); 源代码网推荐searchpoint=search(head,studname); 源代码网推荐printf("你所要查找的人的姓名是:%s",*&searchpoint->name); 源代码网推荐} 源代码网推荐 源代码网供稿. |