DIR Return Create A Forum - Home
---------------------------------------------------------
Class Discussion
HTML https://srm.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: Data Structure
*****************************************************
#Post#: 38--------------------------------------------------
Linked List Linear Search - (Data Structure)
DIR By: prannyll
Date: May 20, 2019, 8:57 am
---------------------------------------------------------
[move]LINEAR SEARCH IN LINKED LIST[/move]
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}t;
t* head;
t* end;
t* ptr;
void main()
{
int i, found = 0, num;
clrscr();
//Creating 5 Linked List
for(i = 0; i < 5; i++)
{
t* ptr = (t*)malloc(sizeof(t*));
t* ptr2 = (t*)malloc(sizeof(t*));
if(i == 0)
head = ptr;
ptr->data = 10 + i;
ptr->next = ptr2;
if(i == 5-1)
{
end = ptr;
end->next = NULL;
}
free(ptr2);
}
//Displaying List
ptr = head;
for(i = 0; i < 5; i++)
{
printf("\n %u \t\t %d \t\t %u", ptr, ptr->data,
ptr->next);
ptr = ptr->next;
}
//Performing Linear Search
printf("\n Enter the element to be searched : ");
scanf("%d", &num);
ptr = head;
for(i = 0; i < 5; i++)
{
if(ptr->data == num)
{
printf("\n %d found at address : %u", ptr->data, ptr);
found = 1;
break;
}
ptr = ptr->next;
}
if(found == 0)
printf("\n Data does not exist in the list");
getch();
}
*****************************************************
Page 1 of 1