chrd123w吧 关注:5贴子:159
  • 0回复贴,共1
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
node *next;
}node;
node* creat(int n)
{
node *h,*p,*r;
int i;
h=NULL;
for (i=0;i<n;i++) {
p=(node*)malloc(sizeof(node));
p->data=n-i;
p->next=NULL;
p->next=h;
h=p;
}
return h;
}
void out(node *h)
{
while (h!=NULL) {
printf("%d ",h->data);
h=h->next;
}
printf("\n");
}
int getElem(node *h,int k)
{
int i;
for (i=1;i<k;i++) {
h=h->next;
}
return h->data;
}
void del(node *h)
{
node *p;
while (h!=NULL) {
p=h->next;
free(h);
h=p;
}
}
int main()
{
node *h;
int k;
scanf("%d",&k);
h=creat(k);
out(h);
scanf("%d",&k);
printf("%d ",getElem(h,k));
del(h);
out(h);
}


IP属地:北京1楼2015-12-03 09:10回复