diff options
author | Prashant S | 2020-04-14 10:25:32 +0530 |
---|---|---|
committer | GitHub | 2020-04-14 10:25:32 +0530 |
commit | 06b09e7d29d252fb2f5a056eeb8bd1264ff6a333 (patch) | |
tree | 2b1df110e24ff0174830d7f825f43ff1c134d1af /Data_Structures_Using_C_And_C_by_Y_Langsam/2-Stacks.ipynb | |
parent | abb52650288b08a680335531742a7126ad0fb846 (diff) | |
parent | 476705d693c7122d34f9b049fa79b935405c9b49 (diff) | |
download | all-scilab-tbc-books-ipynb-master.tar.gz all-scilab-tbc-books-ipynb-master.tar.bz2 all-scilab-tbc-books-ipynb-master.zip |
Initial commit
Diffstat (limited to 'Data_Structures_Using_C_And_C_by_Y_Langsam/2-Stacks.ipynb')
-rw-r--r-- | Data_Structures_Using_C_And_C_by_Y_Langsam/2-Stacks.ipynb | 504 |
1 files changed, 504 insertions, 0 deletions
diff --git a/Data_Structures_Using_C_And_C_by_Y_Langsam/2-Stacks.ipynb b/Data_Structures_Using_C_And_C_by_Y_Langsam/2-Stacks.ipynb new file mode 100644 index 0000000..c5cd8ad --- /dev/null +++ b/Data_Structures_Using_C_And_C_by_Y_Langsam/2-Stacks.ipynb @@ -0,0 +1,504 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 2: Stacks" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.1_2: To_determine_the_syntacticaly_valid_string.sci" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Solved Example 1\n", +"//To determine the syntacticaly valid string\n", +"function[l]=strlen(x)\n", +" i=1;\n", +" l=0;\n", +" [j,k]=size(x)\n", +" for i=1:k\n", +" l=l+length(x(i));\n", +" end\n", +"endfunction\n", +"function[]=stringvalid(str)\n", +" str=string(str);\n", +" stack=struct('a','0','top',0);\n", +" l1=strlen(str);\n", +" valid=1;\n", +" l=1;\n", +" while(l<=l1)\n", +" if(str(l)=='('|str(l)=='['|str(l)=='{')\n", +" if(stack.top==0)\n", +" stack.a=str(l);\n", +" stack.top=stack.top+1;\n", +" else\n", +" stack.a=[stack.a(:,:) str(l)];\n", +" stack.top=stack.top+1;\n", +" end\n", +" disp(stack);\n", +" end\n", +" if(str(l)==')'|str(l)==']'|str(l)=='}')\n", +" if(stack.top==0)\n", +" valid=0;\n", +" break;\n", +" else\n", +" i=stack.a(stack.top);\n", +" b=stack.a(1);\n", +" for i1=2:stack.top-1\n", +" b=[b(:,:) stack.a(i1)]\n", +" end\n", +" stack.a=b;\n", +" stack.top=stack.top-1;\n", +" symb=str(l);\n", +" disp(stack);\n", +" if(((symb==')')&(i=='('))|((symb==']')&(i='['))|((symb='}')&(i='{')))\n", +" else\n", +" valid=0;\n", +" break;\n", +" end\n", +" end\n", +" end\n", +" l=l+1;\n", +" end\n", +" if(stack.top~=0)\n", +" valid=0;\n", +" end\n", +" if(valid==0)\n", +" disp('Invalid String');\n", +" else\n", +" disp('Valid String');\n", +" end\n", +" endfunction\n", +" //Calling Routine:\n", +" stringvalid(['(' 'A' '+' 'B' '}' ')'])\n", +" stringvalid(['{' '[' 'A' '+' 'B' ']' '-' '[' '(' 'C' '-' 'D' ')' ']'])\n", +" stringvalid(['(' 'A' '+' 'B' ')' '-' '{' 'C' '+' 'D' '}' '-' '[' 'F' '+' 'G' ']'])\n", +" stringvalid(['(' '(' 'H' ')' '*' '{' '(' '[' 'J' '+' 'K' ']' ')' '}' ')'])\n", +" stringvalid(['(' '(' '(' 'A' ')' ')' ')'])" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.1: To_determine_the_syntacticaly_valid_string.sci" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Solved Example 1\n", +"//To determine the syntacticaly valid string\n", +"function[l]=strlen(x)\n", +" i=1;\n", +" l=0;\n", +" [j,k]=size(x)\n", +" for i=1:k\n", +" l=l+length(x(i));\n", +" end\n", +"endfunction\n", +"function[]=stringvalid(str)\n", +" str=string(str);\n", +" stack=struct('a','0','top',0);\n", +" l1=strlen(str);\n", +" valid=1;\n", +" l=1;\n", +" while(l<=l1)\n", +" if(str(l)=='('|str(l)=='['|str(l)=='{')\n", +" if(stack.top==0)\n", +" stack.a=str(l);\n", +" stack.top=stack.top+1;\n", +" else\n", +" stack.a=[stack.a(:,:) str(l)];\n", +" stack.top=stack.top+1;\n", +" end\n", +" end\n", +" if(str(l)==')'|str(l)==']'|str(l)=='}')\n", +" if(stack.top==0)\n", +" valid=0;\n", +" break;\n", +" else\n", +" i=stack.a(stack.top);\n", +" stack.top=stack.top-1;\n", +" symb=str(l);\n", +" if(((symb==')')&(i=='('))|((symb==']')&(i=='['))|((symb=='}')&(i=='{')))\n", +" else\n", +" valid=0;\n", +" break;\n", +" end\n", +" end\n", +" end\n", +" l=l+1;\n", +" end\n", +" if(stack.top~=0)\n", +" valid=0;\n", +" end\n", +" if(valid==0)\n", +" disp('Invalid String');\n", +" else\n", +" disp('Valid String');\n", +" end\n", +" endfunction\n", +" //Calling Routine:\n", +"stringvalid(['H' 'E' 'L' 'L' 'O'])" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.2_3: Check_if_string_is_of_certain_form.sci" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"function[l]=strlen(x)\n", +" i=1;\n", +" l=0;\n", +" [j,k]=size(x)\n", +" for i=1:k\n", +" l=l+length(x(i));\n", +" end\n", +"endfunction\n", +"function[]=str(st)\n", +" stack=struct('a',0,'top',0);\n", +" st=string(st);\n", +" l=1;\n", +" l1=strlen(st);\n", +" symb=st(l);\n", +" valid=1;\n", +" while(l<l1)\n", +" while(symb~='C')\n", +" if(stack.top==0)\n", +" stack.a=st(l);\n", +" stack.top=stack.top+1;\n", +" else\n", +" stack.a=[stack.a(:,:) st(l)];\n", +" stack.top=stack.top+1;\n", +" end\n", +" l=l+1;\n", +" symb=st(l);\n", +" end\n", +" i=st(l+1);\n", +" if(stack.top==0)\n", +" valid=0;\n", +" break;\n", +" else\n", +" symb1=stack.a(stack.top);\n", +" stack.top=stack.top-1;\n", +" if(i~=symb1)\n", +" valid=0;\n", +" break;\n", +" end\n", +" end\n", +" l=l+1;\n", +" end\n", +" if(stack.top~=0)\n", +" valid=0;\n", +" end\n", +" if(valid==0)\n", +" disp('Not of the given format');\n", +" else\n", +" disp('String Of the Given Format');\n", +" end\n", +"endfunction\n", +"//Calling Routine:\n", +"st=['A' 'A' 'B' 'A' 'C' 'A' 'B' 'A' 'A']\n", +"str(st) \n", +"st=['A' 'A' 'B' 'A' 'C' 'A' 'B' 'A' ]\n", +"str(st)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.2: Implementing_Stack_using_union.sci" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Solved Example 2:\n", +"//Implementing Stack using union:\n", +"function[stack]=sta_union(etype,a)\n", +" stackelement=struct('etype',etype);\n", +" [k,l]=size(a);\n", +"select stackelement.etype,\n", +"case 'int' then\n", +" a=int32(a);\n", +" stack=struct('top',l,'items',a);,\n", +" case 'float' then\n", +" a=double(a);\n", +" stack=struct('top',l,'items',a);,\n", +" case 'char' then\n", +" a=string(a);\n", +" stack=struct('top',l,'items',a);,\n", +"end\n", +"disp(stack,'Stack is:');\n", +"endfunction\n", +"a=[32 12.34 232 32.322]\n", +"stack=sta_union('float',a)\n", +"stack=sta_union('int',a)\n", +"stack=sta_union('char',a)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.3: Implementing_Push_And_Pop_Functions.sci" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Solved Example 3:\n", +"//Implementing Push And Pop Functions:\n", +"function[y,sta1]=empty(sta)\n", +" y=0;\n", +" sta1=0;\n", +" if(sta.top==0)\n", +" y=0;\n", +" else\n", +" y=1;\n", +" end\n", +" sta1=sta\n", +"endfunction\n", +"\n", +"function[sta]=push(stac,ele)\n", +" sta=0;\n", +" if(empty(stac)==0)\n", +" stac.a=ele;\n", +" stac.top=stac.top+1;\n", +" else\n", +" stac.a=[stac.a(:,:) ele]\n", +" stac.top=stac.top+1;\n", +" end\n", +" disp(stac);\n", +" sta=stac;\n", +"endfunction\n", +"\n", +"function[ele,sta]=pop(stack)\n", +" ele='-1';\n", +" if(empty(stack)==0)\n", +" disp('Stack Underflow');\n", +" break;\n", +" else\n", +" ele=stack.a(stack.top);\n", +" stack.top=stack.top-1;\n", +" if(stack.top~=0)\n", +" b=stack.a(1);\n", +" for i2=2:stack.top\n", +" b=[b(:,:) stack.a(i2)];\n", +" end\n", +" stack.a=b;\n", +" else\n", +" stack.a='0';\n", +" end\n", +" end\n", +" disp(stack);\n", +" sta=stack;\n", +"endfunction\n", +"global stack\n", +"//Calling Routine:\n", +"stack=struct('a',0,'top',0);\n", +"stack=push(stack,4);\n", +"stack=push(stack,55);\n", +"stack=push(stack,199);\n", +"stack=push(stack,363);\n", +"[ele,stack]=pop(stack);\n", +"disp(stack,'After the above operations stack is:');" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2.4: Convering_an_infix_expression_to_a_Postfix_Express.sci" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"//Solved Example 5:\n", +"//Convering an infix expression to a Postfix Expression:\n", +"function[sta]=push(stac,ele)\n", +" sta=0;\n", +" if(stac.top==0)\n", +" stac.a=ele;\n", +" stac.top=stac.top+1;\n", +" else\n", +" stac.a=[stac.a(:,:) ele]\n", +" stac.top=stac.top+1;\n", +" end\n", +" disp(stac);\n", +" sta=stac;\n", +"endfunction\n", +"\n", +"function[ele,sta]=pop(stack)\n", +" ele='-1';\n", +" if(stack.top==0)\n", +" disp('Stack Underflow');\n", +" break;\n", +" else\n", +" ele=stack.a(stack.top);\n", +" stack.top=stack.top-1;\n", +" if(stack.top~=0)\n", +" b=stack.a(1);\n", +" for i2=2:stack.top\n", +" b=[b(:,:) stack.a(i2)];\n", +" end\n", +" stack.a=b;\n", +" else\n", +" stack.a='0';\n", +" end\n", +" end\n", +" sta=stack;\n", +"endfunction\n", +"function[l]=strlen(x)\n", +" i=1;\n", +" l=0;\n", +" [j,k]=size(x)\n", +" for i=1:k\n", +" l=l+length(x(i));\n", +" end\n", +"endfunction\n", +"function[p]=pre(s1,s2)\n", +" i1=0;\n", +" select s1,\n", +" case '+' then i1=5;\n", +" case '-' then i1=5;\n", +" case '*' then i1=9;\n", +" case '/' then i1=9;\n", +" end\n", +" i2=0;\n", +" select s2,\n", +" case '+' then i2=5;\n", +" case '-' then i2=5;\n", +" case '*' then i2=9;\n", +" case '/' then i2=9;\n", +" end\n", +" p=0;\n", +" p=i1-i2;\n", +" if(s1=='(')\n", +" p=-1;\n", +" end\n", +" if(s2=='('&s1~=')')\n", +" p=-1;\n", +" end\n", +" if(s1~='('&s2==')')\n", +" p=1;\n", +" end\n", +" \n", +" endfunction\n", +"function[a2]=intopo(a1,n)\n", +" stack=struct('a',0,'top',0);\n", +" l1=1;\n", +" l2=strlen(a1(1))\n", +" for i=2:n\n", +" l2=l2+strlen(a1(i))\n", +" end\n", +" a2=list();\n", +" while(l1<=l2)\n", +" symb=a1(l1); \n", +" if(isalphanum(string(a1(l1))))\n", +" a2=list(a2,symb);\n", +" else\n", +" while(stack.top~=0&(pre(stack.a(stack.top),symb)>=0))\n", +" [topsymb,stack]=pop(stack);\n", +" if(topsymb==')'|topsymb=='(')\n", +" a2=a2;\n", +" else\n", +" a2=list(a2,topsymb);\n", +" end\n", +" end\n", +" if(stack.top==0|symb~=')')\n", +" stack=push(stack,symb);\n", +" else\n", +" [ele,stack]=pop(stack);\n", +" end\n", +" end\n", +" l1=l1+1;\n", +" end\n", +" while(stack.top~=0)\n", +" [topsymb,stack]=pop(stack);\n", +" if(topsymb==')'|topsymb=='(')\n", +" a2=a2;\n", +" else\n", +" a2=list(a2,topsymb);\n", +" end\n", +" end\n", +" disp(a2);\n", +"endfunction\n", +"//Calling Routine:\n", +"a1=['(' '2' '+' '3' ')' '*' '(' '5' '-' '4' ')']\n", +"a2=intopo(a1,11)" + ] + } +], +"metadata": { + "kernelspec": { + "display_name": "Scilab", + "language": "scilab", + "name": "scilab" + }, + "language_info": { + "file_extension": ".sce", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "scilab", + "version": "0.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} |