天天看點

單連結清單按值和按序号查找

#include<malloc.h>;

#include<stdio.h>;

typedef struct node{

     int data;

     struct node *next;

}NODE;

NODE *locate(NODE *head,int value){  

      NODE *p;

      p=head->next;

      while((p!=NULL)&&(p->data!=value)){

             p=p->next;

      }

      return p;

    }

NODE *find(NODE *head,int index){

     NODE *p;

     int skip=1;

     p=head->next;

     while((p!=NULL)&&(index<skip)){

         p=p->next;

         skip++;

     }

     return p;

}

NODE *create(){   

  NODE *head,*p,*q;

  char ch;

  int a;

  head=(NODE*)malloc(sizeof(NODE));

  q=head;

  scanf("%d",&a);

  ch=getchar();

  while(ch!='?'){

    p=(NODE*)malloc(sizeof(NODE));

    p->data=a;

    q->next=p;

    q=p;

    scanf("%d",&a);

    ch=getchar();

  }

  q->next=NULL;

  return head;

}