首页 > 其他 > 详细

链表结点的删除(有重复)

时间:2018-08-03 11:24:55      阅读:133      评论:0      收藏:0      [点我收藏+]
#include<stdio.h> #include<stdlib.h> #define N 9 typedef struct node{    int  data;    struct node * next; }ElemSN; ElemSN  * Createlink(int a[],int n){            //逆向创建单向链表     int i;     ElemSN * h=NULL, * p;     for( i=N-1;i>=0;i--){        p=(ElemSN *)malloc(sizeof(ElemSN));        p->data =a[i];        p->next=h;        h=p;     }     return h;    }  void Printlink(ElemSN * h){    ElemSN * p;    for(p=h;p;p=p->next)   pintf("%2d\n",p->data);    }  ElemSN * DelSamenode(ElemSN*h,int key){    ElemSN * p,* q;  p=h;  while(p){              //p不为空   if(p->data!=key) {            //未找到key     q=p;            //两指针联动 p=p->next;   }                                   else{                         //key找到p指针指着key      if(p!=h){                  //判断是否为头结点 不是头结点        q->next=p->next;          free(p);          p=q->next;      }     else{                        //是头结点    h=h->next;   free(p);   p=h; }   } } return h;   } int main(void){  int a[]={3,2,9,8,9,7,9,6,1};  int key;      ElemSN * head;  head=Createlink(a,9);  printf("key=");  scanf("%2d",&key);      head=DelSamenode(head,key);  Printlink(head);  }


链表结点的删除(有重复)

原文:http://blog.51cto.com/13645380/2153862

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!