Skip to content

Commit 21ed56c

Browse files
authored
Add files via upload
1 parent 712df54 commit 21ed56c

4 files changed

+271
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
/*class Stack{
5+
private:
6+
char *arr;
7+
int size;
8+
int top=-1;
9+
public:
10+
11+
Stack(int n){
12+
size=n;
13+
arr=new char[size];
14+
}
15+
16+
void push(string t){
17+
18+
if(top==size-1){
19+
cout<<"stack overflow"<<endl;
20+
}else{
21+
top++;
22+
arr[top]=t[i];
23+
}
24+
}
25+
void display(){
26+
for(int i=arr.size()-1;i>=0;i--){
27+
cout<<arr[i];
28+
}
29+
}
30+
}*/
31+
32+
int main(){
33+
34+
stack<char> s;
35+
36+
string n =" Saar";
37+
38+
for(int i=0;i<n.size();i++){
39+
char ch=n[i];
40+
s.push(ch);
41+
}
42+
43+
string mac = "";
44+
45+
while(!s.empty()){
46+
47+
char new1=s.top();
48+
mac.push_back(new1);
49+
50+
s.pop();
51+
}
52+
53+
cout<<"answer is"<<mac<<endl;
54+
55+
return 0;
56+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
class Node{
5+
public:
6+
int data;
7+
Node *next;
8+
};
9+
10+
class Stack{
11+
private:
12+
Node *top;
13+
public:
14+
Stack(){top=NULL;}
15+
16+
void push(int x);
17+
int pop();
18+
void display();
19+
};
20+
21+
void Stack::push(int x){
22+
Node *t=new Node;
23+
if(t==NULL)
24+
cout<<"Stack is Flow"<<endl;
25+
else{
26+
t->data=x;
27+
t->next=top;
28+
top=t;
29+
}
30+
}
31+
32+
int Stack::pop(){
33+
int x=-1;
34+
if(top==NULL){
35+
cout<<"Stack is Emplty"<<endl;
36+
}else{
37+
x=top->data;
38+
Node *t=top;
39+
top=top->next;
40+
delete t;
41+
42+
}
43+
return x;
44+
}
45+
46+
void Stack::display(){
47+
Node *p=top;
48+
while(p!=NULL){
49+
cout<<p->data<<" ";
50+
p=p->next;
51+
}
52+
cout<<endl;
53+
}
54+
55+
int main(){
56+
Stack stk;
57+
58+
stk.push(10);
59+
stk.push(20);
60+
stk.push(30);
61+
62+
stk.display();
63+
cout<<stk.pop()<<endl;
64+
stk.display();
65+
66+
return 0;
67+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
struct Stack
4+
{
5+
int size;
6+
int top;
7+
int *S;
8+
};
9+
void create(struct Stack *st)
10+
{
11+
printf("Enter Size");
12+
scanf("%d", &st->size);
13+
st->top = -1;
14+
st->S = (int *)malloc(st->size * sizeof(int));
15+
}
16+
void Display(struct Stack st)
17+
{
18+
int i;
19+
for (i = st.top; i >= 0; i--)
20+
printf("%d ", st.S[i]);
21+
printf("\n");
22+
}
23+
void push(struct Stack *st, int x)
24+
{
25+
if (st->top == st->size - 1)
26+
printf("Stack overflow\n");
27+
else
28+
{
29+
st->top++;
30+
st->S[st->top] = x;
31+
}
32+
}
33+
int pop(struct Stack *st)
34+
{
35+
int x = -1;
36+
if (st->top == -1)
37+
printf("Stack Underflow\n");
38+
else
39+
{
40+
x = st->S[st->top--];
41+
}
42+
return x;
43+
}
44+
int peek(struct Stack st, int index)
45+
{
46+
int x = -1;
47+
if (st.top - index + 1 < 0)
48+
printf("Invalid Index \n");
49+
x = st.S[st.top - index + 1];
50+
return x;
51+
}
52+
int isEmpty(struct Stack st)
53+
{
54+
if (st.top == -1)
55+
return 1;
56+
return 0;
57+
}
58+
int isFull(struct Stack st)
59+
{
60+
return st.top == st.size - 1;
61+
}
62+
int stackTop(struct Stack st)
63+
{
64+
if (!isEmpty(st))
65+
return st.S[st.top];
66+
return -1;
67+
}
68+
int main()
69+
{
70+
struct Stack st;
71+
create(&st);
72+
push(&st, 10);
73+
push(&st, 20);
74+
push(&st, 30);
75+
push(&st, 40);
76+
printf("%d \n", peek(st, 2));
77+
Display(st);
78+
return 0;
79+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class stack1
5+
{
6+
private:
7+
int size;
8+
int top;
9+
vector<int> s;
10+
11+
public:
12+
stack1(int n){
13+
size=n;
14+
s.resize(n);
15+
top=-1;
16+
}
17+
18+
void display()
19+
{
20+
int i;
21+
for (i = top; i >= 0; i--)
22+
{
23+
cout<<s[i]<<endl;
24+
}
25+
}
26+
27+
void push(int x)
28+
{
29+
if (top == size - 1)
30+
{
31+
cout<<"Stack Overflow"<<endl;
32+
}
33+
else
34+
{
35+
top++;
36+
s[top] = x;
37+
}
38+
}
39+
40+
int pop()
41+
{
42+
int x = -1;
43+
if (top == -1)
44+
{
45+
cout<<"Stack Underflow"<<endl;
46+
}
47+
else
48+
{
49+
x = s[top--];
50+
}
51+
return x;
52+
}
53+
};
54+
55+
int main()
56+
{
57+
stack1 st(4);
58+
59+
60+
st.push(10);
61+
st.push(20);
62+
st.push(30);
63+
st.push(40);
64+
st.push(50);
65+
st.push(60);
66+
67+
st.display();
68+
return 0;
69+
}

0 commit comments

Comments
 (0)