|
源代码网推荐
源代码网整理以下2、插入 源代码网推荐对于双向循环链表,我们现在可以随意地在某已知结点p前或者p后插入一个新的结点。 源代码网推荐假若s,p,q是连续三个结点的指针,若我们要在p前插入一个新结点r,则只需把s的右链域指针指向r,r的左链域指针指向s,r的右链域指针指向p,p的左链域指针指向r即可。 源代码网推荐在p,q之间插入原理也一样。 源代码网推荐下面就是一个应用双向循环链表插入算法的例子: 源代码网推荐#include <stdio.h> 源代码网推荐#include <malloc.h> 源代码网推荐#include <string.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"); 源代码网推荐} 源代码网整理以下void insert(stud *p) 源代码网推荐{ 源代码网推荐char stuname[20]; 源代码网推荐stud *s; 源代码网推荐if((s= (stud *) malloc(sizeof(stud)))==null) 源代码网推荐{ 源代码网推荐printf("不能分配内存空间!"); 源代码网推荐exit(0); 源代码网推荐} 源代码网推荐printf("请输入你要插入的人的姓名:"); 源代码网推荐scanf("%s",stuname); 源代码网推荐strcpy(s->name,stuname); 源代码网推荐s->rlink=p->rlink; 源代码网推荐p->rlink=s; 源代码网推荐s->llink=p; 源代码网推荐(s->rlink)->llink=s; 源代码网推荐} 源代码网整理以下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\n",*&searchpoint->name); 源代码网推荐insert(searchpoint); 源代码网推荐print(head); 源代码网推荐} 源代码网推荐 源代码网供稿. |