单链表

1.简介

线性结构用于描述数据元素之间的线性关系,即数据元素一个接一个的排列,主要用于描述具有单一前驱和后继的数据关系。线性表是一种线性结构,它有顺序存储和链式存储两种方法。线性表的链式存储用结点存储数据元素,元素结点可以连续,也可以不连续,因此,存储数据元素的同时必须存储元素的逻辑关系。结点的结构如下:

其中,前面为数据域,后面为指针域。若节点中只有一个指针域,则称为单链表(线链表)。在链式存储结构中,只需一个指针指向头一个结点,就可以顺序访问表中任意元素。所以,引入一个不存储元素的结点,称为头结点,并令头指针指向该结点。

2.实例

2.1单链表的前置条件

 1 struct Node           //定义结点
 2 {
 3     int data;              //数据域
 4     Node *next;        //指针域
 5 };
 6 class LinkList
 7 {
 8 public:
 9     LinkList(int a[], int n);        //建立有n个元素的单链表
10     int Length();                     //求单链表长度
11     void PrintList();                 //遍历单链表,按序号依次输出各元素
12     void Insert(int i, int x);      //在单链表的指定位置插入指定数据
13     int Get(int i);                    //获取单链表指定位置的元素
14 
15     Delete(int i)                      //删除指定位置的元素
16 private:
17     Node *first;                     //单链表的头指针
18 };

2.1单链表的创建

 1 LinkList::LinkList(int a[], int n)      //头插法
 2 {
 3     first = new Node;              //申请头结点
 4     first->next = NULL;          //初始化一个空链表
 5     for (int i = 0; i<n; i++)
 6     {
 7         Node *s;              
 8         s = new Node;                   //申请新结点
 9 
10         s->data = a[n-1-i];            //给结点数据域赋值
11         s->next = first->next;       //这两句将新结点插入到前一个结点之前
12         first->next = s;
13     }
14 } 

注:first->next=NULL;申请结点p,则p->next=NULL(语句一),first->next=p(语句二);

     first->next=p;申请结点t,则t->next=p(语句一),first->next=t(语句二);

     first->next=t;申请结点s,则s->next=t(语句一),first->next=s(语句二);

最终,指针表示形式为:first->s->t->p->NULL.

 1 LinkList::LinkList(int a[], int n)      //尾插法
 2 {
 3     first = new Node;
 4     Node *r;
 5     r=first;                 //r为终端结点,初始值为头结点
 6     for (int i = 0; i<n; i++)
 7     {
 8         Node *s;
 9         s = new Node; 
10         s->data = a[i];      //为每个数组元素建立一个结点
11         r->next = s;       
12         r= s;                //s插入到终端结点之后
13     }
14     r->next = NULL;    //建表完成后,终端结点的指针域为空
15 }

2.2单链表的遍历

 1 void LinkList::PrintList()
 2 {
 3     Node *p;                  
 4     p = first->next;           //结点p初始指向头结点的后继结点
 5     while (p != NULL)         
 6     {
 7         cout << p->data << "  ";       //输出对应结点的数据
 8         p = p->next;                       //结点p变为p的后继结点
 9     }
10     cout << endl;
11 }

2.3单链表的插入

 1 void LinkList::Insert(int i, int x)            //在第i个位置插入x
 2 {
 3     Node *p;         
 4     Node *s; 
 5     p = first;                               //设p为头结点
 6     int count = 1;                     
 7     while (p != NULL&&count<i )   
 8     {
 9         p = p->next;           //循环直至p为指定位置的前一个结点
10         count++;
11     }
12     if (p == NULL)throw"位置";
13     else {
14         s = new Node;              //申请新结点
15         s->data = x;                //为结点s的数据域赋值
16         s->next = p->next;      //新结点的指针指向结点p的后继结点,即位置i上的结点
17         p->next = s;                //原先位置i-1上的结点指针改为指向新结点
18     }
19 }

2.4单链表的查询

 1 int LinkList::Get(int i)           //获取第i个位置上的元素
 2 {
 3     Node *p;
 4     p=first->next;             //p初始为第一个结点
 5     int count=1;
 6     while(p!=NULL&&count<i)
 7     {
 8         p=p->next;        //结点后移
 9         count++;
10     }
11     if(p==NULL)throw"位置";
12     else 
13         return p->data;
14 }

2.5单链表的长度

 1 int LinkList:: Length( )       //获取单链表的长度
 2 {
 3     Node *p;
 4     p=first->next;
 5     int count=0;
 6     while(p!=NULL)
 7     {
 8         p=p->next;
 9         count++;
10     }
11     return count;
12 }

2.6单链表的删除

 1 int LinkList::Delete(int i)
 2 {
 3     Node *p;
 4     p = first;               
 5     int count = 1;
 6     while (p != NULL&&count<i)
 7     {
 8         p = p->next;
 9         count++;
10     }
11     if (p == NULL || p->next == NULL)
12         throw"位置";
13     else
14     {
15         Node *q;
16         q = p->next;
17         p->next = q->next;
18         return q->data;
19     }
20 }
21 
22 2.6

主函数

 1 void main()
 2 {
 3     int r[5] = { 1, 9, 7, 2, 5 };
 4     LinkList L(r, 5);
 5     cout << "线性表的数据为:" << endl;
 6     L.PrintList();                 
 7     cout << "线性表的数据个数为:" << endl;
 8     cout << L.Length() << endl;
 9     cout << "线性表的第四个数据为:" << endl;
10     cout << L.Get(4) << endl;
11     cout << "线性表的第二个位置插入4:" << endl;
12     L.Insert(2, 4);
13     L.PrintList();
14     cout << "删除第3个位置的数据:" << endl;
15     cout << L.Delete(3) << endl;
16     L.PrintList();
17 }

原文地址:https://www.cnblogs.com/jfl-xx/p/4890116.html