fbpx

Struktur Data : Contoh Program Pencarian dalam Bahasa C

Contoh 1

Buatlah program pencarian sequensial pada data terurut baik terurut menaik atau menurun

Source Code Struktur Array

//Sequential Search On Unordered Array Data
#include <stdio.h>
#include <stdbool.h>
#define MAX 100 //maximum size of array can be used

void fillData(int data[], int *size) //fill data into array
{
    printf("Input array size (max 100) = ");
    scanf("%d", size);
    for (int i = 0; i < *size; i++)
    {
        printf("[%d] Data Entry = ", i);
        scanf("%d", &data[i]);
    }
}

int seq_search(int data[], int size, int x)
{
    for (int i = 0; i < size; i++)
    {
        if (data[i] == x)
            return i;
    }
    return -1;
}
bool checkAscending(int data[], int size)
{
    //this function have TRUE value when array is in ascending order
    //array has one or no element
    if (size == 0 || size == 1)
        return true;

    for (int i = 1; i < size; i++)
        //Unsorted pair found
        if (data[i-1] > data[i])
            return false;
    //No Unsorted pair found
    return true;

}
bool checkDescending(int data[], int size)
{
    //this function have TRUE value when array is in descending order
    //array has one or no element
    if (size == 0 || size == 1)
        return true;

    for (int i = 1; i < size; i++)
        //Unsorted pair found
        if (data[i-1] < data[i])
            return false;
    //No Unsorted pair found
    return true;
}

int ordered_search(int data[], int size, int x)
{
    int i = 0;
    if (checkAscending(data, size))
    {
        while (i < size && data[i] <= x)
        {
            if (data[i] == x)
                return i;
            i++;
        }
        return -1;
    }
    else if (checkDescending(data, size))
    {
        while (i < size && data[i] >= x)
        {
            if (data[i] == x)
                return i;
            i++;
        }
        return -1;
    }
    else
    {
        //if array list not in Ascending/Descending order
        //use the usual seq-search function
        for (int i = 0; i < size; i++)
        {
            if (data[i] == x)
            return i;
        }
        return -1;
    }
}

void main()
{
    int data[MAX];
    int size; //size of array
    int x;
    fillData(data,&size);
    printf("Input the data/value you want to search = ");
    scanf("%d", &x);
    if(ordered_search(data, size, x) == -1)
        printf("\nData (%d) not found", x);
    else
        printf("\nData (%d) was found at index [%d]", x, ordered_search(data, size, x));
}

Output

Kasus data tidak terurut

Kasus data terurut menaik

Kasus data terurut menurun

Source Code Struktur Array

//Sequential Search On Unordered Linked List Data
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct node
{
    int value;
    struct node *next;
};
typedef struct node *ptrnode;

ptrnode head = NULL;
int countnode; //count of node

ptrnode insert(int data)
{
    ptrnode p,q;
    p = (ptrnode)malloc(sizeof(struct node));
    p->value = data;
    p->next = NULL;
    if (head == NULL)
    {
        head = p;
        q = head;
    }
    else
    {
        q = head;
        while (q->next != NULL)
        {
            q = q->next;
        }
        q->next = p;
    }
    return (head);
}

void fillData()
{
    int k;
    printf("Input number of nodes = ");
    scanf("%d", &countnode);
    for (int j = 1; j <= countnode; j++)
    {
        printf("[%d] Data entry = ", j);
        scanf("%d", &k);
        head = insert(k);
    }
}

int search(int x) //x is a value we are looking for
{
    int j = 1;
    ptrnode tmp = head;
    while (tmp != NULL)
    {
        if(x == tmp->value)
        {
            return j;
        }
        else
        {
            tmp = tmp->next;
            j++;
        }
    }
    return -1; //if the value we are looking for doesn't exist, return -1
}

