Javatpoint Logo
Javatpoint Logo

Find the first non-repeating character from a stream of characters

Introduction

Finding the first non-repeating character in a stream of characters is an interesting challenge in the fields of data processing and algorithmic problem-solving. This work is important for many applications, such as natural language processing and real-time data processing. Finding the first non-repeating character in a continuous stream of characters is the task at hand. When reading a text file or getting data from a real-time data source, for example, a stream suggests a series of characters that appear one after the other over time. The objective is to identify quickly, given the characters encountered thus far, the first character in the stream that is unique.

Finding the first non-repeating character is important because it can be applied to real-life situations. This issue might be crucial for natural language processing, for example, when recognizing distinct words or characters in a text stream. Additionally, identifying the first non-repeating character can be crucial for prompt decision-making in situations where data is continuously streaming, like network communication or log processing.

Code

Output:

Find the first non-repeating character from a stream of characters

Code Explanation

Header Files

  • The required header files are included in the code: stdlib.h for memory allocation functions and stdio.h for standard input and output functions.

Macro Definition

  • The definition of NO_OF_CHARS is 256, which is the total number of ASCII characters that can be used. Data structures and array sizes are specified using this macro.

DLL Node Structure

  • The struct DLLNode represents the doubly linked list node. In addition to pointers to the previous (prev) and next (next) nodes, it has a character (ch).

Insertion Function (insertAtEnd)

  • The insertAtEnd function adds a new node with the character x to the doubly linked list's head at the end.
  • The new node's memory is dynamically allocated, its character and pointers are set, and the pointers of the head and previous node are updated appropriately.

Deletion Function (deleteNode)

  • A given node (del) can be removed from the doubly linked list (head_ref) using the deleteNode function.
  • It releases the memory that the deleted node was using by modifying the pointers of the nodes that come before and after it.

First Non-Repeating Function (firstNonRepeating)

  • The firstNonRepeating function uses an array and a doubly linked list to process a stream of characters and determine which character is the first non-repeating character.
  • To record the location of every character in the doubly linked list, it keeps an array in the DLL.
  • To count the occurrences of each character, it makes use of an array called charCount.
  • It updates the count and takes action for each character in the stream according to whether or not the character is repeating.
  • Next, the first character that isn't repeated is printed.

Main Function (main)

  • Using "abacabad" as the input stream, the main function initializes a character array (stream).
  • To locate and print the first non-repeating character in the stream, it makes a call to the first non-repeating function.

Output

  • Every stage of the processing is output by the program, along with the character that is being processed, any characters that have been removed, and the first non-repeating character discovered at each stage.

Memory Management

  • Malloc is used by the code to dynamically allocate memory for new nodes in the doubly linked list.
  • Memory used by deleted nodes is released using the free function.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA