zoukankan      html  css  js  c++  java
  • 链表

     进入了大二,也刚好是开了数据结构这门课,虽然大一也接触过一些数据结构,但还是对很多都不怎么懂,有些懂了也不知道怎么实现。

    今天在课上看了关于链表的一些东西,理解了,然后之后就回寝室写了这个链表

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 
     5 struct note{
     6     int x;
     7     note *next;
     8 };
     9 
    10 typedef struct note *parToNote;
    11 typedef parToNote List;
    12 typedef parToNote position;
    13 
    14 List MakeEmpty(List L);
    15 int IsEmpty(List L);
    16 position Find(int x,List L);
    17 void Delete(int x,List L );
    18 void Insert(int x,List L,position p);
    19 void DeleteList(List L);
    20 position FindPervious(int x,List L);    //这个是找x的前驱节点,用于删除。
    21 bool Islast(List L);
    22 
    23 int IsEmpty(List L)
    24 {
    25     return L->next==NULL;
    26 }
    27 
    28 List MakeEmpty(List L)
    29 {
    30     position p , tmp;
    31     p = L->next;
    32     free(L);
    33     while(p!=NULL)
    34     {
    35         tmp = p->next;
    36         free(p);
    37         p = tmp;
    38     }
    39 }
    40 
    41 position Find(int x, List L)
    42 {
    43     position p;
    44     p = L;
    45     while(p!=NULL&&p->x != x)
    46         p = p->next;
    47     return p;
    48 }
    49 
    50 position FindPervious(int x,List L)
    51 {
    52     position p;
    53     p = L->next;
    54     while(p!=NULL&&p->next->x != x )   //这个和Find的区别就在一个是找出当前节点,一个是找出前驱节点。
    55         p = p -> next;
    56     return p;
    57 }
    58 bool Islast(List L)
    59 {
    60     return L->next==NULL;
    61 }
    62 void Delete(int x,List L)
    63 {
    64     position tmp,p;
    65     p = FindPervious(x,L);
    66     if(!Islast(p))    //当p时最后一个节点时,也就是说明x根本就不在链表中,也就没必要删除。
    67     {
    68         tmp = p -> next;
    69         p ->next = tmp -> next;
    70         free(tmp);
    71     }
    72 }
    73 
    74 void Insert(int x,List L,position p)
    75 {
    76     position tmp;
    77     tmp = (position)malloc( sizeof( struct note ) );
    78     if(tmp == NULL)
    79         printf("out of space
    ");
    80     tmp->x = x;
    81     tmp -> next = p->next;
    82     p->next = tmp;
    83 }
    84 
    85 void MakeEmpty(List L)
    86 {
    87     position p,tmp;
    88     p = L->next;
    89     L->next = NULL;
    90     while(p==NULL)
    91     {
    92         tmp = p->next;
    93         free(p);
    94         p = tmp;
    95     }
    96 }
  • 相关阅读:
    tomcat部署web应用的4种方法
    Spring连接数据库的几种常用的方式
    Spring 的两个配置容器的讲解
    c3p0详细配置
    tomcat启动很慢的原因
    J2EE程序员应该要掌握的linux知识
    hibernate-mapping的各种属性配置
    在Eclipse添加Android兼容包( v4、v7 appcompat )
    微信抢红包软件-android
    Gradle sync failed: Failed to find Build Tools revision 26.0.2的解决办法
  • 原文地址:https://www.cnblogs.com/Tree-dream/p/5857341.html
Copyright © 2011-2022 走看看