File tree Expand file tree Collapse file tree 2 files changed +191
-0
lines changed Expand file tree Collapse file tree 2 files changed +191
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ class SortedStack {
5
+ public:
6
+ stack<int > s;
7
+ void sort ();
8
+ };
9
+
10
+ void printStack (stack<int > s)
11
+ {
12
+ while (!s.empty ())
13
+ {
14
+ printf (" %d " , s.top ());
15
+ s.pop ();
16
+ }
17
+ printf (" \n " );
18
+ }
19
+
20
+ int main ()
21
+ {
22
+ int t;
23
+ cin>>t;
24
+ while (t--)
25
+ {
26
+ SortedStack *ss = new SortedStack ();
27
+ int n;
28
+ cin>>n;
29
+ for (int i=0 ;i<n;i++)
30
+ {
31
+ int k;
32
+ cin>>k;
33
+ ss->s .push (k);
34
+ }
35
+ ss->sort ();
36
+ printStack (ss->s );
37
+ }
38
+ }// } Driver Code Ends
39
+
40
+
41
+ /* The structure of the class is
42
+ class SortedStack{
43
+ public:
44
+ stack<int> s;
45
+ void sort();
46
+ };
47
+ */
48
+
49
+ /* The below method sorts the stack s
50
+ you are required to complete the below method */
51
+ void SortedStack :: sort()
52
+ {
53
+ // Your code here
54
+ stack<int > temp;
55
+
56
+ while (!s.empty ())
57
+ {
58
+ int x=s.top ();
59
+ s.pop ();
60
+ while ( !temp.empty () && temp.top ()<x)
61
+ {
62
+ s.push (temp.top ());
63
+ temp.pop ();
64
+ }
65
+ temp.push (x);
66
+ }
67
+ while (!temp.empty ())
68
+ {
69
+ s.push (temp.top ());
70
+ temp.pop ();
71
+ }
72
+
73
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ /* Link list node */
5
+ struct Node {
6
+ int data;
7
+ struct Node *next;
8
+ Node (int x) {
9
+ data = x;
10
+ next = NULL ;
11
+ }
12
+ }*head;
13
+
14
+ Node *findNode (Node* head, int search_for)
15
+ {
16
+ Node* current = head;
17
+ while (current != NULL )
18
+ {
19
+ if (current->data == search_for)
20
+ break ;
21
+ current = current->next ;
22
+ }
23
+ return current;
24
+ }
25
+
26
+
27
+ void insert ()
28
+ {
29
+ int n,i,value;
30
+ Node *temp;
31
+ scanf (" %d" ,&n);
32
+
33
+ for (i=0 ; i<n; i++)
34
+ {
35
+ scanf (" %d" ,&value);
36
+ if (i==0 )
37
+ {
38
+ head=new Node (value);
39
+ temp=head;
40
+ continue ;
41
+ }
42
+ else
43
+ {
44
+ temp->next = new Node (value);
45
+ temp=temp->next ;
46
+ temp->next =NULL ;
47
+ }
48
+ }
49
+ }
50
+
51
+ /* Function to print linked list */
52
+ void printList (Node *node)
53
+ {
54
+ while (node != NULL )
55
+ {
56
+ printf (" %d " , node->data );
57
+ node = node->next ;
58
+ }
59
+ cout << endl;
60
+ }
61
+
62
+
63
+ void deleteNode (Node *node_ptr);
64
+
65
+ /* Drier program to test above function*/
66
+ int main (void )
67
+ {
68
+ /* Start with the empty list */
69
+
70
+ int t,k,n,value;
71
+
72
+ scanf (" %d" ,&t);
73
+ while (t--)
74
+ {
75
+ insert ();
76
+ scanf (" %d" ,&k);
77
+ Node *del = findNode (head, k);
78
+ if (del != NULL && del->next != NULL )
79
+ {
80
+ deleteNode (del);
81
+ }
82
+ printList (head);
83
+ }
84
+ return (0 );
85
+ }
86
+
87
+
88
+ // } Driver Code Ends
89
+
90
+
91
+ /*
92
+ struct Node {
93
+ int data;
94
+ struct Node *next;
95
+ Node(int x) {
96
+ data = x;
97
+ next = NULL;
98
+ }
99
+ }*head;
100
+ */
101
+
102
+ // This function should delete node from linked list. The function
103
+ // may assume that node exists in linked list and is not last node
104
+ // node: reference to the node which is to be deleted
105
+ void deleteNode (Node *node)
106
+ {
107
+ // Your code here
108
+ Node *cur=node;
109
+ if (cur->next ==NULL )
110
+ {
111
+ free (cur);
112
+ return ;
113
+ }
114
+ cur->data =cur->next ->data ;
115
+ Node *temp=cur->next ;
116
+ cur->next =cur->next ->next ;
117
+ free (temp);
118
+ }
You can’t perform that action at this time.
0 commit comments