天天看點

單連結清單删除與插入

#include<stdio.h>

typedef struct node{

     int data;

     struct node *next;

}NODE;

void insert(NODE *head,NODE *p,int value){

  NODE *q;

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

  q->data=value;

  if(head!=NULL){

     q->next=p->next;

     p->next=q;

   }else{

     head=q;

     q->next=NULL;

   }

}

void delete(NODE *head,int value){

    NODE *p,*q;                  

    q=head;

    p=q->next;

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

            q=p;

            p=p->next;

    }

    if(p==NULL){

       printf("not found");

    }else{

       q->next=p->next;

       free(p);

    }

}