bool checkAscending(ptrnode head)
{
    //function to check linked list is
    //sorred in descending order or not
    if (head == NULL)
        return true;
    //tranverse the list till last node and return
    //false if a node is smaller than or equal
    //its next
    ptrnode tmp;
    for (tmp = head; tmp->next != NULL; tmp = tmp->next)
        if (tmp->value >= tmp->next->value)
            return false;

    return true;
}

bool checkDescending(ptrnode head)
{
    //function to check linked list is
    //sorred in descending order or not
    if (head == NULL)
        return true;
    //tranverse the list till last node and return
    //false if a node is smaller than or equal
    //its next
    ptrnode tmp;
    for (tmp = head; tmp->next != NULL; tmp=tmp->next)
        if (tmp->value <= tmp->next->value)
            return false;

    return true;
}

int ordered_search(int x)
{
    int i = 1;
    ptrnode tmp = head;

    if (checkAscending(head))
    {
        while (tmp != NULL && i < &countnode && tmp->value <= x)
        {
            if(x == tmp->value)
            {
                return i;
            }
            else
            {
                tmp = tmp->next;
                i++;
            }
        }
    }
    else if (checkDescending(head))
    {
        while (tmp != NULL && i < &countnode && tmp->value >= x)
        {
            if(x == tmp->value)
            {
                return i;
            }
            else
            {
                tmp = tmp->next;
                i++;
            }
        }
    }
    else
    {
        int j = 1;
        ptrnode tmp = head;
        while (tmp != NULL)
        {
            if(x == tmp->value)
            {
                return j;
            }
            else
            {
                tmp = tmp->next;
                j++;
            }
        }
        return -1; //if the value we are looking for doesn't exist, return -1
    }
}

void freeMemory()
{
    while (head != NULL)
    {
        ptrnode tmp = head;
        head = head->next;
        tmp->next = NULL;
        free(tmp);
    }
}

void main()
{
    fillData();
    int x;
    printf("Input the data/value you want to search = ");
    scanf("%d", &x);
    if (search(x) == -1)
        printf("Data (%d) not found");
    else
        printf("Data (%d) was found at node [%d]", x, ordered_search(x));
    freeMemory();
}

Output

Kasus data tidak terurut

Kasus data terurut menaik

Kasus data terurut menurun


Contoh 2

Buatlah program untuk pencarian data students berisi int nim, char nama[50] dengan struktur array. Pencarian bisa dilakukan secara sekuensial atau biner dengan berdasarkan nim atau berdasarkan nama.

Source Code Sequensial Struktur Array

//Sequential Searching in Array Based on Name and NIM
#include <stdio.h>
#include <string.h>

struct dataMhs
{
    char name[50];
    int nim;
};

void fillData(struct dataMhs data[], int *size) //fill data into array
{
    for (int i = 0; i < size; i++)
    {
        printf("[%d] Data Entry \n", i+1);
        printf("    NIM           = ");
        scanf("%d/n", &data[i].nim);
        printf("    Students Name = ");
        scanf("%s/n", &data[i].name);
    }
}
int ordered_search_nim(struct dataMhs data[], int *size, int *x)
{
    int i;
    for (i = 0; i < size; ++i)
    {
        if (data[i].nim == x)
        {
            break;
        }
    }

    if (i != size)
        printf("Students (%s) with (%d) is present in the array at index [%d]", data[i].name, x, i+1);
    else
        printf("Student with nim (%d) is not present in the array", x);
}

int ordered_search_name(struct dataMhs data[], int *size, char *x)
{
    int i;
    for (i = 0; i < size; ++i)
    {
        if (!strcmp(x, data[i].name))
        {
            break;
        }
    }

    if (i != size)
        printf("Students (%s) with (%d) is present in the array at index [%d]", x, data[i].nim, i+1);
    else
        printf("Student (%s) with is not present in the array", x);
}

void display(struct dataMhs data[], int size)
{
    printf("\nList of College Students\n");
    for (int i = 0; i < size; i++)
    {
        printf("Student [%d]\n", i+1);
        printf("Name = %s\n", data[i].name);
        printf("NIM  = %d\n", data[i].nim);
    }
}

void main()
{
    int i, c, choice, size; //size of array
    char x[50];
    int y;

    printf("Input array size = ");
    scanf("%d", &size);
    struct dataMhs mhs[size];
    //Make a menu
    do
    {
        printf("SEQUENTIAL SEARCH IN ARRAY LIST\n");
        printf("1. Insert Data\n");
        printf("2. Search Data Based On Student's Name\n");
        printf("3. Search Data Based On Student's NIM\n");
        printf("4. Display List of Students Identity\n");
        printf("ENTER YOUR CHOICE = ");
        scanf("%d", &choice);

        switch(choice)
        {
            printf(" ");
            case 1 :
                fillData(mhs, size);
                break;
            case 2 :
                printf("Input the data/value you want to search = ");
                scanf("%s", &x);
                ordered_search_name(mhs, size, x);
                break;
            case 3 :
                printf("Input the data/value you want to search = ");
                scanf("%d", &y);
                ordered_search_nim(mhs, size, y);
                break;
            case 4 :
                display(mhs, size);
                break;
            default :
                printf("Wrong Input, Try Another\n");
                break;
        }
        printf("\n Do you want to continue? (Press 1 for 'Yes') = ");
        scanf("%d", &c);
    } while (c == 1);
    getchar();
    return 0;
}

Output

Input data

Pencarian berdasarkan nama

Pencarian berdasarkan nim

Source Code Biner Struktur Array

//Binary Search in Array Based on NIM and Name
#include <stdio.h>
#include <string.h>

struct dataMhs
{
    char name[50];
    int nim;
};

void fillData(struct dataMhs data[], int *size) //fill data into array
{
    for (int i = 0; i < size; i++)
    {
        printf("[%d] Data Entry \n", i+1);
        printf("    NIM           = ");
        scanf("%d", &data[i].nim);
        printf("    Students Name = ");
        scanf("%s", &data[i].name);
    }
}

int binarySearch_name(struct dataMhs data[], int size, char x[])
{
    int L = 0;
    int H = size - 1;
    int M;
    int index;

    while (L <= H)
    {
        M = (L + H)/2;
        if (strcmp(data[M].name, x) == 0)
            return M + 1;
        else
        {
            if (strcmp(data[M].name, x) < 0)
                L = M + 1;
            else
                H = M - 1;
        }
    }
    return -1;
}
int binarySearch_nim(struct dataMhs data[], int size, int x)
{
    int L = 0;
    int H = size - 1;
    int M = -1;
    int index = -1;

    while (L <= H)
    {
        M = (L + H) / 2;
        if (data[M].nim == x)
            return M + 1;
        else
        {
            if (data[M].nim < x)
                L = M + 1;
            else
                H = M - 1;
        }
    }
    return -1;
}

void display(struct dataMhs data[], int size)
{
    printf("\nList of College Students\n");
    for (int i = 0; i < size; i++)
    {
        printf("Student [%d]\n", i+1);
        printf("Name = %s\n", data[i].name);
        printf("NIM  = %d\n", data[i].nim);
    }
}

