zoukankan      html  css  js  c++  java
  • 数据结构之__链表

    首先,定义数诀结构listg.h

     1 #ifndef listg_h
     2 #define listg_h
     3 #include <stdlib.h>
     4 #include <stdio.h>
     5 
     6 typedef int DataType;
     7 
     8 typedef struct node_{
     9     DataType data;
    10     struct node_ *next;
    11 } Node;
    12 
    13 typedef struct list_{
    14     Node *head;
    15     Node *tail;
    16     Node *current;
    17 } List;
    18 
    19 void initList(List *);
    20 void addHead(List *, DataType);
    21 void addTail(List *, DataType);
    22 void delNode(List *, DataType);
    23 Node *getNode(List *, DataType);
    24 int getLength(List *);
    25 void dispList(List *);
    26 
    27 #endif

    接着,实现头文件定义的函数listg.c

      1 #include "listg.h"
      2 
      3 void initList(List *list){
      4     list->head = NULL;
      5     list->tail = NULL;
      6     list->current = NULL;
      7 
      8     return;
      9 }
     10 
     11 void addHead(List *list, DataType data){
     12     //1、创建一个节点
     13     Node *node = (Node *)malloc(sizeof(Node));
     14     node->data = data;
     15     node->next = NULL;
     16 
     17     //2、插入节点准备
     18     if(list->head == NULL){
     19         list->tail = node;
     20     }else{
     21         node->next = list->head;
     22     //3、插入节点
     23     }
     24     list->head = node;
     25 
     26     return;
     27 }
     28 
     29 void addTail(List *list, DataType data){
     30     //1
     31     Node *node = (Node *)malloc(sizeof(Node));
     32     node->data = data;
     33     node->next = NULL;
     34 
     35     //2
     36     if(list->head == NULL){
     37         list->head = node;
     38     }else{
     39         list->tail->next = node;
     40     }
     41 
     42     //3
     43     list->tail = node;
     44 
     45     return;
     46 }
     47 
     48 void delNode(List *list, DataType data){
     49     Node *prev = list->head;
     50     Node *node = prev->next;
     51 
     52     while(node != NULL){
     53         if(node->data == data){
     54             prev->next = prev->next->next;
     55         }else{
     56             prev->next = node;
     57         }
     58     }
     59 }
     60 
     61 Node *getNode(List *list, DataType data){
     62     Node *node = (Node *)malloc(sizeof(Node));
     63     node = list->head;
     64 
     65     while(node != NULL){
     66         if(data ==node->data){
     67             return node;
     68         }else{
     69             node = node->next;
     70         }
     71     }
     72 
     73     return NULL;
     74 }
     75 
     76 int getLength(List *list){
     77     Node *node = (Node*)malloc(sizeof(Node));
     78     node = list->head;
     79     int i = 0;
     80     while(node != NULL){
     81         node = node->next;
     82         i++;
     83     }
     84 
     85     return i;
     86 }
     87 
     88 void dispList(List *list){
     89     Node *node = (Node *)malloc(sizeof(Node));
     90     node = list->head;
     91     int i = 0;
     92     while(node != NULL){
     93         printf("the %dth node: %d
    ", i + 1, node->data);
     94         node = node->next;
     95         i++;
     96     }
     97     printf("display finished
    ");
     98 
     99     return;
    100 }

    最后,测试文件testListg.c

     1 #include "listg.h"
     2 
     3 int main(int argc, char **argv)
     4 {
     5     List *list = (List *)malloc(sizeof(List));
     6     initList(list);
     7     addHead(list, 4);
     8     addHead(list, 6);
     9     addHead(list, 8);
    10     addHead(list, 10);
    11     dispList(list);
    12     printf("the list: %d
    ", getLength(list));
    13 
    14     return 0;
    15 }

    测试结果没有什么问题

  • 相关阅读:
    B1028人口普查
    B1004成绩排名
    B1041考试座位号
    A1009 Product of Polynomials多项式相乘
    A1002 A+B for Polynomials 多项式相加
    B1010一元多项式求导
    A1065 A+Band C(64 bit)
    A1046 Shortest Distance 最短路径
    排序
    windows 平台使用wireshark命令行抓包
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/13916556.html
Copyright © 2011-2022 走看看