原文:

给定一个大小为n的单链表和另一个键k,我们必须找到单链表中存在键k的概率。

示例

输入list = 2 -> 3 -> 3 -> 3 -> 4 -> 2, key = 5

输出:0

说明

由于列表中不存在值为 5 的键,因此在链表中找到键的概率为 0。

输入list = 2 -> 3 -> 5 -> 1 -> 9 -> 8 -> 0 -> 7 -> 6 -> 5, key = 5

输出:0.2

方法

在单链表中找到关键元素k的概率如下:

概率等于元素k的出现次数/链表的大小

在我们的方法中,我们将首先计算单链表中元素k的数量,然后通过将k的出现次数除以单链表的大小来计算概率。

以下是上述方法的实现:

c

// c code to find the probability
// of finding an element
// in a singly linked list
#include 
#include 
// link list node
struct node {
    int data;
    struct node* next;
};
/* given a reference (pointer to pointer)
   to the head of a list and an int,
   push a new node on the front of the list. */
void push(struct node** head_ref, int new_data)
{
    // allocate node
    struct node* new_node
        = (struct node*)malloc(
            sizeof(struct node));
    // put in the data
    new_node->data = new_data;
    // link the old list off the new node
    new_node->next = (*head_ref);
    // move the head to point to the new node
    (*head_ref) = new_node;
}
// counts nnumber of nodes in linked list
int getcount(struct node* head)
{
    // initialize count
    int count = 0;
    // initialize current
    struct node* current = head;
    while (current != null) {
        count  ;
        current = current->next;
    }
    return count;
}
float kpresentprobability(
    struct node* head,
    int n, int k)
{
    // initialize count
    float count = 0;
    // initialize current
    struct node* current = head;
    while (current != null) {
        if (current->data == k)
            count  ;
        current = current->next;
    }
    return count / n;
}
// driver code
int main()
{
    // start with the empty list
    struct node* head = null;
    // use push() to construct below list
    // 1->2->1->3->1
    push(&head, 2);
    push(&head, 3);
    push(&head, 5);
    push(&head, 1);
    push(&head, 9);
    push(&head, 8);
    push(&head, 0);
    push(&head, 7);
    push(&head, 6);
    push(&head, 5);
    printf("%.1f",
           kpresentprobability(
               head, getcount(head), 5));
    return 0;
}

java

// java code to find the probability
// of finding an element
// in a singly linked list
class gfg{
// link list node
static class node 
{
  int data;
  node next;
};
// given a reference (pointer to 
// pointer) to the head of a list 
// and an int, push a new node 
// on the front of the list.
static node push(node head_ref, 
                 int new_data)
{
  // allocate node
  node new_node = new node();
  // put in the data
  new_node.data = new_data;
  // link the old list 
  // off the new node
  new_node.next = head_ref;
  // move the head to 
  // point to the new node
  head_ref = new_node;
  return head_ref;
}
// counts nnumber of nodes 
// in linked list
static int getcount(node head)
{
  // initialize count
  int count = 0;
  // initialize current
  node current = head;
  while (current != null) 
  {
    count  ;
    current = current.next;
  }
  return count;
}
static float kpresentprobability(node head,
                                 int n, int k)
{
  // initialize count
  float count = 0;
  // initialize current
  node current = head;
  while (current != null) 
  {
    if (current.data == k)
      count  ;
    current = current.next;
  }
  return count / n;
}
// driver code
public static void main(string[] args)
{
  // start with the empty list
  node head = null;
  // use push() to conbelow list
  // 1.2.1.3.1
  head = push(head, 2);
  head = push(head, 3);
  head = push(head, 5);
  head = push(head, 1);
  head = push(head, 9);
  head = push(head, 8);
  head = push(head, 0);
  head = push(head, 7);
  head = push(head, 6);
  head = push(head, 5);
  system.out.printf("%.1f", kpresentprobability(
                     head, getcount(head), 5));
}
}
// this code is contributed by shikhasingrajput

python3

# python3 code to find the probability 
# of finding an element 
# in a singly linked list 
# node class
class node:
    def __init__(self, data, next = none):
        self.data = data
        self.next = none
class linkedlist:
    def __init__(self):
        self.head = none
    def push(self, data):
        # allocate the node & 
        # put the data
        new_node = node(data)
        # make the next of new node as head
        new_node.next = self.head
        # move the head to point to new node
        self.head = new_node
    # counts the number of nodes in linkedlist
    def getcount(self):
        # initialize current
        current = self.head
        # initialize count
        count = 0
        while current is not none:
            count  = 1
            current = current.next
        return count
    def kpresentprobability(self, n, k):
        # initialize current
        current = self.head
        # initialize count
        count = 0.0
        while current is not none:
            if current.data == k:
                count  = 1
            current = current.next
        return count / n
# driver code
if __name__ == "__main__":
    # start with empty list
    llist = linkedlist()
    # use push to construct the linked list
    llist.push(2)
    llist.push(3)
    llist.push(5)
    llist.push(1)
    llist.push(9)
    llist.push(8)
    llist.push(0)
    llist.push(7)
    llist.push(6)
    llist.push(5)
    print(llist.kpresentprobability(
          llist.getcount(), 5))
# this code is contributed by kevalshah5    

c

// c# code to find the probability
// of finding an element
// in a singly linked list
using system;
class gfg{
// link list node
class node 
{
  public int data;
  public node next;
};
// given a reference (pointer to 
// pointer) to the head of a list 
// and an int, push a new node 
// on the front of the list.
static node push(node head_ref, 
                 int new_data)
{
  // allocate node
  node new_node = new node();
  // put in the data
  new_node.data = new_data;
  // link the old list 
  // off the new node
  new_node.next = head_ref;
  // move the head to 
  // point to the new node
  head_ref = new_node;
  return head_ref;
}
// counts nnumber of nodes 
// in linked list
static int getcount(node head)
{
  // initialize count
  int count = 0;
  // initialize current
  node current = head;
  while (current != null) 
  {
    count  ;
    current = current.next;
  }
  return count;
}
static float kpresentprobability(node head,
                                 int n, int k)
{
  // initialize count
  float count = 0;
  // initialize current
  node current = head;
  while (current != null) 
  {
    if (current.data == k)
      count  ;
    current = current.next;
  }
  return count / n;
}
// driver code
public static void main(string[] args)
{
  // start with the empty list
  node head = null;
  // use push() to conbelow list
  // 1.2.1.3.1
  head = push(head, 2);
  head = push(head, 3);
  head = push(head, 5);
  head = push(head, 1);
  head = push(head, 9);
  head = push(head, 8);
  head = push(head, 0);
  head = push(head, 7);
  head = push(head, 6);
  head = push(head, 5);
  console.write("{0:f1}", kpresentprobability(
                 head, getcount(head), 5));
}
}
// this code is contributed by princi singh

输出

0.2


如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。

如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。