Skip to content

added: features to stack_using_linklist and fixed BinarySearch.c #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions BinarySearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ int main() {
/* check length of array */
int size_of_array = sizeof(arr) / sizeof(int);
/* Check if 24 is into arr */
printf("%d\n", binarySearch(arr, 24, 0, size_of_array-1));
if(binarySearch(arr, 24, 0, size_of_array-1)==0)
printf("24 is present\n");
else
printf("24 is absent\n");
/* Check if 118 is into arr */
printf("%d\n", binarySearch(arr, 118, 0, size_of_array-1));
if(binarySearch(arr, 118, 0, size_of_array-1)==0)
printf("118 is present\n");
else
printf("118 is absent\n");
return 0;
}

Expand All @@ -29,18 +35,16 @@ int binarySearch(int array[], int number, int start, int end) {
return array[start] == number ? 0 : 1;
}

int tmp = (int) end / 2;
int mid = start+(end-start) / 2; //to get the middle number (same as int mid=(start+end)/2; )
/* divide array length in half */
/* if number is greater than element in half, do search by start to tmp
* else search by tmp to end
/* if number is greater than element in half, do search by start to mid
* else search by mid to end
*/
if(number == array[tmp]) {
if(number == array[mid]) {
return 0;
} else if(number > array[tmp]) {
return binarySearch(array, number, start, tmp);
} else if(number < array[mid]) {
return binarySearch(array, number, start, mid-1);
} else {
return binarySearch(array, number, tmp, end);
return binarySearch(array, number, mid+1, end);
}
}


164 changes: 155 additions & 9 deletions stack_using_linklist
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,137 @@ void push(int x)
}
void display()
{
printf("\n\tElements are:\n");
struct node *temp;
temp=top;

if(top==NULL)
{
printf("Underflow!!!\n");
}
else
{
while(temp!=NULL)
{

printf("\t%d\n",temp->data);

temp=temp->link;
}
}
printf("\n");
}
void pop()
{

struct node *temp;

if(top==NULL)
{
printf("No element to delete !!\n");
}
else
{
temp=top;
printf("Popped element is %d\n",temp->data);
top=top->link;
free(temp);
}
}
void top_element()
{
if(top==NULL) printf("\tNo element to display !!\n");
else printf("\tTop element is %d\n",top->data);
}
void free_stack()
{
while(top!=NULL){
struct node *temp;
temp=top;
top=top->link;
free(temp);
}
}

int main()
{


int x;
int ch;
do{
printf("\t1.Push\t2.pop\t3.display\t4.top\t5.Push elements\t6.exit\t: ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf(" Enter element\t");
scanf("%d",&x);
push(x);
break;

case 2:pop();
break;

case 3:display();
break;

case 4:top_element();
break;

case 5:
printf("\tEnter number of elements to push\t");
int temp;
scanf("%d",&temp);
for(int loop=0;loop<temp;loop++){
printf(" Enter element\t");
scanf("%d",&x);
push(x);
}
break;
}
}while(ch!=6);
//getch();
free_stack();
printf("\n\tProgram ended successfully\n");
return 0;
}#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node* link;
} *top=NULL;
void push(int x)
{

struct node *new;
new=(struct node*)malloc(sizeof(struct node));
new->data=x;
new->link=top;
top=new;
}
void display()
{
printf("\n\tElements are:\n");
struct node *temp;
temp=top;

if(top==NULL)
{
printf("Underflow");
printf("Underflow!!!\n");
}
else
{
while(temp!=NULL)
{

printf("%d",temp->data);
printf("\n");
printf("\t%d\n",temp->data);

temp=temp->link;
}
}
printf("\n");
}
void pop()
{
Expand All @@ -43,16 +156,30 @@ void pop()

if(top==NULL)
{
printf("No element to deletes ");
printf("No element to delete !!\n");
}
else
{
temp=top;
printf("Popped element is %d\t",temp->data);
printf("Popped element is %d\n",temp->data);
top=top->link;
free(temp);
}
}
void top_element()
{
if(top==NULL) printf("\tNo element to display !!\n");
else printf("\tTop element is %d\n",top->data);
}
void free_stack()
{
while(top!=NULL){
struct node *temp;
temp=top;
top=top->link;
free(temp);
}
}

int main()
{
Expand All @@ -61,20 +188,39 @@ int main()
int x;
int ch;
do{
printf("\t1.Push\t2.pop\t3.display\t4.exit\t");
printf("\t1.Push\t2.pop\t3.display\t4.top\t5.Push elements\t6.exit\t: ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf(" Enter element\t");
scanf("%d",&x);
push(x);
break;

case 2:pop();
break;

case 3:display();
break;
case 4:exit(0);

case 4:top_element();
break;

case 5:
printf("\tEnter number of elements to push\t");
int temp;
scanf("%d",&temp);
for(int loop=0;loop<temp;loop++){
printf(" Enter element\t");
scanf("%d",&x);
push(x);
}
break;
}
}while(ch!=0);
getch();
}while(ch!=6);

free_stack();

printf("\n\tProgram ended successfully\n");
return 0;
}