Q:

C program to search an item in the linked list

0

C program to search an item in the linked list

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Create a linked list, search the specified items into a created linked list, and print the index.

Program:

The source code to search an item in the linked list is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to search an item in the linked list

#include <stdio.h>
#include <stdlib.h>

//Self-referential structure to create node.
typedef struct tmp {
    int item;
    struct tmp* next;
} Node;

//structure for create linked list.
typedef struct
    {
    Node* head;
    Node* tail;

} List;

//Initialize List
void initList(List* lp)
{
    lp->head = NULL;
    lp->tail = NULL;
}

//Create node and return reference of it.
Node* createNode(int item)
{
    Node* nNode;

    nNode = (Node*)malloc(sizeof(Node));

    nNode->item = item;
    nNode->next = NULL;

    return nNode;
}

//Add new item at the end of list.
void addAtTail(List* lp, int item)
{
    Node* node;
    node = createNode(item);

    //if list is empty.
    if (lp->head == NULL) {
        lp->head = node;
        lp->tail = node;
    }
    else {
        lp->tail->next = node;
        lp->tail = lp->tail->next;
    }
}

//Add new item at begning of the list.
void addAtHead(List* lp, int item)
{
    Node* node;
    node = createNode(item);

    //if list is empty.
    if (lp->head == NULL) {
        lp->head = node;
        lp->tail = node;
    }
    else {
        node->next = lp->head;
        lp->head = node;
    }
}

//To print list from start to end of the list.
void printList(List* lp)
{
    Node* node;

    if (lp->head == NULL) {
        printf("\nEmpty List");
        return;
    }

    node = lp->head;

    printf("List:\n");
    while (node != NULL) {
        printf("| %05d |", node->item);
        node = node->next;

        if (node != NULL)
            printf("--->");
    }
    printf("\n\n");
}

void SearchItem(List* lp, int item)
{
    Node* node;
    int flag = 0;
    int index = 0;

    if (lp->head == NULL) {
        printf("\nEmpty List");
        return;
    }

    node = lp->head;

    while (node != NULL) {
        if (node->item == item) {
            flag = 1;
            break;
        }
        node = node->next;

        index++;
    }

    if (flag == 0)
        printf("Item not found\n");
    else
        printf("Item found at index: %d\n", index);
}

//Main function to execute program.
int main()
{
    List* lp;

    int item = 0;

    lp = (List*)malloc(sizeof(List));

    initList(lp);

    addAtTail(lp, 100);
    addAtTail(lp, 150);
    addAtHead(lp, 200);
    addAtHead(lp, 300);

    printList(lp);

    printf("Enter item to search: ");
    scanf("%d", &item);

    SearchItem(lp, item);

    return 0;
}

Output:

RUN 1:
List:
| 00300 |--->| 00200 |--->| 00100 |--->| 00150 |

Enter item to search: 100
Item found at index: 2

RUN 2:
List:
| 00300 |--->| 00200 |--->| 00100 |--->| 00150 |

Enter item to search: 400
Item not found

Explanation:

Here, we created a self-referential structure to implement a linked list, a function to add a node at the start and end of the lit, and a function to search a specified item into the created list.

 

In the main() function, we created the linked list and read an item from the user to search into the linked list then we searched the item using the SearchItem() function and printed the index of the item in the linked list.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Data Structure programs using C and C++ (Linked List Programs)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to search an item in the linked list usi... >>
<< Clone a linked list with next and random pointer u...