void main()
{
    int choice, c, size; //array size
    char x[50];
    int y;

    printf("Input array size = ");
    scanf("%d", &size);

    struct dataMhs mhs[size];

    //Make a menu
    do
    {
        printf("BINARY SEARCH IN ARRAY LIST\n");
        printf("1. Insert Data\n");
        printf("2. Search Data Based On Student's Name\n");
        printf("3. Search Data Based On Student's NIM\n");
        printf("4. Display List of Students Identity\n");
        printf("ENTER YOUR CHOICE = ");
        scanf("%d", &choice);

        switch(choice)
        {
            printf(" ");
            case 1 :
                fillData(mhs, size);
                break;
            case 2 :
                printf("Input the data/value you want to search = ");
                scanf("%s", &x);
                if (binarySearch_name(mhs, size, x) == -1)
                    printf("Student with name (%s) not found", x);
                else
                    printf("Students with name (%s) was found at index [%d]", x, binarySearch_name(mhs, size, x));
                break;
            case 3 :
                printf("Input the data/value you want to search = ");
                scanf("%d", &y);
                if (binarySearch_nim(mhs, size, y) == -1)
                    printf("Students with NIM (%d) not found", y);
                else
                    printf("Students with NIM (%d) was found at index [%d]", y, binarySearch_nim(mhs, size, y));
                break;
            case 4 :
                display(mhs, size);
                break;
            default :
                printf("Wrong Input, Try Another\n");
                break;
        }
        printf("\n Do you want to continue? (Press 1 for 'Yes') = ");
        scanf("%d", &c);
    } while (c == 1);
    getchar();
    return 0;
}

Output

Input data

Pencarian berdasarkan nama

Pencarian berdasarkan nim


Contoh 3

Buatlah program untuk pencarian data students berisi int nim, char nama[50] dengan struktur linked list. Pencarian bisa dilakukan secara sekuensial atau biner berdasarkan nim atau berdasarkan nama.

Source Code Biner Struktur Linked List

//Binary Searching with Linked List
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
    int nim;
    char name[50];
    struct node *next;
};
typedef struct node* ptrnode;

ptrnode head, tail = NULL;
int countnode;

ptrnode insert (int nim, char name[])
{
    ptrnode p;
    p = (ptrnode)malloc(sizeof(struct node));
    p->nim = nim;
    strcpy(p->name, name);
    p->next = NULL;
    if (head == NULL)
    {
        head = p;
        tail = head;
    }
    else
    {
        tail = head;
        while (tail->next != NULL)
        {
            tail = tail->next;
        }
        tail->next = p;
        tail = p;
    }
    return(head);
}

void fillData()
{
    int j, k;
    char l[50];
    printf("Input count of nodes (data must be in ascending order) = ");
    scanf("%d", &countnode);
    for (j = 1; j <= countnode; j++)
    {
        printf("[%d] Data Entry\n", j);
        printf("    NIM      = ");
        scanf("%d/n", &k);
        printf("    Name     = ");
        scanf("%s/n", &l);
        head = insert(k, l);
    }
}

ptrnode middle(ptrnode start, ptrnode last)
{
    //find the middle node
    if (start == NULL)
        return NULL;
    ptrnode slow = start;
    ptrnode fast = start->next;
    while (fast != last)
    {
        fast = fast->next;
        if (fast != last)
        {
            slow = slow->next;
            fast = fast->next;
        }
    }
    return slow;
}

ptrnode binarySearch_nim(int x)
{
    ptrnode start = head;
    ptrnode last = NULL;
    do
    {
        //find the middle node
        ptrnode mid = middle(start, last);
        //if the middle node is NULL
        if (mid == NULL)
            return NULL;
        //if x was found in the middle node
        if (mid->nim == x)
            return mid;
        //if x greater than the middle node
        else if (mid->nim < x)
            start = mid->next;
        //if x smaller than the middle node
        else last = mid;
    } while (last == NULL || last != start);

    //if x was not found
    return NULL;
}

ptrnode binarySearch_name(char x[])
{
    ptrnode start = head;
    ptrnode last = NULL;
    do
    {
        //find the middle node
        ptrnode mid = middle(start, last);
        //if the middle node is NULL
        if (mid == NULL)
            return NULL;
        //if x was found in the middle node
        if (strcmp(mid->name, x) == 0)
            return mid;
        //if x greater than the middle node
        else if (strcmp(mid->name, x) < 0)
            start = mid->next;
        //if x smaller than the middle node
        else last = mid;
    } while (last == NULL || last != start);

    //if x was not found
    return NULL;
}
void display(ptrnode head)
{
    ptrnode tmp = head;
    int i = 1;
    while (tmp != NULL)
    {
        printf("\nStudent's Detail [%d]\n", i);
        printf("Name  = %s\n", tmp->name);
        printf("NIM   = %d", tmp->nim);
        tmp = tmp->next;
        i++;

    }
}
void freeMemory()
{
    while (head != NULL)
    {
        ptrnode tmp = head;
        head = head->next;
        tmp->next = NULL;
        free(tmp);
    }
}

