好久都没有写博文了,刚实习的我,有一点忘记的代码的编写。这是进公司实习的第一道编程题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef _LINKLIST_
#define _LINKLIST_
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 10  //产生随机数的个数
typedef  int  ElemType;
//定义节点类型
typedef  struct  Node{
  ElemType data;
     struct  Node *pnext;
}Node;
Node *HeadInsertList();
Node *SeekTialList(Node *head,ElemType num);
#endif 
 
#include"LinkList.h"
Node *HeadInsertList(Node *head)
{
  
  Node *p = NULL;
  int  i = 0;
  srand ((unsigned  int ) time (0));  //随机数种子
     head = p = (Node *) malloc ( sizeof (Node)); 
     head-> data =  rand ()%101; 
     head-> pnext = NULL; 
     for (i=1;i<N;i++) 
    
         p = p->pnext = (Node *) malloc ( sizeof (Node)); 
         p->data =  rand ()%101; 
         p->pnext = NULL; 
    
       
     for (p = head;p;p=p->pnext) 
         printf ( "%d  " ,p->data); 
  printf ( "\n" );
  return  head;
}
Node *SeekTialList(Node *head,ElemType num)
{
  Node *p = NULL;
  Node *q = NULL;     
  p = q = head;           
     if (num<=0)         
   return  NULL;         
     while (num-1 > 0)     
     {
   if (q->pnext!= NULL)           
    q = q->pnext;         
         else            
    return  NULL;        
         num--;    
  }           
  while (q->pnext != NULL)     
  {      
   p = p->pnext;     
         q = q->pnext;   
    }      
    return  p;
}
int  main()
{
  Node *head = NULL;
  int  num = 0;
  head = HeadInsertList(head);
  printf ( "请输入倒数第几个" );
  scanf ( "%d" ,&num);
  if (NULL == SeekTialList(head,num))
  {
   printf ( "no" );
  }
  else
  {
   printf ( "%d" ,SeekTialList(head,num)->data);
  }
  printf ( "\n" );
  system ( "pause" );
}