/*Q1.
Write a program to search an element from array of n
integers (Accept the value of n from
the user) by using linear search algorithm. .*/
#include<stdio.h>
#include<stdlib.h>
int linear_serach(int a[],int n,int x){
int i;
for(i=0;i<n;i++){
if(a[i] == x){
return i;
}
}
return -1;
}
void accept(int a[],int n){
int i;
printf("Enter %d Elements :",n);
for (i=0;i<n;i++){
scanf("%d",&a[i]);
}
}
void main(){
int a[20],n,x,pos;
printf("How many number:");
scanf("%d",&n);
accept(a,n);
printf("\nEnter the key want to serach:");
scanf("%d",&x);
pos = linear_serach(a,n,x);
if(pos == -1){
printf("%d not found in array",x);
}else{
printf("%d found at position %d",x,pos);
}
}
/*Output
How many number:5
Enter 5 Elements :1 2 3 4 5
Enter the key want to serach:3
3 found at position 2
*/
/*
Write the menu driven program for singly linked list using following
operations:[15 marks]
i. Create()
ii. Display()
iii. Insert()
iv. Delete()
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node *next;
}NODE;
void create(NODE *head)
{
NODE *last,*newnode;
int n,count;
printf("\nEnter How many Nodes?:\n");
scanf("%d",&n);
last=head;
for(count = 1;count<= n;count = count+1)
{
newnode=(NODE*)malloc(sizeof(NODE));
newnode->next=NULL;
printf("\nEnter the Node %d Data :\n",count);
scanf("%d",&newnode->info);
last->next=newnode;
last=newnode;
}
}
void display(NODE *head)
{
NODE *temp;
for(temp=head->next;temp!=NULL;temp=temp->next)
printf("\n%d\n",temp->info);
}
void insert(NODE* head , int num, int pos){
NODE *newnode,*temp;
int i;
for(temp=head , i=1 ;(temp!=NULL) && (i<=pos-1); i++)
temp = temp->next;
if (temp == NULL)
{
/* code */
printf("Position is out of Range");
return;
}
newnode = (NODE *)malloc(sizeof(NODE));
newnode->info = num;
newnode->next = temp->next;
temp->next=newnode;
}
void delete(NODE *head,int num)
{
NODE *temp,*temp1;
for(temp=head->next;temp->next!=NULL;temp=temp->next)
if(temp->next->info==num){
temp1=temp->next;
temp->next=temp1->next;
free(temp1);
return;
}
printf("Element not found!");
}
void main()
{
NODE *head;
int choice,n,pos;
head=(NODE *)malloc(sizeof(NODE));
head->next=NULL;
do
{
printf("\n\n-------------------Singly Linked List Program-------------------
");
printf("\n1:Create");
printf("\n2:Diplay");
printf("\n3:Insert");
printf("\n4:Delete");
printf("\n0:To Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
create(head);
break;
case 2:
printf("\nElement has been display:\n");
display(head);
break;
case 3:
printf("\nEnter the element and position:\n");
scanf("%d%d",&n,&pos);
insert(head,n,pos);
display(head);
break;
case 4:
printf("\nEnter Value to Delete:\n");
scanf("%d",&n);
delete(head,n);
display(head);
break;
}
}while(choice!=5);
}
/*Output
-------------------Singly Linked List Program-------------------
1:Create
2:Diplay
3:Insert
4:Delete
0:To Exit
Enter Your Choice: 1
Enter How many Nodes?:
5
Enter the Node 1 Data :
1
Enter the Node 2 Data :
2
Enter the Node 3 Data :
3
Enter the Node 4 Data :
4
Enter the Node 5 Data :
5
-------------------Singly Linked List Program-------------------
1:Create
2:Diplay
3:Insert
4:Delete
0:To Exit
Enter Your Choice: 2
Element has been display:
2
3
-------------------Singly Linked List Program-------------------
1:Create
2:Diplay
3:Insert
4:Delete
0:To Exit
Enter Your Choice: 3
Enter the element and position:
63
6
3
-------------------Singly Linked List Program-------------------
1:Create
2:Diplay
3:Insert
4:Delete
0:To Exit
Enter Your Choice: 4
Enter Value to Delete:
6
3
4
*/