void main()
{
    char x[50];
    int choice, c, y;

    //Make a menu
    do
    {
        printf("BINARY SEARCH IN LINKED LIST\n");
        printf("1. Insert Data\n");
        printf("2. Search Data Based On Student's Name\n");
        printf("3. Search Data Based On Student's NIM\n");
        printf("4. Display List of Students Identity\n");
        printf("ENTER YOUR CHOICE = ");
        scanf("%d", &choice);

        switch(choice)
        {
            printf(" ");
            case 1 :
                fillData();
                break;
            case 2 :
                printf("Input the data/value you want to search = ");
                scanf("%s", &x);
                if (binarySearch_name(x) == NULL)
                    printf("Student with Name (%s) not found", x);
                else
                    printf("Student with Name (%s) was found", x);
                break;
            case 3 :
                printf("Input the data/value you want to search = ");
                scanf("%d", &y);
                if (binarySearch_nim(y) == NULL)
                    printf("Student with NIM (%d) not found", y);
                else
                    printf("Student with NIM (%d) was found", y);
                break;
            case 4 :
                display(head);
                break;
            default :
                printf("Wrong Input, Try Another\n");
                break;
        }
        printf("\n Do you want to continue? (Press 1 for 'Yes') = ");
        scanf("%d", &c);
    } while (c == 1);
    getchar();
    return 0;
    freeMemory();
}

Output

Input data

Pencarian berdasarkan nama

Pencarian berdasarkan nim

Source Code Sequensial Struktur Linked List

//Sequential Search On Linked List Data Based on NIM
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

struct node
{
    int nim;
    char name[50];
    struct node *next;
};
typedef struct node *ptrnode;

ptrnode head = NULL;
int countnode; //count of node

ptrnode insert(int nim, char name[])
{
    ptrnode p,q;
    p = (ptrnode)malloc(sizeof(struct node));
    strcpy(p->name, name);
    p->nim = nim;
    p->next = NULL;
    if (head == NULL)
    {
        head = p;
        q = head;
    }
    else
    {
        q = head;
        while (q->next != NULL)
        {
            q = q->next;
        }
        q->next = p;
    }
    return (head);
}

void fillData()
{
    int k;
    char l[50];
    printf("Input number of nodes = ");
    scanf("%d", &countnode);
    for (int j = 1; j <= countnode; j++)
    {
        printf("[%d] Data Entry\n", j);
        printf("    NIM      = ");
        scanf("%d", &k);
        printf("    Name     = ");
        scanf("%s", &l);
        head = insert(k, l);
    }
}

bool checkAscending(ptrnode head)
{
    //function to check linked list is
    //sorred in descending order or not
    if (head == NULL)
        return true;
    //tranverse the list till last node and return
    //false if a node is smaller than or equal
    //its next
    ptrnode tmp;
    for (tmp = head; tmp->next != NULL; tmp = tmp->next)
        if (tmp->nim >= tmp->next->nim)
            return false;

    return true;
}

bool checkDescending(ptrnode head)
{
    //function to check linked list is
    //sorred in descending order or not
    if (head == NULL)
        return true;
    //tranverse the list till last node and return
    //false if a node is smaller than or equal
    //its next
    ptrnode tmp;
    for (tmp = head; tmp->next != NULL; tmp=tmp->next)
        if (tmp->nim <= tmp->next->nim)
            return false;

    return true;
}

