单链表查找第i个节点
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 typedef struct Node 5 { 6 char a; 7 struct Node* next; 8 }Node,*list; 9 void p(list L) 10 { 11 Node *s; 12 char c; 13 int f=1; 14 while(f) 15 { 16 c=getchar(); 17 if(c!='$') 18 { 19 getchar(); 20 s=(Node*)malloc(sizeof(Node)); 21 s->a=c; 22 s->next=L->next; 23 L->next=s; 24 } 25 else 26 f=0; 27 } 28 } 29 Node *get(list L,int i) 30 { 31 int j=0; 32 list p; 33 p=L; 34 if(i<0) 35 return 0; 36 while((p->next!=NULL)&&(j<i)) 37 { 38 p=p->next; 39 j++; 40 } 41 if(i==j) 42 return p; 43 else 44 return 0; 45 } 46 int main() 47 { 48 list L,s; 49 L=(Node*)malloc(sizeof(Node)); 50 L->next=NULL; 51 p(L); 52 s=L->next; 53 while(s->next!=NULL) 54 { 55 printf("%c ",s->a); 56 s=s->next; 57 } 58 printf("%c\n",s->a); 59 int n; 60 scanf("%d",&n); 61 Node *r; 62 r=(Node*)malloc(sizeof(Node)); 63 if(get(L,n)!=NULL) 64 { 65 r=get(L,n); 66 printf("%c\n",r->a); 67 } 68 return 0; 69 }
见贤思齐焉见不贤而内自省也