zoukankan      html  css  js  c++  java
  • 一个例子说明线性链表的简单操作

    单向链表中的结点结构如下:

    typedef struct node
    {
        char info;
        struct node *link;  
    }NODE;

          其中数据域存放线性表中元素的值,指针域保存指向下一个元素的指针(即下一个元素的地址)。链表中至少需要一个头指针head和表头节点。

    其中head是指向向前链表表头的指针,表头是一个特殊的结点,它的数据域不存放普通的数据,而是闲置不用或存放特殊信息,表头节点的指针域存放链表的第一个结点的地址,当单向链表为空时,表头结点的指针域为空值NULL。

        例子:利用单向链表实现集合运算(A-B)U(B-A)。题意分析:即是要分别创建两个链表,在将其中相同的元素删除,不同的元素进行插入操作。

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 tyoedef struct node
      5 {
      6     char info;                         /*store the data of the  element*/
      7     struct node *link;              /*store the address of the next element*/
      8 }NODE;
      9 
     10 /******************************************************************************
     11 **function:    listCreate
     12 **description:create a list
     13 **input:         no
     14 **return:       reurn the head pointer
     15 ******************************************************************************/
     16 NODE *listCreate ()
     17 {
     18     NODE *head, *p1, *p2;
     19     cahr ch;
     20     head = (NODE *)malloc(sizeof(NODE));      /*create the head node*/
     21     head->link = NULL;
     22     p1 = head;
     23     while((ch = getchar()) != '
    ')
     24     {
     25         p2 = (NODE *)malloc(sizeof(NODE));    /*create a new node   */
     26         p2->info = ch;
     27         p2->link = NULL; 
     28 
     29         p1->link = p2;                        /*insert the new node */
     30         p1 = p2;
     31     }  
     32     return (head);
     33 }
     34 
     35 /***************************************************************************
     36 **function:    listTravel
     37 **description:travel the element of the list
     38 **input:         the pointer to the node
     39 **return:       void
     40 **************************************************************************/
     41 void listTravel(NODE *ptr)
     42 {
     43     ptr = ptr->link;
     44     while(ptr != NULL)
     45     {
     46         printf("%c ", ptr->info);
     47         ptr = ptr->link;            /*enable the ptr point to the next node*/
     48     }
     49     printf("
    ");
     50 }
     51 
     52 /********************************************************************
     53 **function:    listProcess
     54 **description:process the data of list
     55 **input:         two node point la,lb
     56 **return:       reurn a new pointer of the list
     57 *******************************************************************/
     58 NODE *listProcess(NODE *la, NODE *lb)
     59 {
     60     NODE *ptr, *p1, *p2;
     61     int found = 0;
     62 
     63     ptr = lb->link;
     64     while(ptr != NULL)
     65     {
     66         p1 = la->link;
     67         p2 = la;
     68         while(p1 != NULL && !found)
     69         {
     70         if(p1->info == ptr->info)              /*if the two datas equals*/
     71             {
     72                 found = 1;
     73             }
     74             else                               /*point the next element*/
     75             {    
     76                 p2 = p1;
     77                 p1 = p1->link;
     78             }
     79          }
     80             if(found)                          /*delete the node */
     81             {
     82                 p2->link = p1->link;
     83                 free(p1);
     84                 found = 0;
     85             }
     86             else                               /*insert the node */
     87 
     88             {
     89                 p1 = (NODE *)malloc(sizeof(NODE));
     90                 p1->info = ptr->info;
     91                 p1->link = NULL;
     92                 p2->link = p1;
     93             }     
     94         
     95         ptr = ptr->link;                      /*compare the next element */
     96     }
     97     return (la);
     98 }
     99 
    100 /*******************************************************************
    101 **function:    main
    102 **description:complete the task
    103 **input:         void
    104 **return:        0
    105 *******************************************************************/
    106 int main(int argc, char *argv[])
    107 {
    108     NODE *la, *lb;
    109     
    110     printf("
    the element of the list A:
     ");
    111     la = listCreate();
    112     listTravel(la); 
    113 
    114     printf("
    the element of the list B:
     ");
    115     lb = listCreate();
    116     listTravel(lb); 
    117 
    118     la = process(la, lb);
    119     printf("the list (A-B)U(B-A) is
    ");
    120     listTravel(la); 
    121 
    122     return 0;
    123 }
  • 相关阅读:
    DS博客作业03--树
    C博客作业05--指针
    C博客作业04--数组
    C博客作业03--函数
    C博客作业02--循环结构
    顺序分支结构
    我的第一篇博客
    第1次任务:购物车程序的面向对象设计
    5-互评-OO之接口-DAO模式代码阅读及应用
    第三周-面向对象基础与类的识别-自主学习任务
  • 原文地址:https://www.cnblogs.com/philospy/p/4020270.html
Copyright © 2011-2022 走看看