Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6a122eb

Browse files
created a program to implement K-stacks using array
1 parent ed198ed commit 6a122eb

File tree

3 files changed

+167
-8
lines changed

3 files changed

+167
-8
lines changed

‎Data Structures/LinkedLists/Singly_LL.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ Node* RemoveDuplicates(Node *head) //O(n)-TIME COMPLEXITY
206206
temp=head;
207207
while(temp->next!=NULL) {
208208
temp1=temp->next;
209+
209210
if(temp->data == temp1->data) {
210211

211212

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<stdbool.h>
4+
#include<stdlib.h>
5+
6+
//All global variables with global scope
7+
8+
int *arr; // Array of size n to store actual content to be stored in stacks
9+
int *top; // Array of size k to store indexes of top elements of stacks
10+
int *next; // Array of size n to store next entry in all stacks
11+
// and free list
12+
int n, k;
13+
int fre; // To store beginning index of free list
14+
15+
16+
// A utility function to check if there is space available
17+
bool isFull()
18+
{
19+
return (fre == -1);
20+
}
21+
22+
// To push an item in stack number 'sn' where sn is from 0 to k-1
23+
void push(int item, int sn);
24+
25+
// To pop an from stack number 'sn' where sn is from 0 to k-1
26+
int pop(int sn);
27+
28+
// To check whether stack number 'sn' is empty or not
29+
bool isEmpty(int sn) { return (top[sn] == -1); }
30+
31+
32+
//function to create K stacks with an array of size n
33+
void CreatekStacks(int k1, int n1);
34+
35+
//function to traverse the stack
36+
void traverseStack(int sn);
37+
38+
39+
int main()
40+
{
41+
42+
// Let us create 3 stacks in an array of size 10
43+
int k = 3, n = 10;
44+
45+
//creating 3 stacks each with size 10
46+
CreatekStacks(k, n);
47+
48+
// Let us put some items in stack number 0
49+
push(10, 0);
50+
push(90, 0);
51+
52+
53+
// Let us put some items in stack number 1
54+
push(15, 1);
55+
push(23, 1);
56+
push(44, 1);
57+
58+
59+
// Let us put some items in stack number 2
60+
push(55, 2);
61+
push(5, 2);
62+
63+
printf("Popped element from stack 0 is %d\n",pop(0));
64+
printf("Popped element from stack 1 is %d\n",pop(1));
65+
printf("Popped element from stack 2 is %d\n",pop(2));
66+
67+
// traverseStack(2);
68+
69+
70+
return 0;
71+
}
72+
73+
74+
void CreatekStacks(int k1, int n1)
75+
{
76+
// Initialize n and k, and allocate memory for all arrays
77+
k = k1, n = n1;
78+
79+
//creating and allocating memory for the 3 arrays dynamically
80+
arr = (int*) malloc( n * sizeof(int));
81+
top = (int*) malloc(k * sizeof(int));
82+
next = (int*) malloc( n * sizeof(int));
83+
84+
// Initialize all stacks as empty
85+
for (int i = 0; i < k; i++)
86+
top[i] = -1;
87+
88+
// Initialize all spaces as free
89+
fre = 0;
90+
for (int i=0; i<n-1; i++)
91+
next[i] = i+1;
92+
next[n-1] = -1; // -1 is used to indicate end of free list
93+
}
94+
95+
96+
//function to push an item on top of stack
97+
void push(int item, int sn)
98+
{
99+
// Overflow check
100+
if (isFull())
101+
{
102+
printf("\nThe Stack is full!\n");
103+
return;
104+
}
105+
106+
int i = fre; // Store index of first free slot
107+
108+
// Update index of free slot to index of next slot in free list
109+
fre = next[i];
110+
111+
// Update next of top and then top for stack number 'sn'
112+
next[i] = top[sn];
113+
top[sn] = i;
114+
115+
// Put the item in array
116+
arr[i] = item;
117+
}
118+
119+
120+
//function to pop an item from stack
121+
int pop(int sn)
122+
{
123+
// Underflow check
124+
if (isEmpty(sn))
125+
{
126+
printf("\nThe stack is empty!\n") ;
127+
return INT_MAX;
128+
}
129+
130+
131+
// Find index of top item in stack number 'sn'
132+
int i = top[sn];
133+
134+
top[sn] = next[i]; // Change top to store next of previous top
135+
136+
// Attach the previous top to the beginning of free list
137+
next[i] = fre;
138+
fre = i;
139+
140+
// Return the previous top item
141+
return arr[i];
142+
}
143+
144+
145+
void traverseStack(int sn)
146+
{
147+
if (isEmpty(sn))
148+
{
149+
printf("\nStack is Empty!!\n");
150+
}
151+
152+
// Finding the size of stack number 'sn'
153+
//printf("%d\n",size);
154+
int i = 0 ;
155+
while(i <= top[sn])
156+
{
157+
printf("%d\n",arr[i]);
158+
i++;
159+
}
160+
161+
162+
}

‎Data Structures/Trees/BST/SmallestNumberInBSTGreaterThanN.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ node *SmallestNumGreaterThanNUsingMorris(node *root,int n)
7676
{
7777
if(!curr->left)
7878
{
79+
7980
if(curr->data >= n)
8081
{
81-
if(diff < abs(curr->data-n))
82+
if(diff > abs(curr->data-n))
8283
{
8384
diff = abs(curr->data - n );
8485
small=curr;
@@ -207,14 +208,9 @@ int main()
207208
//
208209
// cout<<endl;
209210
//
210-
// cout<<SmallestNumGreaterThanNUsingMorris(root,0)->data;
211+
cout<<SmallestNumGreaterThanNUsingMorris(root,8)->data;
211212
//
212-
213-
Preorder(root);
214-
215-
cout<<endl;
216-
217-
nthPreorder(root,5);
213+
218214

219215

220216
return 0;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /