Skip to content

Commit fd7068c

Browse files
authored
Add files via upload
Stack Data Structures
1 parent 9eb11c3 commit fd7068c

3 files changed

+416
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<p style=\"color:Green;font-size:20px\"><strong>Dynamic Array Based Implementation</strong></P>"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"class DynamicStack(object):\n",
17+
" def __init__(self,limit=10):\n",
18+
" self.stk = limit*[]\n",
19+
" self.limit = limit\n",
20+
" def is_empty(self):\n",
21+
" return len(self.stk)<=0\n",
22+
" def push(self,item):\n",
23+
" if len(self.stk)>=self.limit:\n",
24+
" self.resize()\n",
25+
" self.stk.append(item)\n",
26+
" print(\"Stack after Push=\",self.stk)\n",
27+
" def pop(self):\n",
28+
" if len(self.stk)<=0:\n",
29+
" print(\"Stack Underflow\")\n",
30+
" return 0\n",
31+
" else:\n",
32+
" return self.stk.pop()\n",
33+
" def top(self):\n",
34+
" if len(self.stk)<=0:\n",
35+
" print(\"Stack Underflow\")\n",
36+
" return 0\n",
37+
" else:\n",
38+
" return self.stk[-1]\n",
39+
" def size(self):\n",
40+
" return len(self.stk)\n",
41+
" def resize(self):\n",
42+
" newstk = list(self.stk)\n",
43+
" self.limit = 2*self.limit\n",
44+
" self.stk = newstk\n",
45+
" "
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 2,
51+
"metadata": {},
52+
"outputs": [
53+
{
54+
"name": "stdout",
55+
"output_type": "stream",
56+
"text": [
57+
"Stack after Push= ['1']\n",
58+
"Stack after Push= ['1', '11']\n",
59+
"Stack after Push= ['1', '11', '21']\n",
60+
"Stack after Push= ['1', '11', '21', '31']\n",
61+
"Stack after Push= ['1', '11', '21', '31', '41']\n",
62+
"Stack after Push= ['1', '11', '21', '31', '41', '51']\n",
63+
"Stack after Push= ['1', '11', '21', '31', '41', '51', '61']\n",
64+
"Stack after Push= ['1', '11', '21', '31', '41', '51', '61', '71']\n"
65+
]
66+
}
67+
],
68+
"source": [
69+
"st = DynamicStack(5)\n",
70+
"st.push(\"1\")\n",
71+
"st.push(\"11\")\n",
72+
"st.push(\"21\")\n",
73+
"st.push(\"31\")\n",
74+
"st.push(\"41\")\n",
75+
"st.push(\"51\")\n",
76+
"st.push(\"61\")\n",
77+
"st.push(\"71\")"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 5,
83+
"metadata": {},
84+
"outputs": [
85+
{
86+
"name": "stdout",
87+
"output_type": "stream",
88+
"text": [
89+
"31\n",
90+
"31\n",
91+
"21\n",
92+
"21\n",
93+
"2\n",
94+
"False\n"
95+
]
96+
}
97+
],
98+
"source": [
99+
"print(st.top())\n",
100+
"print(st.pop())\n",
101+
"print(st.top())\n",
102+
"print(st.pop())\n",
103+
"print(st.size())\n",
104+
"print(st.is_empty())"
105+
]
106+
}
107+
],
108+
"metadata": {
109+
"kernelspec": {
110+
"display_name": "Python 3",
111+
"language": "python",
112+
"name": "python3"
113+
},
114+
"language_info": {
115+
"codemirror_mode": {
116+
"name": "ipython",
117+
"version": 3
118+
},
119+
"file_extension": ".py",
120+
"mimetype": "text/x-python",
121+
"name": "python",
122+
"nbconvert_exporter": "python",
123+
"pygments_lexer": "ipython3",
124+
"version": "3.7.6"
125+
}
126+
},
127+
"nbformat": 4,
128+
"nbformat_minor": 4
129+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<p style=\"color:Green;font-size:20px\"><strong>Stack Implementation Using Linked list</strong></P>"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"\"\"\" Create Node Class \"\"\"\n",
17+
"\n",
18+
"class Node:\n",
19+
" def __init__(self):\n",
20+
" self.data = None\n",
21+
" self.next = None\n",
22+
" def set_data(self,data):\n",
23+
" self.data = data\n",
24+
" def get_data(self):\n",
25+
" return self.data\n",
26+
" def set_next(self,next):\n",
27+
" self.next = next\n",
28+
" def get_next(self):\n",
29+
" return self.next"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 2,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"\"\"\"Stact Inplementation using linked list\"\"\"\n",
39+
"class Stack_linkedlist:\n",
40+
" def __init__(self,data=None):\n",
41+
" self.head = None\n",
42+
" if data:\n",
43+
" for data in data:\n",
44+
" self.push(data)\n",
45+
" \n",
46+
" def push(self,data):\n",
47+
" temp = Node()\n",
48+
" temp.set_data(data)\n",
49+
" temp.set_next(self.head)\n",
50+
" self.head = temp\n",
51+
" \n",
52+
" def pop(self):\n",
53+
" if self.head is None:\n",
54+
" raise IndexError\n",
55+
" temp = self.head.get_data()\n",
56+
" self.head = self.head.get_next()\n",
57+
" return temp\n",
58+
" \n",
59+
" def top(self):\n",
60+
" if self.head is None:\n",
61+
" raise IndexError\n",
62+
" return self.head.get_data()\n",
63+
" "
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": 6,
69+
"metadata": {},
70+
"outputs": [
71+
{
72+
"name": "stdout",
73+
"output_type": "stream",
74+
"text": [
75+
"c\n",
76+
"a\n"
77+
]
78+
}
79+
],
80+
"source": [
81+
"lst = ['first','second','third','four']\n",
82+
"st = Stack_linkedlist()\n",
83+
"st.push('a')\n",
84+
"st.push('c')\n",
85+
"print(st.pop())\n",
86+
"print(st.top())"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 4,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"st.push(10)"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"metadata": {},
102+
"outputs": [],
103+
"source": []
104+
}
105+
],
106+
"metadata": {
107+
"kernelspec": {
108+
"display_name": "Python 3",
109+
"language": "python",
110+
"name": "python3"
111+
},
112+
"language_info": {
113+
"codemirror_mode": {
114+
"name": "ipython",
115+
"version": 3
116+
},
117+
"file_extension": ".py",
118+
"mimetype": "text/x-python",
119+
"name": "python",
120+
"nbconvert_exporter": "python",
121+
"pygments_lexer": "ipython3",
122+
"version": "3.7.6"
123+
}
124+
},
125+
"nbformat": 4,
126+
"nbformat_minor": 4
127+
}

0 commit comments

Comments
 (0)