Skip to content
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

Stack using Array #25

Open
wants to merge 2 commits into
base: noob
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
78 changes: 78 additions & 0 deletions datastructure-with-noob/stack_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include<stdio.h>

#define n 5
int stack[n];
int top=-1;
void push()
{
int x;
if(top==n-1)
{
printf("Overflow\n");

}
printf("enter data\n");
scanf("%d",&x);
top=top+1;
stack[top]=x;
}

void pop()
{
int x;
if(top==-1)
{
printf("Underflow\n");
}
x=stack[top];
printf("The popped element is %d\n",x);
top=top-1;
}

void peek()
{
int x;
x=stack[top];
printf("THe top most element is %d\n",x);
}

void display()
{
int i;
printf("abc\n");
for(i=top;i>=0;i--)
{
printf("%d ",stack[i]);
}
printf("\n");
}
int main()
{
int ch;
do
{ printf("1 for Push \n2 for pop \n3 for peek \n4 for display \n0 for exit\n");

printf("enter the choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
default:
printf("Invalid Choice\n");
break;
}
}while(ch!=0);
return 0;
}

6 changes: 6 additions & 0 deletions search-with-noob/linear_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ int main()

int search_element;
//Take the input of the element to be searched by the user.
cout<<"Enter the search element"<<endl;
cin>>search_element;


int result = linear_search(arr,size,search_element);

//If the result is -1 then print that the element is not present in the array.
//If the result is any value but -1 then print the element is found on that index.
if(result==-1)
cout<<"Element not found"<<endl;
else
cout<<"Element found at index "<<i<<endl;

return 0;

Expand Down