一个例子说明线性链表的简单操作

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

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()) != ‘\n‘)
 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("\n");
 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("\nthe element of the list A:\n ");
111     la = listCreate();
112     listTravel(la);
113
114     printf("\nthe element of the list B:\n ");
115     lb = listCreate();
116     listTravel(lb);
117
118     la = process(la, lb);
119     printf("the list (A-B)U(B-A) is\n");
120     listTravel(la);
121
122     return 0;
123 }
时间: 2024-07-29 13:24:48

一个例子说明线性链表的简单操作的相关文章

单链表的简单操作

单链表是一种最简单的线性表的链式存储结构,单链表又称线性链表,每个数据元素用一个结点来存储,结点分为存放数据元素的data和存放指向链表下一个结点的指针next. 链表实现:(LinkList.h) //单链表 #ifndef LINKLIST_H_ #define LINKLIST_H_ #include <iostream> //using namespace std; template <typename T> struct Node { //数据成员 T data; Nod

链表的简单操作-----删除,添加,查找(Xcode)

#include <stdio.h> #include "shou_note.h" struct student *create()  /*这个括号是什么意思?*/ { //创建 头指针 尾指针 新指针 struct student *phead=NULL; struct student *pend =NULL; struct student *pnew =NULL; int i=0;    //循环变量 //建立链表及第一个结点 //因为第一个结点是头结点,所以要放在循环

对带头结点的单链表的简单操作

#pragma once #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<memory.h> #define DataType int           //int 可以改写为其它数据类型 typedef struct Node { DataType data; struct Node *next; }Node,*pNode;          //定义结点结构体      

C语言----------链表的简单操作

#include <stdio.h> #include <malloc.h> typedef struct node{ //定义节点类型 char data; //数据域 struct node *next; //指针域 }linklist; linklist* Create(){ //创建链表 char key; linklist *phead; //头指针 linklist *pnew; //新节点 linklist *pend; //尾指针 phead = (linklist

关于链表的一些简单操作

终于上黄金了.. 然后就是一波2连败... 最近 完全不想做题啊  一做题 就想碎觉啊 郁闷死了 根据书本 写了点关于单向链表的简单操作 可能还存在点小bug---先放它一马吧 以后可能再进行补充关于它的操作 毕竟还有好多 先慢慢找回敲键盘打代码的感觉 厌 1 /* 2 线性表之单向链表的一些常见操作 3 */ 4 #include <iostream> 5 using namespace std; 6 7 typedef int ElemType; 8 typedef struct LNod

从新定义线性链表及其基本操作

链表在空间的合理利用上和插入.删除时不需要移动等优点,因此在很多场合下,它是线性表的首先储存结构.然而它也存在着实现某些基本操作,如求线性表的长度时不如顺序储存结构的特点.因而从新定义线性链表及其基本操作 头文件: #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define MYOVERFLOW -2 typedef int Status; typedef int Elemty

实际应用中带头节点的线性链表

/*========带头节点的线性链表类型=========*/ typedef char ElemType //结点类型 typedef struct LNode { char data; struct LNode *next; }*Link,*Position; //链表类型 typedef struct { Link head,tail; int len; }LinkList; /*======================================================

线性链表其他种类(静态,双向,循环)的存储结构和常见操作

一.静态单链表 在不支持动态空间分配的环境中,要使用链表存储数据,那么可采用静态链表的方法:即在一块预分配的存贮空间中,用下标作为指针链来构成链式结构. //既然是静态链表,那么可以使用一维数组实现存储,java没有指针,那么就用这来使用链表结构 //在不支持动态空间分配的环境中,要使用链式结构技术,可采用静态链表的方法:即在一块预分配的存贮空间中,用下标作为指针. //存储结构:在数组中增加一个“指针”域,存放下一元素在数组中的下标.且0为代表空指针 //设S为SLinkList型变量,若第i

创建单线性链表的不同表示方法和操作

创建单线性链表,常见的有头插法.尾插法创建线性链表,常见的操作有:创建链表.查找.删除.添加元素.求逆链等操作. 这里首先用头插法创建链表: //头指针唯一确定一个单链表 #define MaxSize 15 typedef int elem_type ; typedef struct linklist { elem_type data; struct linklist *next; } Linklist; //头插入法建立链表:每次将头结点的指针赋值给新结点,然后将新节点赋值给头结点指针 Li