bool ordered_search_name(ptrnode head, char name)
{
    ptrnode current = head; //initialize current
    while (current != NULL)
    {
        if (strcmp(name, head->name) == 0)
            return true;
        current = current->next;
    }
    return false;
}

int search_name(char name[])
{
    int j = 1;
    ptrnode tmp = head;
    while (tmp != NULL)
    {
        if(strcmp(name, tmp->name) == 0)
        {
            return j;
        }
        else
        {
            tmp = tmp->next;
            j++;
        }
    }
    return -1; //if the value we are looking for doesn't exist, return -1
}

int ordered_search_nim(int x)
{
    int i = 1;
    ptrnode tmp = head;

    if (checkAscending(head))
    {
        while (tmp != NULL && i < &countnode && tmp->nim <= x)
        {
            if(x == tmp->nim)
            {
                return i;
            }
            else
            {
                tmp = tmp->next;
                i++;
            }
        }
    }
    else if (checkDescending(head))
    {
        while (tmp != NULL && i < &countnode && tmp->nim >= x)
        {
            if(x == tmp->nim)
            {
                return i;
            }
            else
            {
                tmp = tmp->next;
                i++;
            }
        }
    }
    else
    {
        int j = 1;
        ptrnode tmp = head;
        while (tmp != NULL)
        {
            if(x == tmp->nim)
            {
                return j;
            }
            else
            {
                tmp = tmp->next;
                j++;
            }
        }
        return -1; //if the value we are looking for doesn't exist, return -1
    }
}

void freeMemory()
{
    while (head != NULL)
    {
        ptrnode tmp = head;
        head = head->next;
        tmp->next = NULL;
        free(tmp);
    }
}

void display(ptrnode head) //display the data in linked list
{
    ptrnode tmp = head;
    int i=1;
        while (tmp != NULL)
        {
            printf("\nStudent's Detail [%d]\n", i);
            printf("Name  = %s\n", tmp->name);
            printf("NIM   = %d", tmp->nim);
            tmp = tmp->next;
            i++;
        }
}

void main()
{
    char x[50];
    int choice, c, y;
    //Make a menu
    do
    {
        printf("SEQUENTIAL SEARCH IN LINKED LIST\n");
        printf("1. Insert Data\n");
        printf("2. Search Data Based On Student's Name\n");
        printf("3. Search Data Based On Student's NIM\n");
        printf("4. Display List of Students Identity\n");
        printf("ENTER YOUR CHOICE = ");
        scanf("%d", &choice);

        switch(choice)
        {
            printf(" ");
            case 1 :
                fillData();
                break;
            case 2 :
                printf("Input the data/value you want to search = ");
                scanf("%s", &x);
                if (search_name(x) == -1)
                    printf("Student with name (%s) not found", x);
                else
                    printf("Student with name (%s) found at node [%d]", x, search_name(x));
                break;
            case 3 :
                printf("Input the data/value you want to search = ");
                scanf("%d", &y);
                if (ordered_search_nim(y) == -1)
                    printf("Student with NIM (%d) not found", y);
                else
                    printf("Student with NIM (%d) found at node [%d]", y, ordered_search_nim(y));
                break;
            case 4 :
                display(head);
                break;
            default :
                printf("Wrong Input, Try Another\n");
                break;
        }
        printf("\n Do you want to continue? (Press 1 for 'Yes') = ");
        scanf("%d", &c);
    } while (c == 1);
    getchar();
    return 0;

    freeMemory();
}

Output

Input data

Pencarian berdasarkan nama

Pencarian berdasarkan nim


Materi Lengkap

Silakan baca juga beberapa artikel menarik kami tentang Searching, daftar lengkapnya adalah sebagai berikut.


Tonton juga video pilihan dari kami berikut ini

Bagikan ke teman-teman Anda

Contact Us

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site
error: Content is protected !!
Up