zoukankan      html  css  js  c++  java
  • 粗暴,干就完了----徐晓冬似的C语言自学笔记-----实现一个链表结构

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #define N 5
      4 /*N 假定数组长度为5*/
      5 typedef struct snode
      6 {
      7     int data;
      8     struct snode *next; 
      9 } SNODE;
     10 
     11 /*第一步,添加链表头信息*/
     12 SNODE *createhead(int a[])
     13 {
     14     SNODE *h,*p,*q;
     15 /*q为上一节点,p为当前节点*/
     16     int i;
     17     q=(SNODE *)malloc(sizeof(SNODE));
     18     h=q;
     19     for(i=0;i<N;i++)
     20      {
     21        p=(SNODE *)malloc(sizeof(SNODE));
     22        p->data = a[i];
     23        q->next=p;
     24        q=p;
     25      }
     26     q->next=0;
     27     return h;
     28 }
     29 
     30 /*第二步打印链表中的元素*/
     31 void showstars(SNODE *h)
     32 {
     33     SNODE *p;
     34     p=h->next;
     35     while(p)
     36      {
     37        printf("%d 其地址为 %x
    ",p->data,p);
     38        p=p->next;
     39      }
     40 }
     41 
     42 
     43 /*第三步删除/回收节点*/
     44 void revokeresource(SNODE *h)
     45 {
     46     SNODE *p,*q;
     47     p=h->next;
     48     while(p)
     49      {
     50     q=p->next;
     51       printf("哦,no~~~%d被回收了
    ",p->data);
     52       free(p);  
     53       p=q;
     54      }
     55    free(h);
     56 }
     57 
     58 /*添加一个节点,目标在值为4的结点前添加一个结点,如果值为4的节点不存在,则在末尾添加一个结点*/
     59 void insert(SNODE *h,int a,int d)
     60 {
     61     SNODE *p,*q;
     62     SNODE *s;
     63     s=(SNODE *)malloc(sizeof(SNODE));
     64     s->data=d;
     65     q=h;p=h->next;
     66     while(p)
     67      {
     68        if(p->data==a) break;
     69        q=p;p=p->next;
     70      }
     71     s->next=q->next;
     72     q->next=s;
     73     showstars(h);
     74 }
     75 
     76 /*删除值为targetdta的链接节点*/
     77 void collectnode(int targetdata,SNODE *h)
     78 {
     79     SNODE *p,*q;
     80     p=h->next;
     81     q=h;
     82     while (p)
     83      {
     84         if(p->data==targetdata)
     85           {
     86            break;
     87           }
     88         q=p;p=p->next;
     89      }
     90     if(p)
     91      {
     92          q->next=p->next;
     93          free(p);
     94      }
     95 }
     96 
     97 void main()
     98 {
     99     int saiwa[N]={1,2,3,4,5};
    100     SNODE *head;
    101     head=createhead(saiwa);
    102     showstars(head);
    103     printf("================*在数组的值4前插入节点值8=================
    ");
    104     insert(head,4,8);
    105      
    106     printf("================*回收节点4=================
    ");
    107     collectnode(4,head);
    108     showstars(head);
    109 }

     输出结果:

  • 相关阅读:
    WINDOWS操作系统中可以允许最大的线程数
    OCP-1Z0-新051-61题版本-36
    OCP-1Z0-新051-61题版本-37
    OCP-1Z0-新051-61题版本-38
    OCP-1Z0-新051-61题版本-39
    OCP-1Z0-新051-61题版本-40
    OCP-1Z0-新051-61题版本-31
    OCP-1Z0-新051-61题版本-32
    OCP-1Z0-新051-61题版本-33
    OCP-1Z0-新051-61题版本-34
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/11231302.html
Copyright © 2011-2022 走看看