{"id": "DS-Module II-GangaHoli", "local_path": "local_pdfs\\DS-Module II-GangaHoli.pdf", "text": " \n \n \nMODULE II-STACKS, QUEUES, \nRECURSION \nDr. Ganga Holi, Professor & Head, ISE Dept. \n \n \n \nGlobal Academy of Technology \nDept. of Information Science & Engineering \n[COMPANY NAME] \n[Company address] \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n1 \n \n \n \nGlobal Academy of Technology \nDept. of Information Science & Engineering \nData Structure Notes \nOn \nModule II \nStacks, Queues & Recursion \n \nDr. Ganga Holi, \nProfessor & Head \n \n \nContents \nStacks .......................................................................................................................................................... 1 \nArray representation of stacks ................................................................................................................. 2 \nStack Operations ........................................................................................................................................ 2 \nAlgorithms for stack operations ................................................................................................................ 2 \nApplications of Stack ................................................................................................................................. 4 \nEvaluation of Postfix expression .............................................................................................................. 4 \nConversion to Infix expression to Postfix expression ........................................................................... 5 \nMultiple stacks ............................................................................................................................................ 8 \nRecursion .................................................................................................................................................... 9 \nRules ...................................................................................................................................................... 11 \nQueue Data structure .............................................................................................................................. 13 \nBasic features of Queue .......................................................................................................................... 14 \nApplications of Queue ............................................................................................................................. 14 \nDisadvantage Simple Queue .................................................................................................................. 17 \nCircular Queue .......................................................................................................................................... 17 \nCircular Queue Program ......................................................................................................................... 20 \nDequeues- Double Ended Queues ....................................................................................................... 21 \nOutput Restricted Double Ended Queue .............................................................................................. 22 \nPriority Queue ......................................................................................................................................... 22 \nBasic Operations .................................................................................................................................... 23 \nUsing single array ................................................................................................................................... 23 \nUsing Multiple arrays ............................................................................................................................... 25 \nRelated Questions on Queue & Stacks ................................................................................................ 26 \nRelated Questions on Stacks ................................................................................................................. 26 \n \n \n \n \n \n \n \n \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n1 \nStacks \nDefinition: A stack is linear Data Sturcture in which items are added (pushed, inserted) \nand removed(pop, deleted) from one end called top. \nA stack is a container of objects that are inserted and removed according to the last-in \nfirst-out (LIFO) principle. In the stacks only two operations are allowed: push the item \ninto the stack, and pop the item out of the stack. A stack is a limited access data \nstructure - elements can be added and removed from the stack only at the top. Push \nadds an item to the top of the stack, pop removes the item from the top. A helpful \nanalogy is to think of a stack of books; you can remove only the top book, also you can \nadd a new book on the top. \nGiven a stack S=(a0,a1, ….an-1), we say a0 is the bottom element, an-1 is the top element \nand ai , is on the top of element ai-1 , 0=0;i--) \n \n \nprintf(\"%d\\n\",stack[i]); \n} \nint topStack() \n{ \nreturn(stack[top]; \n} \n// Complete program for stack operations \n#include \n#define max_size 5 \nint stack[max_size],top=-1; \nvoid push(); \nvoid pop(); \nvoid display(); \nint main() \n{ \nint choice; \n \nwhile(1) \n \n{ \nprintf(\"\\n\\n--------STACK OPERATIONS-----------\\n\"); \n \n \nprintf(\"1.Push\\n\"); \n \n \nprintf(\"2.Pop\\n\"); \n \n \nprintf(\"3.Display\\n\"); \n \n \nprintf(\"4.Exit\\n\"); \n \n \nprintf(\"\\nEnter your choice:\\t\"); \n \n \nscanf(\"%d\",&choice); \n \n \nswitch(choice) \n \n \n{ \ncase 1 : printf(\"Enter the element to be inserted:\\t\"); \n \n \n \n \nscanf(\"%d\",&item); \n \n \n \n \npush(ele); break; \n \n \n \ncase 2 : ele=pop(); \nif(ele==-1) \n \nprintf(\"stack Underflow\\n”); \nelse \nprintf(\"Poped element =%d\\n”, ele); \n \n \n \nbreak; \n \n \n \ncase 3 : display(); break; \n \n \n \ncase 4 : return 0; \n \n \n \ndefault : printf(\"\\nInvalid choice:\\n\") ; break; \n \n \n} \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n4 \n \n} return 0; \n} \nApplications of Stack \n• The simplest application of a stack is to reverse a word. You push a given word \nto stack - letter by letter - and then pop letters from the stack. \n• Another application is an \"undo\" mechanism in text editors; this operation is \naccomplished by keeping all text changes in a stack. \n• Evaluation of Expressions \n• Conversion from Infix to Postfix, prefix \n• \nLanguage processing: \no space for parameters and local variables is created internally using a \nstack. \no compiler's syntax check for matching braces is implemented by using \nstack. \no support for recursion \nInfix expression- The expression in which operator is placed in between operands \nEx. a+b \nPolish Notation- names after the Polish Mathematician Jan Lukasiewiez, refers to the \nexpression in which the operator is placed before the operands also called prefix \nexpression. \nEx. +ab \nReverse Polish Notation- refers to the expression in which the operator is placed after \nthe operands also called postfix expression or suffix Expression. \nEx. ab+ \n \nEvaluation of Postfix expression \nIn high level languages, infix notation cannot be used to evaluate expressions. Instead \ncompilers typically use a parenthesis free notation to evaluate the expression. A \ncommon technique is to convert a infix notation into postfix notation, then evaluating it. \n \nBefore knowing the function that translates infix expression to postfix expression, we \nneed to know how to evaluate the postfix expression. \n \n45+ will be evaluate as 4+5=9 \n \nTo evaluate an expression we make single left to right scan (read) of it. We place the \noperands on a stack until we find an operator. We then remove, from the stack, correct \nnumber of operands for the operator, perform operation, and place the result back on \nthe stack. We continue until we reach the end of the expression. We remove the result \ntop of the stack. \n \n \n \n \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n5 \nExample3. \nPostfix \n \nStack \n234*+ \n \n \n 34*+ \n \n2 \n 4*+ \n \n2 3 \n *+ \n \n2 3 4 (3*4=12) \n + \n \n2 \n12 \n(2+12=24) \n \n \n24 \n \n Algorithm/ Function to evaluate Postfix expression \nint op(int op1,char sym,int op2) //op=evaluate \n{ \nswitch(sym) \n \n{ \ncase '+': return op1+op2; \n \n \ncase '-': return op1-op2; \n \n \ncase '*': return op1*op2; \n \n \ncase '/': return op1/op2; \n \n \ncase '%': return op1%op2; \n \n \ncase '^': \n \n \ncase '$': return pow(op1,op2); \n \n} \n \nreturn 0; \n} \n \n void evaluatePostfix() \n { \nfor(i=0;i='0' && sym<='9') \n \n \ns[++top]=sym-'0'; \n \nelse \n \n{ \n \n \nop2=s[top--]; \n \n \nop1=s[top--]; \n \n \ns[++top]=op(op1,sym,op2); \n \n} \n} \n \nprintf(\"\\nThe result is %d\",s[top]); \n} \n \nConversion to Infix expression to Postfix expression \nInfix to Postfix Notation. Normally you will code your expressions in the programmes \nusing infix notation, but compiler understands and uses only postfix notation and hence \nit has to convert infix notation to post fix notation. For this activity, stack data structure is \nused. We will consider following binary operators and their precedence rules: \n^ Exponentiation \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n6 \n* / Multiplication and Division. Both have same priority-- Execution is from left to \nright. \n+ - Addition and Subtraction. Both have same priority--- Execution is from left to \nright. \n \nOther Operators : order is from left to right \nExample D+E-G means (D+E)-G \nLet us solve a problem \nInfix notation : A +B * C – D \nBased on priorities of operators, parenthesize the expression. You know, the priority for \nthe expression given is ( * or / ) and followed by (+ or -). \n( ( A + ( B * C ) ) – D ) \nCheck your brackets are correct and opening and closing brackets match. \nOpening brackets = closing brackets = 3 \nStep 3. Number your brackets starting from Right Hand side. Give the same number to \ngoverning bracket on left hand side. \n( ( A + ( B * C ) ) – D ) \n1 2 3 3 2 1 \n Post Fix Notation : A B C * + D - \n Example: Infix Expression -- A + B * C - D / E \nS.No \nInfix \nStack(top)\n \n \nPostfix \n1. \n A + B * C -D / E \n# \n \n2. \n + B * C -D / E \n# \nA \n3. \n B * C -D / E \n# + \nA \n4. \n *C -D / E \n#+ \nAB \n5. \n C -D / E \n#+* \nAB \n6. \n -D / E \n#+* \nABC \n7. \n -D / E \n#+ \nA B C * \n8. \n D / E \n#+- \nA B C * \n9. \n / E \n#+- \nA B C * D \n10. \n E \n#+-/ \nA B C * D \n11. \n \n \n#+-/ \nA B C * D E \n12. \n \n#+- \nA B C * D E / \n13. \n \n#+ \nA B C * D E /- \n14. \n \n# \nA B C * D E/-\n+ \n \n \n \nExample 2: \nA * B - ( C + D ) + E \nS.No \nInfix \n Stack(bot->top) \nPostfix \n1. \n \nA * B - ( C - D ) + E \nempty \nempty \n2. \n \n * B - ( C + D ) + E \nempty \nA \n3. \n \n B - ( C + D ) + E \n* \nA \n4. \n - ( C + D ) + E \n* \nA B \n5. \n - ( C + D ) + E \nempty \nA B * \n6. \n ( C + D ) + E \n- \nA B * \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n7 \n7. \n C + D ) + E \n- ( \nA B * \n8. \n + D ) + E \n- ( \nA B * C \n9. \n D ) + E \n- ( + \nA B * C \n10. \n ) + E \n- ( + \nA B * C D \n11. \n + E \n- \nA B * C D + \n12. \n + E \nempty \nA B * C D + - \n13. \n E \n+ \nA B * C D + - \n14. \n \n+ \nA B * C D + -E \n15. \n \nEmpty \nA B * C D + -E+ \n \nAlgorithm or function for conversion infix to postfix expression \n \nvoid postfix(void) \n{ // assuming variables are declared as global \n \nchar ch; int i; \npush('#'); \n \nwhile ((ch = infx[i++]) != '\\0') \n \n{ \n \n \nif (ch == '(') \n \n \n \npush(ch); \n \n \nelse if (isalnum(ch)) \n \n \n \npofx[k++] = ch; \n \n \nelse if (ch == ')') \n \n \n{ \n \n \n \nwhile (s[top] != '(') \n \n \n \n \npofx[k++] = pop(); \n \n \n \nelem = pop(); /* Remove ( */ \n \n \n} \n \n \nelse /* Operator */ \n \n \n{ \n \n \n \nwhile (pr(s[top]) >= pr(ch)) \n \n \n \npofx[k++] = pop(); \n \n \n \npush(ch); \n \n \n} \n \n} \n \n \n \nwhile (s[top] != '#') /* Pop from stack till empty */ \n \n \npofx[k++] = pop(); \n \npofx[k] = '\\0'; /* Make pofx as valid string */ \n \nprintf(\"Given Infix Expn: %s Postfix Expn: %s\\n\", infx, pofx); \n} \n \n \n// pr() is a function for finding the operator precedence value \n \n int pr(char elem) /* Function for precedence */ \n{ \nswitch (elem) \n \n{ \ncase '#' : return 0; \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n8 \n \n \ncase '(' : return 1; \n \n \ncase '+' : \n \n \ncase '-' : return 2; \n \n \ncase '*' : \n \n \ncase '/' : \n \n \ncase '%' : return 3; \n \n \ncase '^': return 4; \n \n} \n} \n \n \nExercise Problems from Sahni \n1. Write postfix and prefix expression of the following expressions \na. A*b*c \nb. –a+b-c+d \nc. a*(-b)+c -→ ans: ab-*c+ here no. of operators equal to no. of \noperands. So there has to be one unary operator… So –b will be \nevaluated first, then * will be performed, then +. \nd. a&&b||c||!(e>f) ( assuming C precedence Refer C text book) \n \n \nMultiple stacks \nMultiple stacks can be implemented using multiple arrays, one array to represent on stack. \nMultiple stacks can be implemented using single array. \nSingle array can be used to represent two stacks: one will go from beginning from starting \ntowards end and second stack will go in opposite direction. \ntop \n \n \n \n \n \n \n \n \n \n \nS1[top]=-1 stack empty condition if(top==-1) \nS2[bot]=N stack empty condition if(bot==N) \nStack full if (top==bot) for both \nStack 1 :Push -- S1[++top]=ele Pop -→ ele=S1[top--] \nStack 2 :Push -- S2[--bot]=ele Pop -→ ele=S2[top++] \n \nSingle array can be used to represent multiple stacks by dividing into equal number of parts. \n \n \n \n \ni=0 \nI=N……….. \nI=2N……….. I=3N-\nI=3N……….. \n………. \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n9 \n…………….i0 the n!=n.(n-1)! \n \nfact(n) \n{ if(n<1) return 1; \nelse return(n*fact(n-1)); \n } \n \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n10 \n \n \nFibonacci Sequence \nFibonacci series 0,1,1,2,3,5,8,13,21,….. \n1. if n=0 or n=1, then Fn =n \n2. if n>1 then Fn=Fn-1+Fn-2 \n fib(n) \n { \nif(n==0) return 0; \nif(n==1) return 1; \nelse \nreturn( fib(n-1)+fib(n-2)); \n } \n \nAckermann–Péter function \nThe two-argument Ackermann–Péter function, is defined as follows for nonnegative \nintegers m and n: \n𝑨(𝒎, 𝒏) = {\n𝒏+ 𝟏 𝒊𝒇𝒎= 𝟎\n𝑨(𝒎−𝟏, 𝟏) 𝒊𝒇 𝒎> 𝟎 𝒂𝒏𝒅 𝒏= 𝟎\n𝑨(𝒎−𝟏, 𝑨(𝒎, 𝒏−𝟏)) 𝒊𝒇 𝒎> 𝟎 𝒂𝒏𝒅 𝒏> 𝟎\n \n \nA(0,1)=2 \nA(1,1)=A(m-1,A(m,n-1))=A(0,A(1,0))=A(0,A(0,1))=A(0,2)=3 \nA(2,1)=A(1,A(2,0))=A(1,A(1,1))=A(1,3)=A(0,A(1,2))=A(0,A(0,A(1,1))) \n=A(0,A(0,3))=A(0,4)=5 \nIt grows very fast and non primitive recursive function. \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n11 \nTower of Hanoi \nTower of Hanoi, is a mathematical puzzle which consists of three tower (pegs) and \nmore than one rings; as depicted below − \n \nThese rings are of different sizes and stacked upon in ascending order i.e. the smaller \none sits over the larger one. There are other variations of puzzle where the number of \ndisks increase, but the tower count remains the same. \nRules \nThe mission is to move all the disks to some another tower without violating the \nsequence of arrangement. The below mentioned are few rules which are to be followed \nfor tower of hanoi − \n• \nOnly one disk can be moved among the towers at any given time. \n• \nOnly the \"top\" disk can be removed. \n• \nNo large disk can sit over a small disk. \n \nTower of hanoi puzzle with n disks can be solved in minimum 2n−1 steps. This \npresentation shows that a puzzle with 3 disks has taken 23−1 = 7 steps. \n \nStep 1 − Move n-1 disks from source to temp \nStep 2 − Move nth disk from source to dest \nStep 3 − Move n-1 disks from temp to dest \n \nN=1 \nStep 1 − Move nth disk from source(A) to dest(C) \n \nN=2 \nStep 1 − Move n-1=1 disk from source(A) to temp(B) \nStep 2 − Move nth =2 disk from source(A) to dest(C) \nStep 3 − Move n-1=1 disk from temp(B) to dest(C) \n \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n12 \n \n \nvoid tower(n, source, temp, dest) \n{ \n if(n == 0) \n printf(“move %d disk from %c to %c”, source,dest); \n \n else \n { \n tower(n - 1, source, dest,temp) // Step 1 \n printf(“move %d disk from %c to %c”, source,dest); // Step 2 \n tower(n - 1, temp, source, dest) // Step 3 \n } \n \n} \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n13 \n \n \nQueue Data structure \nIn our daily life, to catch a bus, to with draw money from ATM or to buy a cinema ticket, we form \na queue. Queue is a first in first out FIFO linear data structure Queue is an important data \nstructure for \ncomputer applications even. \n \nQueue is linear data structure in which deletion of element can take place only at one end called \nthe front end and insertion can take place at the other end called the rear. \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n14 \n \nBasic features of Queue \n1. Like Stack, Queue is also an ordered list of elements of similar data types. \n2. Queue is a FIFO( First in First Out ) structure. \n3. Once a new element is inserted into the Queue, all the elements inserted before the new \nelement in the queue must be removed, to remove the new element. \n4. peek( ) function is oftenly used to return the value of first element without dequeuing it. \nApplications of Queue \nQueue, as the name suggests is used whenever we need to have any group of objects \nin an order in which the first one coming in, also gets out first while the others wait for \nthere turn, like in the following scenarios : \n1. Serving requests on a single shared resource, like a printer, CPU task scheduling \netc. \n2. In real life, Call Center phone systems will use Queues, to hold people calling \nthem in an order, until a service representative is free. \n3. Handling of interrupts in real-time systems. The interrupts are handled in the \nsame order as they arrive, First come first served. \n \nImplementation of Queue \nQueue can be implemented using an Array, Stack or Linked List. The easiest way of \nimplementing a queue is by using an Array. Initially the head(FRONT) and the \ntail(REAR) of the queue points at the first index of the array (starting the index of array \nfrom 0). As we add elements to the queue, the rear keeps on moving ahead, always \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n15 \npointing to the position where the next element will be inserted, while the front remains \nat the first index. \nWhen we remove element from Queue, we can follow two possible approaches \n(mentioned [A] and [B] in above diagram). In [A] approach, we remove the element at \nfront position, and then one by one move all the other elements on position forward. In \napproach [B] we remove the element from front position and then move front to the \nnext position. \nIn approach [A] there is an overhead of shifting the elements one position forward every \ntime we remove the first element. In approach [B] there is no such overhead, but \nwhenever we move head one position ahead, after removal of first element, the size on \nQueue is reduced by one space each time. \n \nRear=-1=-1 \nFront=0 \nFront=0 \nFront=0 \nRear=4 \n Rear=0 \nFront=1 \nFront=1 \nRear=4 \nRear=3 \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n16 \n/*SIMPLE QUEUE \nDesign, Develop and Implement a menu driven Program in C for the following \noperations on \nSimple QUEUE of Characters (Array Implementation of Queue with maximum size \nMAX) \na. Insert an Element on to QUEUE \nb. Delete an Element from QUEUE \nc. Demonstrate Overflow and Underflow situations on QUEUE \nd. Display the status of QUEUE \ne. Exit \n \nSupport the program with appropriate functions for each of the above operations \n*/ \n#include \n#define SIZE 5 \nint i,rear=-1,front=0,option,j; \nchar q[SIZE]; \nint qFull() \n{ \nif(rear==SIZE-1) \n \n \nreturn 1; \n \nelse \n \n \nreturn 0; \n} \nint qEmpty() \n{ \nif(front>rear) \n \n \nreturn 1; \n \nelse \n \n \nreturn 0; \n} \n \nvoid enqueue(char ch) \n{ \nif(qFull()) \n \n{ \nprintf(\" Queue overflow\\n\"); return; } \n \nq[++rear]=ch; \n \nreturn; \n} \n \nchar dequeue() \n{ \n \nreturn q[front++]; } \n \nvoid display() \n{ \nint i; \n \nfor(i=front;i<=rear;i++) \n \n \nprintf(\"%c \",q[i]); \n \nprintf(\"\\n\"); \n \n} \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n17 \n \nint main() \n{ \nchar ch; \n \nfor(;;) \n \n{ \nprintf(\"\\n SIMPLE QUEUE\\n\"); \n \n \nprintf(\"1.Insert\\n\"); \n \n \nprintf(\"2.Delete\\n\"); \n \n \nprintf(\"3.Display\\n\"); \n \n \nprintf(\"4.Exit\"); \n \n \nprintf(\"\\nEnter your option:\"); \n \n \nscanf(\"%d\",&option); \n \n \nswitch(option) \n \n \n{ \n \n \n \ncase 1 : \nprintf(\"\\nEnter the char:\"); \n \n \n \n \nch=getchar(); \n \n \n \n \nch=getchar(); \n \n \n \n \nenqueue(ch); \n \n \n \n \nbreak; \n \n \n \ncase 2 : \nif(qEmpty()) \n \n \n \n \n \nprintf(\"\\nQ is empty\\n\"); \n \n \n \n \nelse \n \n \n \n \n \nprintf(\"\\nDeleted item is: %c\",dequeue()); \n \n \n \n \nbreak; \n \n \n \ncase 3 : \nif(qEmpty()) \n \n \n \n \n \nprintf(\"\\nQ is empty\\n\"); \n \n \n \n \nelse \n \n \n \n \n \ndisplay(); \n \n \n \n \nbreak; \n \n \n \ndefault : return 0; \n \n \n} \n \n} \n} \n \nDisadvantage Simple Queue \nWhen the first element is serviced, the front is moved to next element. However, the \nposition vacated is not available for further use. Thus, we may encounter a situation, \nwherein program shows that queue is full, while all the elements have been deleted are \navailable but unusable, though empty. \n \nCircular Queue \nIn a standard queue data structure re-buffering problem occurs for each dequeue \noperation. To solve this problem by joining the front and rear ends of a queue to make \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n18 \nthe queue as a circular queue. Circular queue is a linear data structure. It follows FIFO \nprinciple. \n• \nIn circular queue the last node is connected back to the first node to make a \ncircle. \n• \nElements are added at the rear end and the elements are deleted at front end of \nthe queue \n• \nBoth the front and the rear pointers points to the beginning of the array. \n• \nIt is also called as “Ring buffer”. \nCircular Queue can be created in three ways they are \n · Using linked list · Using arrays \nIn arrays the range of a subscript is 0 to n-1 where n is the maximum size. To make the \narray as a circular array by making the subscript 0 as the next address of the subscript \nn-1 by using the formula subscript = (subscript +1) % maximum size. In circular queue \nthe front and rear pointer are updated by using the above formula. \nThe following figure shows circular array: \n \n \n \n \nQueue shown in above figure is full. \n \n \n \n \n \n \n \nQueue shown in above figure has one empty slot at the beginning, as first \nelement is deleted from the queue. Below figure shows two elements deletion and \nqueue has two empty slots at the beginning. Now element can be inserted from \nthe beginning making use of rear pointer pointing to beginning location. \n \n \n \nR \nF \n \n20 \n30 \n40 \n50 \n60 \n70 \nR \nF \n \n \n30 \n40 \n50 \n60 \n70 \nR \nF \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n19 \n \n \n \n \n \nFigure shown below shows that element 80 is added at the beginning location and rear is \nnow pointing to 0th location. \n \n \n \n \n \n \n \n \n \n \nEvery time rear is incremented by the value =(rear+1)%SIZE. \nEvery time front is incremented by the value=(front+1)%SIZE. \n \nAlgorithm for Enqueue operation using array \nStep 1. start \nStep 2. if (front == (rear+1)%max) \nPrint error “circular queue overflow “ \nStep 3. else \n{ rear = (rear+1)%max \nQ[rear] = element; \nIf (front == -1 ) f = 0; \n} \nStep 4. stop \n \nAlgorithm for Dequeue operation using array \n \nStep 1. start \nStep 2. if ((front == rear) && (rear == -1)) \nPrint error “circular queue underflow “ \nStep 3. else \n{ element = Q[front] \nIf (front == rear) front=rear = -1 \nElse \nFront = (front + 1) % max \n} \nStep 4. stop \n \n80 \n \n30 \n40 \n50 \n60 \n70 \nR \nF \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n20 \nCircular Queue Program \n Design, Develop and Implement a menu driven Program in C for the following operations on \nCircular QUEUE of Characters (Array Implementation of Queue with maximum size MAX) \na. Insert an Element on to Circular QUEUE \nb. Delete an Element from Circular QUEUE \nc. Demonstrate Overflow and Underflow situations on Circular QUEUE \nd. Display the status of Circular QUEUE \ne. Exit \nSupport the program with appropriate functions for each of the above operations \n*/ \n#include \n#include \n#include \n#define SIZE 5 \nint q[SIZE],rear=-1,front=0,option,count=0; \nint qFull() \n{ \n \nif(count==SIZE) \n \n \n return 1; \n \nelse \n \n \n return 0; \n} \nint qEmpty() \n{ \n \nif(count==0) \n \n \nreturn 1; \n \nelse \n \n \nreturn 0; \n} \n \nvoid enqueue(char ch) \n{ \n \nif(qFull()) \n \n{ \nprintf(\" Queue overflow\\n\"); return; \n \n} \n \nrear=(rear+1)%SIZE; \n \nq[rear]=ch; count++; printf(\" rear=%d count=%d\\n\",rear,count); \n \nreturn; \n} \n \nchar dequeue() \n{ \nchar c; \n \nc=q[front]; front=(front+1)%SIZE; count--; \n \nreturn c; \n} \n \nvoid display() \n{ int i; j=front; \n \nfor(i=0;i \n#include \n#include \n#include \n#define MAX 6 \n \nint intArray[MAX]; \nint itemCount = 0; \n \nint peek(){ \n return intArray[itemCount - 1]; \n} \n \nbool isEmpty(){ \n return itemCount == 0; \n} \n \nbool isFull(){ \n return itemCount == MAX; \n} \n \nint size(){ \n return itemCount; \n} \n \nvoid insert(int data){ \n int i = 0; \n \n if(!isFull()){ \n // if queue is empty, insert the data \n if(itemCount == 0){ \n intArray[itemCount++] = data; \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n24 \n }else{ \n // start from the right end of the queue \n \n \n \n \n for(i = itemCount - 1; i >= 0; i-- ){ \n // if data is larger, shift existing item to right end \n if(data > intArray[i]){ \n intArray[i+1] = intArray[i]; \n }else{ \n break; \n } \n } \n \n \n \n \n // insert the data \n intArray[i+1] = data; \n itemCount++; \n } \n } \n} \n \nint removeData(){ \n return intArray[--itemCount]; \n} \n \nint main() { \n /* insert 5 items */ \n insert(3); \n insert(5); \n insert(9); \n insert(1); \n insert(12); \n \n // ------------------ \n // index : 0 1 2 3 4 \n // ------------------ \n // queue : 12 9 5 3 1 \n insert(15); \n \n // --------------------- \n // index : 0 1 2 3 4 5 \n // --------------------- \n // queue : 15 12 9 5 3 1 \n \n \n if(isFull()){ \n printf(\"Queue is full!\\n\"); \n } \n \n // remove one item \n int num = removeData(); \n printf(\"Element removed: %d\\n\",num); \n \n \n // --------------------- \n // index : 0 1 2 3 4 \n // --------------------- \n // queue : 15 12 9 5 3 \n \n // insert more items \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n25 \n insert(16); \n \n // ---------------------- \n // index : 0 1 2 3 4 5 \n // ---------------------- \n // queue : 16 15 12 9 5 3 \n \n // As queue is full, elements will not be inserted. \n insert(17); \n insert(18); \n \n // ---------------------- \n // index : 0 1 2 3 4 5 \n // ---------------------- \n // queue : 16 15 12 9 5 3 \n printf(\"Element at front: %d\\n\",peek()); \n \n printf(\"----------------------\\n\"); \n printf(\"index : 5 4 3 2 1 0\\n\"); \n printf(\"----------------------\\n\"); \n printf(\"Queue: \"); \n \n \n while(!isEmpty()){ \n int n = removeData(); \n printf(\"%d \",n); \n } \n} \n \n Using Multiple arrays \nMultiple arrays are used to implement the priority queue. Array number represents the priority \nvalue. If the priority of the element is 1 then inserted that element in Q1,if the priority of the \nelement is 2 then inserted that element in Q2, and so on. But deletion will happen from the \nhighest priority queue first. All the elements of the highest priority queue will be deleted first, and \nthen second priority queue elements and so on. \nUsing linked list we can implement the Priority Queue. \nTwo dimensional arrays can also be used to represent Priority Queue. Row represents the \nPriority value and order of arrival of the elements. Size of the array depends on the Priority \nValue. \n Col→ \nRow | \n V \n1 \n2 \n3 \n4 \n5 \n6 \nQ P -1 \n \n Q \nP \nH \n \n \n \nQ P -2 \n \nA \nB \nC \nD \nE \n \nQ P -3 \n \nT \nP \nR \nM \n \n \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n26 \n \n \n \nRelated Questions on Queue & Stacks \n• \nWhat is stack? What are the operations on stack?> \n• \nWhat is the recursion? What are two important criteria for recursive functions? \n• \nHow does rear and front work in circular queue in C language? \n• \nWhat are the applications of queues and stacks? \n• \nHow is priority queue implemented in C? \n• \nCan we use an array to create a circular queue? \n• \nWhat is the application of queues in computer science? \n• \nWhat is the advance application of circular queue? \n• \nWhat is queue and a circular queue? \n• \nHow does rear and front work in circular queue in C language? \n• \nWhat is dequeue? How to insert and delete from dequeue. \n• \nDescribe the priority queue. How do we implement the priority queues? \nRelated Questions on Stacks \n \n1. Entries in a stack are \"ordered\". What is the meaning of this statement? \no A. A collection of stacks can be sorted. \no B. Stack entries may be compared with the '<' operation. \no C. The entries must be stored in a linked list. \no D. There is a first entry, a second entry, and so on. \n2. The operation for adding an entry to a stack is traditionally called: \no A. add \no B. append \no C. insert \no D. push \n3. The operation for removing an entry from a stack is traditionally called: \no A. delete \no B. peek \no C. pop \no D. remove \n4. Which of the following stack operations could result in stack underflow? \no A. is_empty \no B. pop \no C. push \no D. Two or more of the above answers \n5. Which of the following applications may use a stack? \no A. A parentheses balancing program. \no B. Keeping track of local variables at run time. \no C. Syntax analyzer for a compiler. \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n27 \no D. All of the above. \n6. Consider the following pseudocode: \n \n declare a stack of characters \n while ( there are more characters in the word to read ) \n { \n read a character \n push the character on the stack \n } \n while ( the stack is not empty ) \n { \n write the stack's top character to the screen \n pop a character off the stack \n } \n \n7. What is written to the screen for the input \"carpets\"? \no A. serc \no B. carpets \no C. steprac \no D. ccaarrppeettss \n8. Here is an INCORRECT pseudocode for the algorithm which is supposed to determine \nwhether a sequence of parentheses is balanced: \n declare a character stack \n while ( more input is available) \n { \n read a character \n if ( the character is a '(' ) \n push it on the stack \n else if ( the character is a ')' and the stack is not empty ) \n pop a character off the stack \n else \n print \"unbalanced\" and exit \n } \n print \"balanced\" \n \n9. Which of these unbalanced sequences does the above code think is balanced? \no A. ((()) \no B. ())(() \no C. (()())) \no D. (()))() \n10. Consider the usual algorithm for determining whether a sequence of parentheses is \nbalanced. What is the maximum number of parentheses that will appear on the stack AT \nANY ONE TIME when the algorithm analyzes: (()(())(()))? \no A. 1 \no B. 2 \no C. 3 \no D. 4 \no E. 5 or more \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n28 \n11. Consider the usual algorithm for determining whether a sequence of parentheses is \nbalanced. Suppose that you run the algorithm on a sequence that contains 2 left \nparentheses and 3 right parentheses (in some order). What is the maximum number of \nparentheses that will ever appear on the stack AT ONE TIME during the computation? \no A. 1 \no B. 2 \no C. 3 \no D. 4 \no E. 5 or more \n12. Suppose we have an array implementation of the stack class, with ten \nitems in the stack stored at data[0] through data[9]. The CAPACITY \nis 42. Where does the push member function place the new entry in \nthe array? \no A. data[0] \no B. data[1] \no C. data[9] \no D. data[10] \n13. Consider the implementation of the stack using a partially-filled array. What goes wrong \nif we try to store the top of the stack at location [0] and the bottom of the stack at the last \nused position of the array? \no A. Both peek and pop would require linear time. \no B. Both push and pop would require linear time. \no C. The stack could not be used to check balanced parentheses. \no D. The stack could not be used to evaluate postfix expressions. \n14. In the linked list implementation of the stack class, where does the push member function \nplace the new entry on the linked list? \no A. At the head \no B. At the tail \no C. After all other entries that are greater than the new entry. \no D. After all other entries that are smaller than the new entry. \n15. In the array version of the stack class (with a fixed-sized array), which operations require \nlinear time for their worst-case behavior? \no A. is_empty \no B. peek \no C. pop \no D. push \no E. None of these operations require linear time. \n16. In the linked-list version of the stack class, which operations require linear time for their \nworst-case behavior? \no A. is_empty \no B. peek \no C. pop \no D. push \no E. None of these operations require linear time. \nMultiple Choice \nSection 7.3 \nImplementations of \nthe stack ADT \nMultiple Choice \nModule II-Stacks, Queues, Recursion \n \n \n Dr. Ganga Holi, ISE Dept., Global academy of Technology \n29 \n17. What is the value of the postfix expression 6 3 2 4 + - *: \no A. Something between -15 and -100 \no B. Something between -5 and -15 \no C. Something between 5 and -5 \no D. Something between 5 and 15 \no E. Something between 15 and 100 \n18. Here is an infix expression: 4+3*(6*3-12). Suppose that we are using the usual stack \nalgorithm to convert the expression from infix to postfix notation. What is the maximum \nnumber of symbols that will appear on the stack AT ONE TIME during the conversion of \nthis expression? \no A. 1 \no B. 2 \no C. 3 \no D. 4 \no E. 5 \n \n \nSection 7.4 \nMore Complex \nStack Applications \n"} {"id": "DSA Assignment 2-1", "local_path": "local_pdfs\\DSA Assignment 2-1.pdf", "text": ""} {"id": "DSA Mod2@AzDOCUMENTS.in-1", "local_path": "local_pdfs\\DSA Mod2@AzDOCUMENTS.in-1.pdf", "text": "Scanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\nScanned by CamScanner\n"} {"id": "DSA-Module-III Notes", "local_path": "local_pdfs\\DSA-Module-III Notes.pdf", "text": " \n \n \n \n \n \n \nData Structure & Applications \nModule III Notes \n \n \nDr. Ganga Holi, \n \nProfessor & Head, \n \n \n \n \n \nDepartment of Information Science & Engineering \nGLOBAL ACADEMY OF TECHNOLOGY \nRajarajeshwari Nagar, Bangalore-560 098. \n \n \n \n \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n2 \nTable of Contents \nTable of Contents .................................................................................................................................... 2 \n1 \nLinked List Definition .................................................................................................................... 3 \n2 \nRepresentation of Linked List in Memory ............................................................................... 4 \n3 \nMemory Allocation and Deallocation ........................................................................................ 7 \n4 \nLinked List Operations .................................................................................................................. 8 \n1. \nNode creation .......................................................................................................................... 8 \n2. \nInsertion: .................................................................................................................................. 8 \n3. \nDeletion ..................................................................................................................................... 9 \n4. \nTraversing & Displaying ................................................................................................... 10 \n5 \nWhat is node? How it is created? ............................................................................................. 11 \n6 \nWrite functions to insert the node at front and rear end. ............................................... 11 \n7 \nWrite functions to delete node from front and rear end. .................................................. 12 \n8 \nWrite functions to insert into ordered linked list ............................................................... 13 \n9 \nWrite a function to delete a specified element. .................................................................... 14 \n10 \nWrite a function to insert at specified position ............................................................... 15 \n11 \nWrite a function to delete element from specified position ......................................... 16 \n12 \nWrite a function to traverse the linked list and display alternative nodes. ........... 16 \n13 \nWrite a function to reverse the linked list. ....................................................................... 17 \n14 \nWrite a program to insert at front, rear, in ordered, at specified position, and \ndelete from front, rear, ordered, element and from specified position. ............................... 20 \n15 \nWrite a program to insert, delete from both ends and insert & delete from \nOrdered using circular single linked list. ...................................................................................... 25 \n16 \nWrite a program to insert and delete from circular single linked list with head \nnode. 31 \n17 \nWrite a program to insert & delete from both ends with first & last pointer. ....... 38 \n18 \nWrite a progrm to insert, delete from both ends and insert into ordered DLL and \ndelete from Ordered DLL. ................................................................................................................... 42 \n19 \nWrite a program to insert, delete from both ends and insert into ordered DLL \nand delete from Ordered DLL with header node, and last pointer. ...................................... 46 \n20 \nWrite a program to create linked to represent polynomial equation and evaluate \nthe equation with one variable(x7 + 8x – 43). .............................................................................. 50 \n21 \nWrite a program to create linked to represent polynomial equation and add two \npolynomial the equation with one variable P1: 2x7 + 83x +4x+5 & p2: 4x7 +5x2+18x \n+3. \n53 \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n3 \n \n1 Linked List Definition \nList is a linear collection of data items. List can be implemented using arrays and linked \nlists. In arrays there is linear relationship between the data elements which is evident \nfrom the physical relationship of data in the memory. The address of any element in the \narray can easily be computed but, it is very difficult to insert and delete any element in \nan array. Usually, a large block of memory is occupied by an array which may not be in \nuse and it is difficult to increase the size of an array, if required. \nAnother way of storing a list is to have each element in a list contain a field called a link \nor pointer, which contains the address of the address of the next element in the list. \nThe successive elements in the list need not occupy adjacent space in memory. This \ntype of data structure is called linked list. \nLinked List is a linear data structure and it is very common data structure which \nconsists of group of nodes in a sequence which is divided in two parts. Each node \nconsists of its own data and the address of the next node and forms a chain. Linked \nLists are used to create trees and graphs. \n \n \n1.1.1.1 Advantages of Linked Lists \n• \nThey are a dynamic in nature which allocates the memory when required. \n• \nInsertion and deletion operations can be easily implemented. \n• \nStacks and queues can be easily executed. \n• \nLinked List reduces the access time. \n1.1.1.2 Disadvantages of Linked Lists \n• \nThe memory is wasted as pointers require extra memory for storage. \n• \nNo element can be accessed randomly; it has to access each node sequentially. \n• \nReverse Traversing is difficult in single linked list. \n1.1.1.3 Applications of Linked Lists \n• \nLinked lists are used to implement stacks, queues, graphs, etc. \n• \nLinked lists let you insert elements at the beginning and end of the list. \n• \nIn Linked Lists we don’t need to know the size in advance. \n1.1.1.4 Types of Linked Lists \n• \nSingly Linked List : Singly linked lists contain nodes which have a data part \nas well as an address part i.e. next, which points to the next node in sequence \nof nodes. The operations we can perform on singly linked lists are insertion, \ndeletion and traversal. \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n4 \n \n• \nDoubly Linked List : In a doubly linked list, each node contains two links the \nfirst link points to the previous node and the next link points to the next node \nin the sequence. \n \n• \nCircular Linked List: In the circular linked list the last node of the list contains \nthe address of the first node and forms a circular chain. \n \n2 Representation of Linked List in Memory \nList can be represented using arrays and linked list. \nArray Representation: \nLinked list can be implemented using arrays. One array will be used for storing the \ninformation field and another array is used for storing the links. START variable \nwill hold the beginning node address and INFO[9] has value and LINK[9] has the \naddress of another node. Here \nLINK[9]=3 LINK[3]=6 \nLINK[6]=11 LINk[11]=7 \nLINK[7]]=10 LINK[10]=4 \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n5 \nLINKJ[4]=0 which is the last node in the linked list \n \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n6 \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n7 \n \n3 Memory Allocation and Deallocation \n Memory cane be allocated on heap if linked list is used and free function is used to \ndeallocate the memory. \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n8 \n4 Linked List Operations \n1. \nNode creation \nA linked-list is a sequence of nodes which are connected together via links. \nLinked List is a sequence of links which contains items. Each link contains a \nconnection to another link. Linked list the second most used data structure \nafter array. Following are important terms to understand the concepts of \nLinked List. \n• \nINFO/Link − Each Link of a linked list can store a data called an \nelement. \n• \nNext/Ptr − Each Link of a linked list contain a link to next link \ncalled Next. \n• \nLinkedList − A LinkedList contains the connection link to the first \nLink called First. \n \n \nAs per above shown illustration, following are the important points to be \nconsidered. \n• \nLinked List contains a pointer variable called first. \n• \nEach node carries a data field(s) and a pointer Field called next. \n• \nEach node is linked with its next pointer using its next pointer. \n• \nLast node carries a pointer as null to mark the end of the list. \n \nNode can be created by using malloc function \nsturct node { \nint data; \nstruct node *next; \n}; \ntypedef struct node NODE; \nNODE *first=NULL; \n \nNODE *temp; \n \nTemp=(NODE*)malloc(siezof(NODE)); \n2. \nInsertion: \nInsertion of node can be done in various ways. 1. Insertion at \nfront. 2. Insert at rear end 3. Insert into ordered list 4. Insert at \nspecified position. \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n9 \n \nvoid addrear(int data) \n{ NODE *temp,*cur; \n temp=(NODE*)malloc(sizeof(NODE)); \n temp->data=data; temp->next=NULL; \n if(first==NULL) \n temp=first; return; \n cur=first; \n while(cur->next != NULL) \n { cur=cur->next; \n } \n cur->next =temp; \n return ; \n} \n \nvoid addfront(int data) \n{ NODE *temp; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n temp->data=data; temp->next=NULL: \n \n if (first== NULL) \n \n \nfirst=temp; \n else \n { temp->next=first; \n first=temp; \n } \n return ; \n} \n \n3. \nDeletion \nDeletion of node can be done in various ways. 1. Deletion at from. 2. \nDeletion at rear end. 3. Deletion into ordered list 4. Deletion at \nspecified position. \nvoid deletefront() \n{ \n NODE *temp; int num; \n temp=first; \n if(first==NULL) \n { \n \nprintf(\"List is Empty\"); return; \n } \n \n \n printf(“deleted element is %d\\n”, first->data); \n \n first=first->next; \n free(temp); \n return ; \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n10 \n} \n \nvoid deleterear() \n{ \n NODE *cur, *prev; \n cur=first; \n prev=NULL; \n if(first==NULL) \n { \n \nprintf(\"List is Empty\"); return; \n } \n if(first->next==NULL) \n { \nfirst=NULL; \n \n \nfree(cur); return; \n } \n while(cur->next!=NULL) \n { prev=cur; \n \n cur=cur->next; \n } \n prev->next=NULL; \n free(cur); \n return; \n} \n \n \n4. \nTraversing & Displaying \nvoid display() \n{ NODE *r; \n \n r=first; \n printf(\"Data \\n\"); \n if(r==NULL) \n return; \n while(r!=NULL) \n { printf(“%d→”, r->data); \nr=r->next; \n } \n} \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n11 \nWhat is node? How it is created? \n#include \n#include \nstruct node \n{ \n \n \nint item; \n \n \nstruct node *next; \n}; \n \n \ntypedef struct node NODE; \nNODE *first=NULL; \n \nNode is created by calling malloc function and storing the return value in \npointer to node variable. \n \n5 Write functions to insert the node at front and rear end. \nstruct node \n{ \n \nint item; \n \nstruct node *next; \n}; \n \ntypedef struct node NODE; \n \nNODE* insertfront(NODE* first, int data) \n{ \n \nNODE* temp; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n \nif(temp==NULL) \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return NULL; \n \n} \n \ntemp→item=data; \n \ntemp→next=NULL; \n \nif(first==NULL) \n \n \nfirst=temp; \n \nelse \n \n \n{ \n \n \ntemp→next=first; \n \n \nfirst=temp; \n \n} \n \nreturn first; \n} \n \nNODE* insertrear(NODE* first, int data) \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n12 \n{ \n \nNODE *temp, *cur; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n \nif(temp==NULL) \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return NULL; \n \n} \n \ntemp→item=data; \n \ntemp→next=NULL; \n \nif(first==NULL) \n \n \nfirst=temp; \n \nelse \n \n \n{ \n \n \ncur=first; \n \n \nwhile(cur→next!=NULL) \n \n \n{ \n \n \n \n \ncur=cur→next; \n \n \n} \n \n \ncur→next=temp; \n \n} \n \nreturn first; \n} \n \nvoid display(NODE* first) \n{ \nNODE *cur; \n \nif(first==NULL) \n \n{ \n \n \nprintf(\" Empty List\\n\"); return; \n \n} \n \ncur=first; \n \nwhile(cur!=NULL) \n \n{ \n \n \nprintf(\"%d→\", cur→item); \n \n \ncur=cur→next; \n \n} \n \nprintf(\"NULL\"); \n} \n \n \n6 Write functions to delete node from front and rear end. \nNODE* deletefront(NODE* first) \n{ \n \nNODE *temp; \ntemp=first; \nif(first==NULL) \n{ \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n13 \n \n \nprintf(\"List is Empty\"); return first; \n } \n \nprintf(\"Front element deleted =%d\\n\",first→item); \nfirst=first→next; \nfree(temp); \nreturn first; \n} \n \nNODE* deleterear(NODE* first) \n{ \nNODE *cur, *prev; \ncur=first; \nif(first==NULL) \n \n{ \n \n \nprintf(\"List is Empty\"); return first; \n \n} \n \nif(first→next==NULL) \n{ \nprintf(\"Deleted *** rear element=%d\\n\",first→item); \n \n \nfirst=NULL; \n \n \nfree(cur); return first; \n \n} \n \nprev=NULL; \nwhile(cur→next!=NULL) \n{ \nprev=cur; \n \ncur=cur→next; \n} \nprev→next=cur→next; \nprintf(\"Deleted Rear element=%d\\n\",cur→item); \nfree(cur); \nreturn first; \n} \n \n \n7 Write functions to insert into ordered linked list \n \nNODE* orderedInsert(NODE* first, int ele) \n{ \n \nNODE *nn, *pre, *cur; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=ele; nn→next=NULL; \n \nif(first==NULL) \n \n{ \n \n \nfirst=nn; return first; \n \n} \n \nif(first→item >ele) \n \n{ \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n14 \n \n \nnn→next=first; \n \n \nfirst=nn; \n \n \nreturn first; \n \n} \n \n \n \npre=first; cur=first; \n \nwhile(cur→item <=ele && cur→next !=NULL) \n \n{ pre=cur; \n \n \ncur=cur→next; \n \n} \n \nif(cur→item >ele) \n \n{ \npre→next=nn; \n \n \nnn→next=cur; \n \n} \n \nelse \n \n{ \n \n \ncur→next=nn; \n \n} \n \nreturn first; \n} \n \n \n8 Write a function to delete a specified element. \n \nNODE* deleteFromordered(NODE* first, int ele) \n{ \n \nNODE *pre, *cur; \n \nif(first==NULL) \n \n{ \n \n \nprintf(\"Empty list\\n\"); return first; \n \n} \n \ncur=first; \n \nif(first→item ==ele) \n \n{ \n \n \nfirst=NULL; \n \n \nprintf(\"Deleted Rear element=%d\\n\",cur→item); \n \n \nfree(cur); \n \n \nreturn first; \n \n} \n \npre=first; cur=first; \n \nwhile(cur→item !=ele && cur→next !=NULL) \n \n{ \npre=cur; \n \n \ncur=cur→next; \n \n} \n \n \nif(cur-->item !=ele && cur-->next ==NULL) \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n15 \n \n{ \n \n \nprintf(\"Element not found\\n\"); return first; \n \n} \n \npre→next=cur→next; \n} \n \nreturn first; \n} \n \n \n \n9 Write a function to insert at specified position \nNODE* InsertAtPos(NODE* first, int ele, int pos) \n{ \n \nNODE *nn, *pre, *cur; int count=1; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=ele; nn→next=NULL; \n \nif(first==NULL) \n \n{ \n \n \nfirst=nn; return first; \n \n} \n \nif(pos==1) \n \n{ \n \n \nnn→next=first; \n \n \nfirst=nn; \n \n \nreturn first; \n \n} \n \n \n \npre=first; cur=first; \n \nwhile(count < pos && cur→next !=NULL) \n \n{ \npre=cur; count++; \n \n \ncur=cur→next; \n \n} \n \nif(count >= pos) \n \n{ \npre→next=nn; \n \n \nnn→next=cur; \n \n} \n \nelse \n \n{ \n \n \ncur→next=nn; \n \n} \n \nreturn first; \n} \n \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n16 \n10 Write a function to delete element from specified position \nNODE* DeleteFromPos(NODE* first, int pos) \n{ \n \nNODE *pre, *cur; int count=1; \n \npre=NULL; cur=first; \n \nif(first==NULL) \n { \n \nprintf(\"List is Empty\"); return first; \n \n} \n \n \nif(pos==1) \n \n{ \n \n \nprintf(\"Front element deleted =%d\\n\",first→item); \n \n \nfirst=first→next; \n \nfree(cur); \n \nreturn first; \n \n} \n \npre=first; cur=first; \n \nwhile(count != pos && cur→next !=NULL) \n \n{ \npre=cur; count++; \n \n \ncur=cur→next; \n \n} \n \nif(count!=pos && cur-->next==NULL) \n \n{ \n \n \nprintf(\" position is not found\\n\"); \n \n} \n \nif(count == pos) \n \n{ \npre→next=cur→next; \n \n \nprintf(\"Element deleted %d from pos %d\\n\",pos, first→item); \n \n \nfree(cur); \n \n} \n \nreturn first; \n} \n \n11 Write a function to traverse the linked list and display \nalternative nodes. \nvoid displayalt() \n{ \n \nNODE *temp; \n \ntemp=first; \n \nif(first==NULL) \n \n{ printf(\"empty list\\n\"); return; \n \n} \n \nwhile(temp!=NULL) \n \n{ \n \n \nprintf(\"%d→\",temp→item); \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n17 \n \n \nif(temp→next==NULL) break; \n \n \ntemp=temp→next→next; \n \n} \n \n \n \nprintf(\"\\ndone\\n\"); \n} \n \nint main() \n{ \n \nint i,ele; \n \nprintf(\"enter 4 elements=\"); \n \nfor(i=0;i<5;i++) \n \n{ \n \n \nprintf(\"enter ele=\"); \n \n \nscanf(\"%d\",&ele); \n \n \norderedInsert(ele); \n \n \ndisplay(); \n \n} \n \nprintf(\" Display alternate elements\\n\"); \n \ndisplayalt(); \n \n} \n \n12 Write a function to reverse the linked list. \n#include \n#include \nstruct node \n{ \n \nint item; \n \nstruct node *next; \n}; \n \ntypedef struct node NODE; \nNODE *first=NULL; \nNODE *revFirst=NULL; \nNODE *newfirst=NULL; \nvoid orderedInsert(int ele) \n{ \n \nNODE *nn, *pre, *cur; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=ele; nn-->next=NULL; \n \nif(first==NULL) \n \n{ \n \n \nfirst=nn; return; \n \n} \n \nif(first→item >ele) \n \n{ \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n18 \n \n \nnn→next=first; \n \n \nfirst=nn; return; \n \n} \n \n \n \n \npre=first; cur=first; \n \nwhile(cur→item <=ele && cur→next !=NULL) \n \n{ \npre=cur; \n \n \ncur=cur→next; \n \n} \n \nif(cur→item >ele) \n \n{ \npre→next=nn; \n \n \nnn→next=cur; \n \n} \n \nelse \n \n{ \n \n \ncur→next=nn; \n \n} \n} \n \nvoid display(NODE *first) \n{ \n \nNODE *temp; \n \ntemp=first; \n \nif(first==NULL) \n \n{ printf(\"empty list\\n\"); return; \n \n} \n \nwhile(temp!=NULL) \n \n{ \n \n \nprintf(\"%d→\",temp→item); temp=temp→next; \n \n} \n \nprintf(\"\\ndone\\n\"); \n} \n \nNODE* insertFront(NODE *revFirst,int item) \n{ \n \nNODE *nn; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=item; nn→next=NULL; \n \nif(revFirst==NULL) \n \n \nrevFirst=nn; \n \nelse \n \n{ \n \n \nnn→next=revFirst; \n \n \nrevFirst=nn; \n \n} \n \nreturn revFirst; \n} \n \nvoid reverse1() \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n19 \n{ \n struct node *nn,*prev = NULL; \n struct node *current = first; \n while (current!= NULL) \n { revFirst=insertFront(revFirst,current→item); \n \nprev = current; \ncurrent = current→next; \n } \n} \n \nNODE* reverse2() \n{ \n struct node* prev = NULL; \n struct node* current = first; \n struct node* next; \n while (current != NULL) \n { \n next = current→next; \n current→next = prev; \n prev = current; \n current = next; \n } \n newfirst = prev; \n} \n \n \nint main() \n{ \n \nint i,ele,n; \n \nprintf(\"enter n=\"); scanf(\"%d\",&n); \n \nprintf(\"enter %d elements=\",n); \n \nfor(i=0;i \n#include \nstruct node \n{ \n \nint item; \n \nstruct node *next; \n}; \n \ntypedef struct node NODE; \nNODE *head=NULL; \n \nvoid insertfront(int data) \n{ \n \nNODE* temp; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n \nif(temp==NULL) \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return ; \n \n} \n \ntemp→item=data; \n \ntemp→next=NULL; \n \nif(head→next==NULL) \n \n{ \n \n \n \nhead→next=temp; head→item++; \n \n} \n \nelse \n \n \n{ \n \n \ntemp→next=head→next; \n \n \nhead→next=temp; head→item++; \n \n} \n \nreturn; \n} \n \n \n \nvoid insertrear(int data) \n{ \n \nNODE *temp, *cur; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n21 \n \nif(temp==NULL) \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return ; \n \n} \n \ntemp-->item=data; \n \ntemp→next=NULL; \n \nif(head→next==NULL) \n \n{ \n \n \nhead→next=temp; \n \n} \n \nelse \n \n \n{ \n \n \ncur=head→next; \n \n \nwhile(cur→next!=NULL) \n \n \n{ \n \n \n \n \ncur=cur→next; \n \n \n} \n \n \ncur→next=temp; \n \n} \n \nhead→item++; \n \n \nreturn ; \n} \n \n \nvoid deletefront() \n{ \nNODE *temp; int num; \ntemp=head→next; \nif(head→next==NULL) \n{ \n \nprintf(\"List is Empty\"); return; \n} \nprintf(\"Front element deleted =%d\\n\",temp-->item); \nhead→next=temp→next; head→item--; \nfree(temp); \nreturn ; \n} \n \nvoid deleterear() \n{ \nNODE *cur, *prev; \ncur=head→next; \nif(head→next==NULL) \n \n{ \nprintf(\"List is Empty\"); return; \n \n} \n \nif(cur→next==NULL) \n{ \nprintf(\"Deleted *** rear element=%d\\n\",cur→item); \n \n \nhead→next=NULL; head→item--; \n \n \nfree(cur); return ; \n \n} \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n22 \n \nprev=NULL; \n \nwhile(cur→next!=NULL) \n{ \nprev=cur; \n \n \ncur=cur→next; \n \n} \n \nprev→next=cur→next; head→item--; \nprintf(\"Deleted Rear element=%d\\n\",cur→item); \nfree(cur); \nreturn ; \n} \n \nvoid orderedInsert(int ele) \n{ \n \nNODE *nn, *pre, *cur, *first; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=ele; nn→next=NULL; \n \nfirst=head→next; \n \nif(first==NULL) \n \n{ \n \n \nhead→next=nn; head→item++; return; \n \n} \n \nif(first→item >ele) \n \n{ \n \n \nnn→next=first; \n \n \nhead→next=nn; head→item++; \n \n \nreturn; \n \n} \n \npre=first; cur=first; \n \nwhile(cur→item <=ele && cur→next !=NULL) \n \n{ \npre=cur; \n \n \ncur=cur→next; \n \n} \n \nif(cur→item >ele) \n \n{ \npre→next=nn; \n \n \nnn→next=cur; head→item++; \n \n} \n \nelse \n \n{ \n \n \ncur→next=nn; head→item++; \n \n} \n} \n \nvoid deleteFromordered(int ele) \n{ \n \nNODE *pre, *cur; \n \nif(head-->next==NULL) \n \n{ \n \n \nprintf(\"Empty list\\n\"); return ; \n \n} \n \ncur=head→next; \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n23 \n \nif(cur→item ==ele) \n \n{ \n \n \nhead→next=NULL; head→item--; \n \n \nprintf(\"Deleted Rear element=%d\\n\",cur→item); \n \n \nfree(cur); \n \n \nreturn ; \n \n} \n \npre=NULL; \n \nwhile(cur→item !=ele && cur→next !=NULL) \n \n{ \npre=cur; \n \n \ncur=cur→next; \n \n} \n \nif(cur→item !=ele && cur→next ==NULL) \n \n{ \n \n \nprintf(\" Element not found\\n\"); return; \n \n} \n \nif(cur→item == ele) \n \n{ \npre→next=cur→next; head→item--; \n \n} \n \nreturn ; \n} \n \n \nvoid InsertAtPos(int ele, int pos) \n{ \n \nNODE *nn, *pre, *cur; int count=1; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=ele; nn→next=NULL; \n \nif(head→next==NULL) \n \n{ \n \n \nhead→next=nn; head→item++; \nreturn; \n \n} \n \nif(pos==1) \n \n{ \n \n \nnn→next=head→next; \n \n \nhead→next=nn; head→item++; \n \n \n \nreturn; \n \n} \n \n \n \npre=NULL; cur=head→next; \n \nwhile(count < pos && cur→next !=NULL) \n \n{ \npre=cur; count++; \n \n \ncur=cur→next; \n \n} \n \nif(count >= pos) \n \n{ \npre→next=nn; \n \n \nnn→next=cur; \n \n} \n \nelse \n \n{ \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n24 \n \n \ncur→next=nn; \n \n} \n \nhead→item++; \n \n \nreturn ; \n} \n \nvoid DeleteFromPos(int pos) \n{ \n \nNODE *pre, *cur; int count=1; \n \npre=NULL; cur=head-->next; \n \nif(head→next==NULL) \n{ \n \n \nprintf(\"List is Empty\"); return ; \n \n} \n \n \nif(pos==1) \n \n{ \n \n \nprintf(\"Front element deleted =%d\\n\",cur-->item); \n \n \nhead→next=cur→next; head→item--; \n \nfree(cur); \n \nreturn ; \n \n \n} \n \npre=NULL; cur=head→next; \n \nwhile(count != pos && cur→next !=NULL) \n \n{ \npre=cur; count++; \n \n \ncur=cur→next; \n \n} \n \nif(count != pos && cur→next ==NULL) \n \n{ \n \n \nprintf(\"Posotion not valid\\n\"); return; \n \n} \n \nif(count == pos) \n \n{ \npre→next=cur→next; head-->item--; \n \n \nprintf(\"Element deleted %d from pos %d\\n\",pos, cur→item); \n \n \nfree(cur); \n \n} \n \nreturn ; \n} \n \n \nvoid display() \n{ \n \nNODE *temp, *first; \n \ntemp=first=head→next;; \n \nif(first==NULL) \n \n{ \n \nprintf(\"empty list\\n\"); return; \n \n} \n \nwhile(temp!=NULL) \n \n{ \n \n \nprintf(\"%d→\",temp→item); temp=temp→next; \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n25 \n \n} \n \nprintf(\"\\ndone total nodes=%d\\n\", head→item); \n} \n \nint main() \n{ \n \nint i,ele, val, pos; \n \nhead=(NODE*) malloc(sizeof(NODE)); \n \nhead→next=NULL; head-->item=0; \n \nprintf(\"enter 4 elements=\"); \n \nfor(i=0;i<4;i++) \n \n{ \n \n \nprintf(\"enter ele=\"); \n \n \nscanf(\"%d\",&val); \n \n \ninsertfront(val); \n \n \n//orderedInsert(val); \n \n \ndisplay(); \n \n} \n \nprintf(\"\\n item value and position=\"); \n \nscanf(\"%d%d\", &val,&pos); \n \nInsertAtPos(val,pos); \n \ndisplay(); \n \nprintf(\"\\n Enter value to delete =\"); \n \nscanf(\"%d\", &val); \n \ndeleteFromordered(val); \n \ndisplay(); \n \nprintf(\"\\n Enter posotion from which ele to delete =\"); \n \nscanf(\"%d\", &pos); \n \nDeleteFromPos(pos); \n \ndisplay(); \n} \n \n14 Write a program to insert, delete from both ends and insert \n& delete from Ordered using circular single linked list. \n#include \n#include \n \nstruct node \n{ \n \nint item; \n \nstruct node *next; \n}; \n \ntypedef struct node NODE; \n \nNODE* insertfront(NODE* first, int data) \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n26 \n{ \n \nNODE* temp, *cur; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n \nif(temp==NULL) \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return NULL; \n \n} \n \nTemp→item=data; \n \nTemp→next=NULL; \n \nif(first==NULL) \n \n{ \n \n \nfirst=temp; first→next=first; \n \n \n} \n \nelse \n \n \n{ \ncur=first; \n \n \nwhile(cur→next!=first) \n \n \n{ \n \n \n \ncur=cur→next; \n \n \n} \n \n \ntemp→next=first; \n \n \nfirst=temp; \n \n \ncur→next=first; \n \n} \n \nreturn first; \n} \n \nNODE* insertrear(NODE* first, int data) \n{ \n \nNODE *temp, *cur; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n \nif(temp==NULL) \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return NULL; \n \n} \n \ntemp→item=data; \n \ntemp→next=NULL; \n \nif(first==NULL) \n \n{ \nfirst=temp; first→next=first; \n \n} \n \nelse \n \n \n{ \n \n \ncur=first; \n \n \nwhile(cur→next!=first) \n \n \n{ \n \n \n \n \ncur=cur→next; \n \n \n} \n \n \ncur→next=temp; temp→next=first; \n \n} \n \nreturn first; \n} \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n27 \n \nvoid display(NODE* first) \n{ \nNODE *cur; \n \nif(first==NULL) \n \n{ \n \n \nprintf(\" Empty List\\n\"); return; \n \n} \n \ncur=first; \n \nwhile(cur-->next!=first) \n \n{ \n \n \nprintf(\"%d-→\", cur→item); \n \n \ncur=cur→next; \n \n} \n \nprintf(\"%d-→\", cur→item); \n \nprintf(\"NULL\\n\\n\"); \n \n \n} \n \nNODE* deletefront(NODE* first) \n{ \n NODE *temp, *last; int num; \n temp=first; \n if(first==NULL) \n { \n \nprintf(\"List is Empty\"); return first; \n \n} \n \n \nprintf(\"Front element deleted =%d\\n\",first→item); \n \nlast=first; \n \nwhile(last→next!=first) \n \n{ \nlast=last→next; \n \n} \n \nfirst=first→next; \n \nlast→next=first; \n \n \n free(temp); \n return first; \n} \n \nNODE* deleterear(NODE* first) \n{ \n NODE *cur, *prev; \n cur=first; \n if(first==NULL) \n { \n \nprintf(\"List is Empty\"); return first; \n \n} \n \nif(first→next==NULL) \n { printf(\"Deleted *** rear element=%d\\n\",first→item); \n \n \nfirst=NULL; \n \n \nfree(cur); return first; \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n28 \n \n} \n \nprev=NULL; \n \nwhile(cur→next!=first) \n { prev=cur; \n \n \ncur=cur→next; \n } \n prev→next=cur→next; \n printf(\"Deleted Rear element=%d\\n\",cur→item); \n free(cur); \n return first; \n} \n \nNODE* orderedInsert(NODE* first, int ele) \n{ \n \nNODE *nn, *pre, *cur, *last; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=ele; nn→next=NULL; \n \nif(first==NULL) \n \n{ \n \n \nfirst=nn; first→next=first; first; \n \n} \n \nif(first→item >ele) \n \n{ \n \n \nnn→next=first; \n \n \nlast=first; \n \n \nwhile(last→next!=first) \n \n \n{ \nlast=last→next; \n \n \n} \n \n \nfirst=nn; \n \n \nlast→next=first; \n \n \nreturn first; \n \n} \n \n \n \npre=first; cur=first; \n \nwhile(cur→item <=ele && cur→next !=NULL) \n \n{ \npre=cur; \n \n \ncur=cur→next; \n \n} \n \nif(cur→item >ele) \n \n{ \npre→next=nn; \n \n \nnn→next=cur; \n \n} \n \nelse \n \n{ \n \n \ncur→next=nn; nn→next=first; \n \n} \n \nreturn first; \n} \n \nNODE* deleteFromordered(NODE* first, int ele) \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n29 \n{ \n \nNODE *pre, *cur,*last; \n \nif(first==NULL) \n \n{ \n \n \nprintf(\"Empty list\\n\"); return first; \n \n} \n \ncur=first; \n \nif(first→item ==ele) \n \n{ \n \n \nfirst=first→next; \n \n \nprintf(\"Deleted Rear element=%d\\n\",cur→item); \n \n \nlast=first; \n \n \nwhile(last→next!=first) \n \n \n{ \nlast=last→next; \n \n \n} \n \n \nlast→next=first; \n \n \nfree(cur); \n \n \nreturn first; \n \n} \n \npre=first; cur=first; \n \nwhile(cur→item !=ele && cur→next !=first) \n \n{ \npre=cur; \n \n \ncur=cur→next; \n \n} \n \nif(cur→item !=ele && cur→next ==first) \n \n{ \n \n \nprintf(\"Element not found\\n\"); return first; \n \n} \n \npre→next=cur→next; free(cur); \n \nreturn first; \n} \n \n \nNODE* InsertAtPos(NODE* first, int ele, int pos) \n{ \n \nNODE *nn, *pre, *cur, *last; int count=1; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=ele; nn→next=NULL; \n \nif(first==NULL) \n \n{ \n \n \nfirst=nn; first→next=first; return first; \n \n} \n \nif(pos==1) \n \n{ \n \n \nnn→next=first; \n \n \nlast=first; \n \n \nwhile(last→next!=first) \n \n \n{ \nlast=last→next; \n \n \n} \n \n \nfirst=nn; last→next=first; \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n30 \n \n \nreturn first; \n \n} \n \npre=NULL; cur=first; \n \nwhile(count < pos && cur→next !=NULL) \n \n{ \npre=cur; count++; \n \n \ncur=cur→next; \n \n} \n \nif(count >= pos) \n \n{ \npre→next=nn; \n \n \nnn→next=cur; \n \n} \n \nelse \n \n{ \n \n \ncur→next=nn; nn→next=first; \n \n} \n \nreturn first; \n} \n \nNODE* DeleteFromPos(NODE* first, int pos) \n{ \n \nNODE *nn, *pre, *cur, *last; int count=1; \n \npre=NULL; cur=first; \n \nif(first==NULL) \n{ \n \n \nprintf(\"List is Empty\"); return first; \n \n} \n \n \nif(pos==1) \n \n{ \n \n \nprintf(\"Front element deleted =%d\\n\",first→item); \n \n \nlast=first; \n \n \nwhile(last→next!=first) \n \n \n{ \nlast=last→next; \n \n \n} \n \n \nfirst=first→next; last→next=first; \n \n \nfree(cur); \nreturn first; ; \n \n \n} \n \npre=NULL; cur=first; \n \nwhile(count != pos && cur→next !=NULL) \n \n{ \npre=cur; count++; \n \n \ncur=cur→next; \n \n} \n \nif(count!=pos && cur→next==NULL) \n \n{ \n \n \nprintf(\" position is not found\\n\"); \n \n} \n \nif(count == pos) \n \n{ \npre→next=cur→next; \n \n \nprintf(\"Element deleted %d from pos %d\\n\",pos, first→item); \n \n \nfree(cur); \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n31 \n \n} \n \nreturn first; \n} \n \n \n \nint main() \n{ int val, n,i,pos; \n \nNODE *first=NULL; \n \nprintf(\" Enter n of nodes n=\"); \n \nscanf(\"%d\", &n); \n \nfor(i=0;i \n#include \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n32 \nstruct node \n{ \n \nint item; \n \nstruct node *next; \n}; \n \ntypedef struct node NODE; \nNODE *head=NULL; \n \nvoid insertfront(int data) \n{ \n \nNODE* temp, *last; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n \nif(temp==NULL) \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return ; \n \n} \n \ntemp→item=data; \n \ntemp→next=NULL; \n \nif(head→next==NULL) \n \n{ \nprintf(\" First element\\n\\n\"); \n \n \nhead→next=temp; temp→next=temp; head→item++; \n \n} \n \nelse \n \n \n{ \n \n \ntemp→next=head→next; \n \n \nlast=head→next; \n \n \nwhile(last→next!=head→next) \n \n \n{ \n \n \n \nlast=last→next; \n \n \n} \n \n \nhead→next=temp; \n \n \nlast→next=head→next; head→item++; \n \n} \n \nreturn; \n} \n \nvoid insertrear(int data) \n{ \n \nNODE *temp, *cur; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n \nif(temp==NULL) \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return ; \n \n} \n \ntemp→item=data; \n \ntemp→next=NULL; \n \nif(head→next==NULL) \n \n{ \n \n \nhead→next=temp; temp→next=temp; \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n33 \n \n} \n \nelse \n \n \n{ \n \n \ncur=head→next; \n \n \nwhile(cur→next!=head→next) \n \n \n{ \n \n \n \n \ncur=cur→next; \n \n \n} \n \n \ncur→next=temp; temp→next=head→next; \n \n} \n \nhead→item++; \n \n \nreturn ; \n} \n \n \nvoid deletefront() \n{ \n NODE *temp, *last; int num; \n temp=head-->next; \n if(head→next==NULL) \n { \n \nprintf(\"List is Empty\"); return; \n \n} \n \n \nprintf(\"Front element deleted =%d\\n\",temp→item); \n \n \n \nlast=head→next; \n \nwhile(last→next!=head→next) \n \n{ \n \n \n \nlast=last→next; \n \n} \n \nhead→next=temp→next; \n \nlast→next=head→next; \n \nhead-->item--; \n free(temp); \n return ; \n} \n \nvoid deleterear() \n{ \n NODE *cur, *prev; \n cur=head-->next; \n if(head-->next==NULL) \n { \n \nprintf(\"List is Empty\"); return; \n \n} \n \nif(cur→next==NULL) \n{ \nprintf(\"Deleted *** rear element=%d\\n\",cur→item); \n \n \nhead→next=NULL; head→item--; \n \n \nfree(cur); return ; \n \n} \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n34 \n \nprev=NULL; \n \nwhile(cur→next!=NULL) \n { \nprev=cur; \n \n \ncur=cur→next; \n \n} \nprev→next=cur→next; head→item--; \nprintf(\"Deleted Rear element=%d\\n\",cur→item); \nfree(cur); \nreturn ; \n} \n \nvoid orderedInsert(int ele) \n{ \n \nNODE *nn, *pre, *cur, *last; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=ele; nn→next=NULL; \n \ncur=head→next; \n \nif(head→next==NULL) \n \n{ \n \n \nhead→next=nn; nn→next=nn; head→item++; return; \n \n} \n \nif(cur→item >ele) \n \n{ \n \n \nnn→next=cur; \n \n \n \n \n \nlast=cur; \n \n \nwhile(last→next!=head-->next) \n \n \n{ \n \n \n \nlast=last→next; \n \n \n} \n \n \nhead→next=nn; last→next=head→next; \n \n \nhead→item++; \n \n \nreturn; \n \n} \n \npre=NULL; cur=head→next; \n \nwhile(cur→item <=ele && cur→next !=head→next) \n \n{ \npre=cur; \n \n \ncur=cur→next; \n \n} \n \nif(cur→item >ele) \n \n{ \npre→next=nn; \n \n \nnn→next=cur; head→item++; \n \n} \n \nelse \n \n{ \n \n \ncur→next=nn; nn→next=head→next; head→item++; \n \n} \n} \n \nvoid deleteFromordered(int ele) \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n35 \n{ \n \nNODE *pre, *cur, *last; \n \nif(head→next==NULL) \n \n{ \n \n \nprintf(\"Empty list\\n\"); return ; \n \n} \n \ncur=head→next; \n \nif(cur→item ==ele) \n \n{ if(cur→next==head→next) \n \n \n{ \nhead→next=NULL; \n \n \n} \n \n \nelse \n \n \n{ \n \n \n \nlast=head→next; \n \n \n \nwhile(last→next!=head→next) \n \n \n \n{ \n \n \n \n \nlast=last→next; \n \n \n \n} \n \n \n \nhead→next=cur→next; \n \n \n \nlast→next=head→next; \n \n \n} \n \n \nprintf(\"Deleted Rear element=%d\\n\",cur→item); \n \n \nfree(cur); \n \n \nhead→item--; \n \n \nreturn ; \n \n} \n \npre=NULL; \n \nwhile(cur→item !=ele && cur→next !=head→next) \n \n{ \npre=cur; \n \n \ncur=cur→next; \n \n} \n \nif(cur→item !=ele && cur→next ==NULL) \n \n{ \n \n \nprintf(\" Element not found\\n\"); return; \n \n} \n \nif(cur→item == ele) \n \n{ \npre→next=cur→next; free(cur); head→item--; \n \n} \n \nreturn ; \n} \n \nvoid InsertAtPos(int ele, int pos) \n{ \n \nNODE *nn, *pre, *cur, *last; int count=1; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=ele; nn→next=NULL; \n \nif(head→next==NULL) \n \n{ \n \n \nhead→next=nn; nn→next=nn; head→item++; \nreturn; \n \n} \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n36 \n \nif(pos==1) \n \n{ \n \n \nnn→next=head→next; \n \n \nlast=head→next; \n \n \nwhile(last→next!=head→next) \n \n \n{ \n \n \n \nlast=last→next; \n \n \n} \n \n \nhead→next=nn; \n \n \nlast→next=head→next; head→item++; \n \n \n \nreturn; \n \n} \n \n \n \npre=NULL; cur=head→next; \n \nwhile(count < pos && cur→next !=head→next) \n \n{ \npre=cur; count++; \n \n \ncur=cur→next; \n \n} \n \nif(count >= pos) \n \n{ \npre→next=nn; \n \n \nnn→next=cur; \n \n} \n \nelse \n \n{ \n \n \ncur→next=nn; nn→next=head→next; \n \n} \n \nhead→item++; \n \n \nreturn ; \n} \n \nvoid DeleteFromPos(int pos) \n{ \n \nNODE *pre, *cur, *last; int count=1; \n \npre=NULL; cur=head→next; \n \nif(head→next==NULL) \n { \n \nprintf(\"List is Empty\"); return ; \n \n} \n \n \nif(pos==1) \n \n{ \n \n \nprintf(\"Front element deleted =%d\\n\",cur→item); \n \n \nlast=head→next; \n \n \nwhile(last→next!=head→next) \n \n \n{ \n \n \n \nlast=last→next; \n \n \n} \n \n \nhead→next=cur→next; \n \n \nlast→next=head→next; head→item--; \n \n \nfree(cur); \n \n \nreturn ; \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n37 \n \n} \n \npre=NULL; cur=head→next; \n \nwhile(count != pos && cur→next !=NULL) \n \n{ \npre=cur; count++; \n \n \ncur=cur→next; \n \n} \n \nif(count != pos && cur→next ==NULL) \n \n{ \n \n \nprintf(\"Posotion not valid\\n\"); return; \n \n} \n \nif(count == pos) \n \n{ \npre→next=cur→next; head→item--; \n \n \nprintf(\"Element deleted %d from pos %d\\n\",pos, cur→item); \n \n \nfree(cur); \n \n} \n \nreturn ; \n} \n \n \nvoid display() \n{ \n \nNODE *temp; \n \ntemp=head→next; \n \nif(head→next==NULL) \n \n{ \nprintf(\"empty list\\n\"); return; \n \n} \n \nwhile(temp→next!=head→next) \n \n{ \n \n \nprintf(\"%d→\",temp→item); temp=temp→next; \n \n} \n \nprintf(\"%d→\",temp→item); \n \nprintf(\"\\n \\nDone total nodes=%d\\n\", head→item); \n} \n \nint main() \n{ \n \nint i,ele, val, pos,n; \n \nhead=(NODE*) malloc(sizeof(NODE)); \n \nhead→next=NULL; head→item=0; \n \nprintf(\"enter n=\"); \n \nscanf(\"%d\",&n); \n \nprintf(\"enter %d elements=\",n); \n \nfor(i=0;i \n#include \n \nstruct node \n{ \n \nint item; \n \nstruct node *next; \n}; \n \ntypedef struct node NODE; \nvoid insertfront(NODE** first, NODE **last, int data) \n{ \n \nNODE* temp; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n \nif(temp==NULL) \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return ; \n \n} \n \nTemp→item=data; \n \nTemp→next=NULL; \n \nif(*first==NULL) \n \n{ \n \n \n*first=temp; *last=temp; \n \n} \n \nelse \n \n \n{ \n \n \ntemp→next=*first; \n \n \n*first=temp; \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n39 \n \n} \n \nreturn; \n} \n \nvoid insertrear(NODE** first, NODE **last, int data) \n{ \n \nNODE *temp, *cur; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n \nif(temp==NULL) \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return ; \n \n} \n \ntemp→item=data; \n \ntemp→next=NULL; \n \nif(*first==NULL) \n \n{ \n \n \n \n*first=temp; *last=temp; \n \n} \n \n \nelse \n \n \n{ \n \n \ncur=*last; \n \n \ncur→next=temp; \n \n \n*last=temp; \n \n \n \n \n} \n \nreturn ; \n} \n \nvoid display(NODE* first) \n{ \nNODE *cur; \n \nif(first==NULL) \n \n{ \n \n \nprintf(\" Empty List\\n\"); return; \n \n} \n \ncur=first; \n \nwhile(cur!=NULL) \n \n{ \n \n \nprintf(\"%d→\", cur->item); \n \n \ncur=cur→next; \n \n} \n \nprintf(\"NULL\\n\\n\"); \n} \n \nvoid deletefront(NODE** first, NODE **last) \n{ \n \nNODE *temp; int num; \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n40 \n \ntemp=*first; \n \nif(*first==NULL) \n \n{ \n \n \nprintf(\"List is Empty\"); return ; \n \n} \n \n \nprintf(\"Front element deleted =%d\\n\",temp→item); \n \n*first=temp→next; \n \nfree(temp); \n \nreturn ; \n} \n \nNODE* deleterear(NODE** first, NODE **last) \n{ \n \nNODE *cur, *prev; \n \ncur=*first; \n \nif(*first==NULL) \n \n{ \n \n \nprintf(\"List is Empty\"); return; \n \n} \n \nif(cur→:next==NULL) \n \n{ \nprintf(\"Deleted *** rear element=%d\\n\",cur→item); \n \n \n*first=NULL; \n \n \nfree(cur); return ; \n \n} \n \nprev=NULL; \n \nwhile(cur→next!=NULL) \n{ \nprev=cur; \n \n \ncur=cur→next; \n \n} \n \nPrev→next=cur→next; \n \nprintf(\"Deleted Rear element=%d\\n\",cur→item); \n \nfree(cur); \n \nreturn; \n} \n \nNODE* orderedInsert(NODE** first, NODE **last, int ele) \n{ \n \nNODE *nn, *pre, *cur; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=ele; nn→next=NULL; \n \ncur=*first; \n \nif(*first==NULL) \n \n{ \n \n \n*first=nn; return ; \n \n} \n \nif(cur→item >ele) \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n41 \n \n{ \n \n \nnn→next=*first; \n \n \n*first=nn; \n \n \nreturn ; \n \n} \n \n \n \npre=NULL; cur=*first; \n \nwhile(cur→item <=ele && cur→next !=NULL) \n \n{ \npre=cur; \n \n \ncur=cur→next; \n \n} \n \nif(cur→item >ele) \n \n{ \npre→next=nn; \n \n \nnn→next=cur; \n \n} \n \nelse \n \n{ \n \n \ncur→next=nn; \n \n} \n \nreturn ; \n} \n \n \nint main() \n{ int val, n,i,pos; \n \nNODE *first=NULL, *last=NULL; \n \nprintf(\" Enter n of nodes n=\"); \n \nscanf(\"%d\", &n); \n \nfor(i=0;i \n#include \n#include \nstruct node \n{ \nint item; \n \nstruct node *next; \n \nstruct node *prev; \n}; \ntypedef struct node NODE; \nNODE *first=NULL; \n \nvoid insertrear(int ele) \n{ \nNODE *temp,*cur, *prev; \n \ntemp= (NODE *)malloc(sizeof(NODE)); \ntemp→item=ele; temp→next=NULL; \nif(first==NULL) \n{ \nfirst=temp; return; \n \n} \n \ncur=first; prev=NULL; \nwhile(cur→next != NULL) \n{ \nprev=cur; \n \n \ncur=cur→next; \n \n} \ncur→next =temp; \ntem→prev=prev; \nreturn ; \n} \n \nvoid insertfront(int ele) \n{ \nNODE *temp; \n \ntemp= (NODE *)malloc(sizeof(NODE)); \n \ntemp→item=ele; temp→next=NULL; \n \nif (first== NULL) \n{ \n \n \nfirst=temp; \n \n} \n \nelse \n{ \ntemp→next=first; \n \n \nfirst→prev=temp; \n \n \nfirst=temp; \n \n} \nreturn ; \n} \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n43 \n \n \nvoid deletefront() \n{ \nNODE *temp; int num; \n \ntemp=first; \nif(first==NULL) \n \n{ \n \n \nprintf(\" List is Empty \\n\");return ; \n \n} \n \nif(first→next==NULL) \n \n \n first=NULL; \n \nelse \n \n{ \nfirst=first→next; \n \n \nFirst→prev=NULL; \n \n} \n \nprintf(\" Element=%d\\n\", temp→item); \n \nfree(temp); \n \nreturn ; \n} \n \nvoid deleterear() \n{ \n \nNODE *cur, *prev; \ncur=first; \n \nprev=NULL; \n \nif(first==NULL) \n \n{ \n \n \nprintf(\" List is Empty \\n\");return ; \n \n} \n \nif(first→next==NULL) \n \n{ \n \n \nfirst=NULL; \n \n} \n \nelse \n \n{ \n \n \nwhile(cur→next!=NULL) \n \n{ \nprev=cur; \n \n \n \ncur=cur→next; \n \n \n} \n \n \nprev→next=NULL; \n \n} \n \nprintf(\" Element=%d\\n\", cur→item); \n \nfree(cur); \n \nreturn; \n} \n \nvoid display() \n{ \nNODE *r; int count=0; \n \nr=first; \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n44 \n \nif(r==NULL) \n \n{ \n \n \nreturn; \n \n} \n \nprintf(\"Doubly Linked List\\n\"); \n \nwhile(r!=NULL) \n{ \nprintf(\"%d-→\",r->item); \n \n \nr=r→next; count++; \n \n} \n \nprintf(\"\\n No. Of Nodes=%d\\n\",count); \n} \n \nvoid insertOrdered(int ele) \n{ \n \nNODE *nn, *pre, *cur; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn→item=ele; nn→next=NULL; nn→prev=NULL; \n \nif(first==NULL) \n \n{ \n \n \nfirst=nn; return; \n \n} \n \nif(first→item >ele) \n \n{ \n \n \nnn→next=first; first→prev=nn; \n \n \nfirst=nn; return; \n \n} \n \npre=NULL; cur=first; \n \nwhile(cur→item <=ele && cur→next !=NULL) \n \n{ \npre=cur; \n \n \ncur=cur→next; \n \n} \n \nif(cur→item >ele) \n \n{ \npre→next=nn; nn→prev=pre; \n \n \nnn→next=cur; cur→prev=nn; \n \n} \n \nelse \n \n{ \n \n \ncur→next=nn; nn→prev=cur; \n \n} \n} \n \nvoid deleteOrdered(int ele) \n{ \n \nNODE *pre, *cur; \n \nif(first==NULL) \n \n{ \n \n \nprintf(\"Empty list\\n\"); return first; \n \n} \n \ncur=first; \n \nif(first→item ==ele) \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n45 \n \n{ \n \n \nfirst=first→next; if(first!=NULL) first→prev=NULL; \n \n \nprintf(\"Deleted Rear element=%d\\n\",cur→item); \n \n \nfree(cur); \n \n \nreturn first; \n \n} \n \npre=NULL; cur=first; \n \nwhile(cur→item !=ele && cur→next !=NULL) \n \n{ \npre=cur; \n \n \ncur=cur→next; \n \n} \n \nif(cur→item !=ele && cur→next ==NULL) \n \n{ \n \n \nprintf(\"Element not found\\n\"); return first; \n \n} \n \npre→next=cur→next; \n \nreturn ; \n} \n \nint main() \n{ int ele; int i, ch; \n first=NULL; \n while(1) \n { \n \nprintf(\"\\nList Operations\\n\"); \n \nprintf(\"===============\\n\"); \n \nprintf(\"1.Insert at Front end\\n\"); \n \nprintf(\"2.Deletion from the Front end\\n\"); \n \nprintf(\"3.Insertion at the Rear end of DLL\\n\"); \n \nprintf(\"4.Deletion from the rear end End of DLL\"); \n \nprintf(\"5.Display DLL\\n\"); \n \nprintf(\"6.Perform Insertion into Ordered DLL\\n\"); \n \nprintf(\"7.Deletion from the Ordered DLL\\n\"); \n \nprintf(\"8.Exit\\n\"); \n \nprintf(\"Enter your choice : \"); \n \nscanf(\"%d\",&i); \n \n \n \nswitch(i) \n { \n case 1 \n: \nprintf(\"Enter Item=\"); scanf(\"%d\",&ele); \n \n \n \n \n \ninsertfront(ele); break; \n \n \ncase 2 \n: \ndeletefront(); break; \n \n \n \n \n \ncase 3 \n: \nprintf(\"Enter Item=\"); scanf(\"%d\",&ele); \n \n \n \n \n \ninsertrear(ele); break; \n \n \ncase 4 \n: \ndeleterear(); break; \n \n \n \n \n \n \ncase 5 \n: \nif(first==NULL) \n \n \n \n \n printf(\"List is Empty\\n\"); \n \n \n \nelse \n \n \n \n \n \ndisplay(); \n \n \n \n \nbreak; \n \n \n \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n46 \n \n \n \ncase 6 \n: \nprintf(\"Enter Item=\"); \nscanf(\"%d\",&ele); \n \n \n \n \n \n \ninsertOrdered(ele); break; \n \n \n \ncase 7 \n: \nprintf(\"Enter Item to delete=\"); \n \n \n \n \n \n \nscanf(\"%d\",&ele); \n \n \n \n \n \n \ndeleteOrdered(ele); break; \n \n \n \ncase 8 \n: \nreturn 0; \n default \n: \nprintf(\"Invalid option\\n\"); \n \n} \n \n} \n \nreturn 0; \n} \n \n18 Write a program to insert, delete from both ends and insert \ninto ordered DLL and delete from Ordered DLL with header \nnode, and last pointer. \n/* Doubly Linked List (DLL) with first and last pointer */ \n \n#include \n#include \n#include \n \nstruct node \n{ \nint item; \n \nstruct node *next; \n \nstruct node *prev; \n}; \ntypedef struct node NODE; \nNODE *head=NULL, *last=NULL; \n \nvoid insertrear(int ele) \n{ \nNODE *temp,*cur, *prev; \n \ntemp= (NODE *)malloc(sizeof(NODE)); \n \ntemp->item=ele; temp->next=NULL; temp->prev=NULL; \n \nif(head->next==NULL) \n \n{ head->next=temp; last=temp;return; \n \n} \n \nlast->next =temp; temp->prev=last; \n \nlast=temp; \n \nreturn ; \n} \n \nvoid insertfront(int ele) \n{ \nNODE *temp; \n \ntemp= (NODE *)malloc(sizeof(NODE)); \n \ntemp->item=ele; temp->next=NULL; temp->prev=NULL; \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n47 \n \nif (head->next== NULL) \n \n{ \n \n \nhead->next=temp; last=temp; \n \n} \n \nelse \n \n{ \ntemp->next=head->next; \n \n \nhead->next->prev=temp; \n \n \nhead->next=temp; \n \n} \n \nreturn ; \n} \n \n \nvoid deletefront() \n{ \n NODE *temp; int num; \n temp=head->next; \n if(head->next==NULL) \n \n { \n \nprintf(\" List is Empty \\n\");return ; \n \n } \n if(head->next==last) \n \n { \n \n \n \nhead->next=last=NULL; \n \n } \n \n else \n \n { \nhead->next=temp->next; \n \n \ntemp->prev=NULL; \n } \n printf(\" Element=%d\\n\", temp->item); \n \n free(temp); \n \n return ; \n} \n \nvoid deleterear() \n{ \n NODE *cur, *pre; \n cur=last; \n if(head->next==NULL) \n \n { \n \nprintf(\" List is Empty \\n\");return ; \n \n } \n \n printf(\" Element=%d\\n\", last->item); \n if(head->next==last) \n \n { \n \n \nhead->next=last=NULL; \n \n } \n \n else \n \n { \n \n \npre=last->prev; \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n48 \n \npre->next=NULL; last=pre; \n \n } \n free(cur); \n return; \n} \n \nvoid display() \n{ \nNODE *r; int count=0; \nr=head->next; \nif(r==NULL) \n{ \n \n \nreturn; \n \n} \nprintf(\"Doubly Linked List\\n\"); \nwhile(r!=NULL) \n{ \nprintf(\"%d-->\",r->item); \n \n \nr=r->next; count++; \n \n} \nprintf(\"\\n No. Of Nodes=%d\\n\",count); \n} \n \nvoid insertOrdered(int ele) \n{ \n \nNODE *nn, *pre, *cur; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \nnn->item=ele; nn->next=NULL; nn->prev=NULL; \n \nif(head->next==NULL) \n \n{ \n \n \nhead->next=last=nn; return; \n \n} \n \nif(head->next->item >ele) \n \n{ \n \n \nnn->next=head->next; head->next->prev=nn; \n \n \nhead->next=nn; return; \n \n} \n \npre=NULL; cur=head->next; \n \nwhile(cur->item <=ele && cur->next !=NULL) \n \n{ pre=cur; \n \n \ncur=cur->next; \n \n} \n \nif(cur->item >ele) \n \n{ \npre->next=nn; nn->prev=pre; \n \n \nnn->next=cur; cur->prev=nn; \n \n} \n \nelse \n \n{ \n \n \ncur->next=nn; nn->prev=cur; last=nn; \n \n} \n} \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n49 \n \nvoid deleteOrdered(int ele) \n{ \n \nNODE *pre, *cur; \n \nif(head->next==NULL) \n \n{ \n \n \nprintf(\"Empty list\\n\"); return; \n \n} \n \ncur=head->next; \n \nif(cur->item ==ele) \n \n{ \n \n \nhead->next=cur->next; \nif(head->next!=NULL) \n head->next->prev=NULL; \n \n \nprintf(\"Deleted Rear element=%d\\n\",cur->item); \n \n \nfree(cur); \n \n \nreturn ; \n \n} \n \npre=NULL; \ncur=head->next; \n \nwhile(cur->item !=ele && cur->next !=NULL) \n \n{ \npre=cur; \n \n \ncur=cur->next; \n \n} \n \nif(cur->item !=ele && cur->next ==NULL) \n \n{ \n \n \nprintf(\"Element not found\\n\"); return ; \n \n} \n \npre->next=cur->next; \n \nreturn ; \n} \n \nint main() \n{ int ele; int i, ch; \n head= (NODE *)malloc(sizeof(NODE)); \n head->item=0; head->next=NULL; head->prev=NULL; \n while(1) \n { \n \nprintf(\"\\nList Operations\\n\"); \n \nprintf(\"===============\\n\"); \n \nprintf(\"1.Insert at Front end\\n\"); \n \nprintf(\"2.Deletion from the Front end\\n\"); \n \nprintf(\"3.Insertion at the Rear end of DLL\\n\"); \n \nprintf(\"4.Deletion from the rear end End of DLL\\n\"); \n \nprintf(\"5.Display DLL\\n\"); \n \nprintf(\"6.Perform Insertion into Ordered DLL\\n\"); \n \nprintf(\"7.Deletion from the Ordered DLL\\n\"); \n \nprintf(\"8.Exit\\n\"); \n \nprintf(\"Enter your choice : \"); \n \nscanf(\"%d\",&i); \n \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n50 \n \nswitch(i) \n { \n case 1 \n: \nprintf(\"Enter Item=\"); scanf(\"%d\",&ele); \n \n \n \n \n \ninsertfront(ele); break; \n \n \ncase 2 \n: \ndeletefront(); break; \n \n \n \n \n \ncase 3 \n: \nprintf(\"Enter Item=\"); scanf(\"%d\",&ele); \n \n \n \n \n \ninsertrear(ele); break; \n \n \ncase 4 \n: \ndeleterear(); break; \n \n \n \n \n \n \ncase 5 \n: \nif(head->next==NULL) \n \n \n \nprintf(\"List is Empty\\n\"); \n \n \n \n else \n \n \n \n \ndisplay(); \n \n \n \n \nbreak; \n \n \n \n \n \n \ncase 6 \n: \nprintf(\"Enter Item=\"); scanf(\"%d\",&ele); \n \n \n \n \n \ninsertOrdered(ele); break; \n \n \ncase 7 \n: \nprintf(\"Enter Item to delete=\"); \n \n \n \n \n \nscanf(\"%d\",&ele); \n \n \n \n \n \ndeleteOrdered(ele); break; \n \n \n \n \ncase 8 \n: \nreturn 0; \n default \n: \nprintf(\"Invalid option\\n\"); \n \n} \n} \n \nreturn 0; \n} \n \n19 Write a program to create linked to represent polynomial \nequation and evaluate the equation with one variable(x7 + \n8x – 43). \n#include \n#include \n \nstruct node \n{ \n \nint coef, pwr; \n \nstruct node *next; \n}; \n \ntypedef struct node NODE; \n \nNODE* insertrear(NODE *first, int c, int p) \n{ \n \nNODE *temp, *cur; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n \nif(temp==NULL) \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n51 \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return NULL; \n \n} \n \ntemp->coef=c; temp->pwr=p; \n \ntemp->next=NULL; \n \nif(first==NULL) \n \n \nfirst=temp; \n \nelse \n \n \n{ \n \n \ncur=first; \n \n \nwhile(cur->next!=NULL) \n \n \n{ \n \n \n \n \ncur=cur->next; \n \n \n} \n \n \ncur->next=temp; \n \n} \n \nreturn first; \n} \n \nvoid display(NODE *first) \n{ NODE *cur; \n \nif(first==NULL) \n \n{ \n \n \nprintf(\" Empty List\\n\"); return ; \n \n} \n \ncur=first; \n \nwhile(cur!=NULL) \n \n{ \n \n \nprintf(\"%dx^%d + \", cur->coef, cur->pwr); \n \n \ncur=cur->next; \n \n} \n \nprintf(\"NULL\\n\\n\"); \n} \n \nvoid evaluate(NODE *third, int x) \n{ \n \nNODE *cur; \n \nint s=0; \n \ncur=third; \n \nif(third==NULL) \n \n{ \n \n \nprintf(\" empty list\\n\"); \n \n \nreturn; \n \n} \n \nwhile(cur!=NULL) \n \n{ \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n52 \n \n \ns=s+cur->coef*pow(x, cur->pwr); \n \n \ncur=cur->next; \n \n} \n \nprintf(\"Value=%d\\n\",s); \n \nreturn; \n} \n \nint main() \n{ int c, p, x,ln1,ln2,i; \n \nNODE *first=NULL; \n \nNODE *second=NULL; \n \nNODE *third=NULL; \n \nprintf(\" Enter no of nodes for first List=\"); \n \nscanf(\"%d\", &ln1); \n \nfor(i=0;i \n#include \n \nstruct node \n{ \n \nint coef, pwr; \n \nstruct node *next; \n}; \n \ntypedef struct node NODE; \n \nNODE* insertrear(NODE *first, int c, int p) \n{ \n \nNODE *temp, *cur; \n \ntemp=(NODE*)malloc(sizeof(NODE)); \n \nif(temp==NULL) \n \n{ \n \n \nprintf(\"insufficient memory\\n\"); return NULL; \n \n} \n \ntemp->coef=c; temp->pwr=p; \n \ntemp->next=NULL; \n \nif(first==NULL) \n \n \nfirst=temp; \n \nelse \n \n \n{ \n \n \ncur=first; \n \n \nwhile(cur->next!=NULL) \n \n \n{ \n \n \n \n \ncur=cur->next; \n \n \n} \n \n \ncur->next=temp; \n \n} \n \nreturn first; \n} \n \nvoid display(NODE *first) \n{ NODE *cur; \n \nif(first==NULL) \n \n{ \n \n \nprintf(\" Empty List\\n\"); return ; \n \n} \n \ncur=first; \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n54 \n \nwhile(cur!=NULL) \n \n{ \n \n \nprintf(\"%dx^%d + \", cur->coef, cur->pwr); \n \n \ncur=cur->next; \n \n} \n \nprintf(\"NULL\\n\\n\"); \n} \n \nNODE* polyadd(NODE *first, NODE *second, NODE *third) \n{ \n \nNODE *nn, *cur1, *cur2; \n \nint sum; \n \nnn=(NODE*)malloc(sizeof(NODE)); \n \ncur1=first; cur2=second; \n \nif(cur1==NULL && cur2==NULL) \n \n \nreturn NULL; \n \nif(cur1==NULL) return cur2; \n \nif(cur2==NULL) return cur1; \n \nwhile(cur1!=NULL && cur2!=NULL) \n \n{ \n \n \nif(cur1->pwr >cur2->pwr) \n \n \n{ \nthird=insertrear(third, cur1->coef, cur1->pwr); \n \n \n \ndisplay(third); \n \n \n \ncur1=cur1->next; \n \n \n} \n \n \nelse \n \n \nif(cur1->pwr==cur2->pwr) \n \n \n{ \nsum=cur1->coef+cur2->coef; \n \n \n \nthird=insertrear(third, sum, cur1->pwr); \n \n \n \ndisplay(third); \n \n \n \ncur1=cur1->next;cur2=cur2->next; \n \n \n} \n \n \nelse \n \n \n{ \nthird=insertrear(third, cur2->coef, cur2->pwr); \n \n \n \ndisplay(third); \n \n \n \ncur2=cur2->next; \n \n \n} \n \n} \n \nif(cur1==NULL) \n \n{ \n \n \nwhile(cur2!=NULL) \n \n \n{ \n \n \n \nthird=insertrear(third, cur2->coef, cur2->pwr); \n \n \n \ncur2=cur2->next; \n \n \n} \n \n \nModule III Notes/Programs on Linked List \nDr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology \n55 \n \n} \n \nelse \n \nif(cur2==NULL) \n \n{ \n \n \nwhile(cur1!=NULL) \n \n \n{ \n \n \n \nthird=insertrear(third, cur1->coef, cur1->pwr); \n \n \n \ncur1=cur1->next; \n \n \n} \n \n} \n \n \nreturn third; \n} \n \nint main() \n{ int c, p, x,ln1,ln2,i; \n \nNODE *first=NULL; \n \nNODE *second=NULL; \n \nNODE *third=NULL; \n \nprintf(\" Enter no of nodes for first List=\"); \n \nscanf(\"%d\", &ln1); \n \nfor(i=0;i