List------Linked 链表

1、Definition

Linked list consists of a series of nodes. Each nodes contains the element and a pointer which points to the next node. The last node‘s next link points to NULL.

Linked=data+pointer

use the pointer to describe the logical relationship

2、Implementation

template<class List_entry>
class List
{
  public:
     List();
     int size();
     bool empty();
     bool full();
     ...
  protected:
     int count;
     Node<List_entry> *head;             //当声明一个linked时,只会拥有一个头指针
}

template<class List_entry>
struct Node
{
  List_entry entry;                            // data
  Node<List_entry> *next;               // point to the next node
  Node();
  Node(List_entry, Node<List_entry>*link=NULL);
}

3、Operations

(1)two types of insert

①在p节点所在位置插入

New(S)
 S->data=a;
 q->next=S;
 S->next=p;

②在p节点之后插入

New(S)
   S->data=a;
   S->next=p->next;
   p->next=S;

(2) create a linked list

In order to create a linked list, we have this algorithm

①To create a head

②To create new node S

③Insert S

void CreateList(Head)
{
   new(Head);
   Head->next=NULL;
   scanf("%c",ch);
   while(ch<>‘#‘)do
    {
       new(S);
       S->data=ch;
       S->next=Head->next;
       Head->next=S;
     }
}

上述构造方式为从头构造,在头元素出一个一个地插入

下面一种是从链尾增添

void CreateList(Head)
{
   new(Head);
   Head->next=NULL;
   Last=Head;
   scanf("%c",ch);
   while(ch<>‘#‘)do
   {
      new(S);
      S->data=ch;
      S->next=NULL;
      Last->next=S;
      Last=S;
    }
}

(3)insert an element at location i

Status ListInsert L(LinkList &L, int i, DataType e)
{
   LinkList p, s;
   p=L;
   int j=0;

   while(p&&j<i-1)
   {
      p=p->next;
      j++;
    }

   if(!p) return ERROR;
   s=(LinkList)malloc(sizeof(LNode));
   s->data=e;
   s->next=p->next;
   p->next=s;

   return OK;
}

(4)Delete

Status ListDelete(LinkList &L, int i)
{
   LinkList p, q;
   p=L;
   int j=0;

   while(p&&j<i-1)
   {
       p=p->next;
       j++;
    }

   if(!p) return ERROR;

    q=p->next;
    p->next=q->next;
    free(q);
}

(5)Search

①按位查找

Status ListSearch(LinkList &L, int i)
{
   LinkList p;
   int j=0;
   while(p&&j<i-1)
   {
      p=p->next;
      j++;
   }
   if(!p)return ERROR;
   e=p->next;
   return OK;
}

③按值查找

Status ListSearch(LinkList &L, DataType e)
{
   LinkList p;
   p=L;
   int j=0;
  while(p&&(p->next)!=e)
  {
         p=p->next;
         j++;
   }
   if(!p)
    {
        cout<<"Not found"<<endl;
        return ERROR;
    }
   else
    {
        return (j);
    }
}

3、circular linked list

4、Doubly linked list

(1)Insert

p->next=current->next;
p->prior=current;
current->next->prior=p;
current->next=p;

(2)Delete

current->next->prior=current->prior;
current->prior->next=current->next;

 

时间: 2024-08-07 08:37:15

List------Linked 链表的相关文章

Java 常用的几个算法(菜鸟初学)

[数组,数组列表,链表求最大值的算法比较(只是例子,可以举一反三)] 方法类 /** * 算法练习 * @author Jason * */ public class Methods { /** * 在数组中查找最大值算法(一般) * compareTo 不支持int 比较 所以要转成Integer * 22行 报错详情:Cannot invoke(包含) compareTo(int) on the primitive(原始的) type int */ public void SearchMax

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

234. 回文链表 Palindrome Linked List

Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? 判断一个链表是否为回文串 思路:1.找到中间点,2.反转后半部分链表,3.判断前半部分与后半部分是否相同 /** * Definition for singly-linked list. * public class ListNode { * public int v

LeetCode OJ:Intersection of Two Linked Lists(两个链表的插入)

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 找出第一个合并的节点的位置(或者说插入),注意复杂度为O(N),那么先遍历两个链

LeetCode OJ :Remove Linked List Elements (移除链表元素)

Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 ps:这一题感觉没什么技巧可言,选取一个头指针,一个当前指针,一个前向指针.简单的链表操作而已,代码如下: 1 /**

Leetcode 链表 Linked List Cycle II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Linked List Cycle II Total Accepted: 20444 Total Submissions: 66195My Submissions Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you

Leetcode:Reverse Linked List II 反转链表区间

Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given   1->2->3->4->5->NULL,  m = 2 and n = 4, return  1->4->3->2->5->NULL. Note:Given m, n satisfy the following

Chapter six Linked List &amp; Array(链表与数组)

1.reverse-nodes-in-k-group(k组翻转链表)[hard] 给你一个链表以及一个k,将这个链表从头指针开始每k个翻转一下.链表元素个数不是k的倍数,最后剩余的不用翻转. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution

160. 两个链表的相交点 Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

203. 移除链表中的元素 Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 题意:移除链表中指定val的元素 注意:考虑删除节点在尾部,以及连续删除两个相邻节点的情况 /** * Definit