Repository navigation
Expand file tree
/
Copy pathday56.cpp
More file actions
145 lines (138 loc) · 2.96 KB
/
Copy pathday56.cpp
File metadata and controls
145 lines (138 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <stack>
using namespace std;
struct Node{
int data;
Node* next;
};
void printlist(Node* head){
Node* current=head;
while(current!=nullptr){
cout<<current->data<<"->";
current=current->next;
}
cout<<"nullptr"<<endl;
};
void insertathead(Node*& head,int value){
Node* newnode=new Node();
newnode->data=value;
newnode->next=head;
head=newnode;
};
void deletevalue(Node*& head,int target){
if (head==nullptr) {
return ;
}
if (head->data==target){
Node* temp=head;
head=head->next;
delete temp;
return;
}
Node* current=head;
while(current->next != nullptr && current->next->data != target){
current=current->next;
}
if (current->next!=nullptr){
Node* temp=current->next;
current->next=current->next->next;
delete temp;
}
};
Node* reverselist(Node* head){
Node* prev=nullptr;
Node* current=head;
Node* next=nullptr;
while(current!=nullptr){
next=current->next;
current->next=prev;
prev=current;
current=next;
}
return prev;
};
bool isvalid(string s){
stack<char>st;
for (char c:s){
if (c=='(' ||c=='{'||c=='['){
st.push(c);
} else {
if (st.empty()) {
return false;
}
char top = st.top();
if ( (c==')' && top=='(') || (c=='}' && top=='{') || (c==']' && top=='[')){
st.pop();
} else {
return false;
}
}
}
return st.empty();
}
int main(){
int arr[5]={10,20,30,40,50};
int sum=0;
int mx=arr[0];
int count=0;
for (int i=0;i<5;++i){
cout<<arr[i]<<" ";
sum+=arr[i];
if (arr[i]>mx){
mx=arr[i];
}
if (arr[i]%2==0){
count++;
}
}
for (int i=4;i>=0;--i){
cout<<arr[i]<<" ";
}
cout<<sum<<"\n";
cout<<mx<<"\n";
cout<<count<<"\n";
Node* head=new Node{1,nullptr};
Node* second=new Node{2,nullptr};
Node* head=nullptr;
insertathead(head,30);
insertathead(head,20);
insertathead(head,10);
head->data=10;
head->next=second;
head->next->next=new Node{3,nullptr};
second->data=20;
second->next=nullptr;
printlist(head);
cout<<"first "<<head->data<<" second "<<head->next->data<<endl;
stack<int>s;
s.push(10);
s.push(20);
s.push(30);
cout<<s.top()<<endl;
s.pop();
cout<<s.top()<<endl;
if (s.empty()){
cout<<"stack is empty";
}
string str="hello";
stack<char>s;
for (char c:str){
s.push(c);
}
string reversed="";
while(!s.empty()){
reversed+=s.top();
s.pop();
}
cout<<reversed;
stack<int>s;
s.push(1);
s.push(2);
s.push(3);
cout<<s.size();
while (!s.empty()){
cout<<s.top()<<" ";
s.pop();
}
return 0;
}