Você está na página 1de 107

Stack

A Stack is defined as linear list in which insertion and deletion is taking place at the same end or at only at one end. This end is called as top end. The end at which no operation is taking place is called as bottom end. The Stack is a linear data structure where element which is recently entered is deleted first, hence stack is also called as LIFO structure(Last In First Out).The fig. Shown below is an sample type stack. Basically stack is nothing but an array or a vector with a maximum capacity of size. Basic operation on Stack: The important operation performed on the stack is PUSH and POP. The push operation is called inserting an element onto the stack and pop operation is called as deleting top element of the stack. In a stack only top most element can be accessed but not any other element in the stack. The pointer used to perform insertion and deletion operation on a stack is TOP. The insertion and deletion onto the stack and from the stack is depend on value of TOP. The following is the example which explains how insertion and deletion operation can be done on the stack. Stack insertion top top top top Stack is Empty Top = 0 top=1 stack[top]=10 stack[top]=20 stack[top]=30 stack[top]=40 Stack is full

Stack deletion top top top top top=4 top=3 top=2 top=1 top=0

A Stack can be represented as a simple one dimensional vector and also using structure. 1. Using vector definition: Int stk[80] Here stk is a vector of reserved with 80 location to store integer data. Out of which user can define any number of location form 0 to 80 as size of stack. The elements in above stack can be accessed as simple as vector access. 2. Using structure definition: Struct stk{ Int Int st[80]; top; }; Typedef struct stk STACK; Here STACK represents a stack of type struct stk. Member of structure can be accessed as STACK.item[top]; Implementation of Push function:

PUSH() { int ele; printf("Enter the element to be PUSH \n"); scanf("%d",&ele); if(top = = n) { printf("Stack is FULL\n"); return; } else { top++; STACK[top]=ele; } }

Implementation of POP function:

POP() { int ele; if(top = = 0) { printf("Stack is Empty \n"); return(0); } else { ele = STACK[top]; top--; return(ele); } } *Write a program in C to simulate or to implement a stack using a static vector or one dimensional array of size n #include <stdio.h> #include <conio.h> int STACK[80]; int top=0; int n; main() { int choice=0,l; printf("Enter the size of Stack \n"); scanf("%d",&n); while(choice != 4) { printf("1. PUSH \n"); printf("2. POP \n"); printf("3 DISPLAY \n"); printf("4. EXIT \n"); printf("Enter the choice \n"); scanf("%d",&choice);

switch(choice) { case 1: PUSH(); printf(" STACK after PUSH \n"); DISPLAY(); break; case 2: l=POP(); printf("Deleted element from the stack is %d \n",l); printf(" STACK after POP \n"); DISPLAY(); break; case 3: printf("Content of the Stack \n"); DISPLAY(); break; case 4: exit(0); } } } PUSH() { int ele; printf("Enter the element to be PUSH \n"); scanf("%d",&ele); if(top = = n) { printf("Stack is FULL\n"); return; } else { top++; STACK[top]=ele; } } POP() { int ele; if(top = = 0) { printf("Stack is Empty \n"); return(0); } else { ele = STACK[top]; top--; return(ele); } }

DISPLAY( ) { int i; if(top = = 0) { printf("Stack is empty \n"); return; } else { printf("TOP "); for(i=top;i>=1;i--) printf("%d ",STACK[i]); printf("BOTTOM\n"); } }

* Write a program in C to simulate or to implement a stack using a structure in C #include <stdio.h> struct st{ int item[10]; int top; }; typedef struct st stk; stk STACK; int n; main() { int choice = 0,l; printf("Enter the size of the stack\n"); scanf("%d",&n); while(choice!=4) { printf("1. PUSH \n"); printf("2. POP \n"); printf("3. Dispa \n"); printf("4. Exit \n"); printf("Enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: PUSH(); printf("The stack after insertion \n"); DISPLAY(); break;

case 2: l=POP(); printf("The deleted element is %d\n", l); printf("The stack after the deletion\n"); DISPLAY(); break; case 3: printf("the content of the stack is \n"); DISPLAY(); break; case 4: exit(0); } } } PUSH() { int ele; printf("Enter the element\n"); scanf("%d",&ele); if(STACK.top == n) { printf("Stack is overflow\n"); return; } STACK.top++; STACK.item[STACK.top]=ele; } POP() { int ele; if(STACK.top == 0) { printf("Stack is underflow\n"); return(0); } ele=STACK.item[STACK.top]; STACK.top--; return(ele); } DISPLAY() { int i; if(STACK.top == 0) { printf("Stack is empty \n"); return; } printf("TOP \n");

for(i=STACK.top;i>=1;i--) printf("%d \n",STACK.item[i]); printf("BOTTOM\n"); }

Applications of Stack: Stacks are very much useful for solving various problems in computer science and engineering. Stack are also used store return address in microprocessors. The some of the different application of Stacks are Parenthesis matching Recursion Arithmetic Expression evaluation Arithmetic Expression conversion 1. Infix expression to postfix expression 2. Infix expression to Prefix expression

Parenthesis matching: The main objective of this problem is find wheather given infix expression is fully parenthesized or not. If it is not parenthesized properly then which parenthesis has not got matching parenthesis in the expression. Also we can which position of left parenthesis has not matched right parenthesis. In a given expression. **Write a C program to check Parenthesis match using a Stack. #include <stdio.h> #include <string.h> main() { char str[80],c; int l; printf("Enter the expression \n"); scanf("%s",str); printf("The given expression is %s\n",str); l=parmatch(str); }

parmatch(st) char st[]; { int i,j,len,top=0; char ch,stk[80]; len=strlen(st)-1; stk[top]=-1; for(i=0;i<=len;i++) { if(st[i] == '(') { top=top+1; stk[top]=i; } else { if(st[i] == ')') { j=stk[top]; top=top-1; if(j != -1) { printf("%d %d \n",j+1,i+1); } else { printf("No match for right parenthesis at: %d\n",i+1); } } } } while(top != 0) { j=stk[top]; printf("No match for left parenthesis at: %d\n",j+1); top=top-1; } }

Arithmetic Expression conversion: A arithmetic expression can be defined as combination of operator and an operand. A arithmetic expression can be classified in to 3 types Infix Expression Postfix Expression Prefix Expression Infix Expression: Infix expression can be defined as expression where operator present in between two operands. Ex: a+b a*b+c (a+b)*c Postfix Expression / Suffix Expression : Postfix expression is parenthesis free expression. It consist of operator preceded by operand. Ex: ab+ ab*c+ ab+c* Prefix Expression: Prefix expression is parenthesis free expression. It consist of operator followed by an operand. Ex: +ab +*abc Conversion of Infix Expression to Postfix Expression: The essential of converting expression from infix to postfix is solve the expression easily. The conversion also BODMAS rule of evaluation of infx expression. The important input for conversion is The infix expression must contain only single character operand or digit The operator must be valid arithmetic operator The expression may or may not contain parentheses The given infix expression is assumed to be valid.

The following is important method to be consider in converting an infix expression to postfix or suffix expression. 1. 2. 3. 4. 5. Intialise top of stack with a special symbol say # Scan the infix expression from left to right and get a character one by one Check the character is operand or not If the character is operand then copy the same to postfix vector directly If character is an operator the check for precedence value of the same with top character of stack content.

6. If precedence of the character is greater than precedence of top character of the stack then push that infix character to the stack. 7. If precedence of the character is lesser than precedence of top character of the stack then delete top character of the stack and copy to postfix vector and repeat the step 6 until precedence of infix character is greater than precedence of the top character of the stack. 8. Repeat the step 2 through step 7 until the null character reach in the infix expression. 9. Once if end of the character is reached then copy all the character of the stack and copied to postfix vector. 10. Copy null character to the end of the postfix vector. The following table gives precedence value of infix character and top character stack Symbol Infix character precedence + 1 * / 2 ( 3 # Top character of stack precedence 1 2 0 0

Consider a simple Infix expression to convert it into a postfix or suffix expression. The following table gives much information how the conversion is taking place. Step Infix Expn a+b*c 1 2 3 4 5 6 7 8 Scan character Stack content Post vector a + b * c End of Expn # # #+ #+ #+* #+* #+ # A A Ab Ab Abc abc* abc*+ Post[0]=a Post[1]=b Post[2]=c Post[3]=* Post[4]=+ Post[5]=\0

Input Expression a+b*c

Postfix Expression abc*+

Step Infix Expn Scan character a*b+c 1 2 3 4 5 6 7 8 a * b + + c End of Expn

Stack content # # #* #* # #+ #+ #

Post vector Post[0]=a Post[1]=b Post[2]=* Post[3]=c Post[4]=+ Post[5]=\0

A A Ab ab* ab* ab*c ab*c+

Input Expression a*b+c Step Infix Expn a*(b+c) 1 2 3 4 5 6 7 8 9

Postfix Expression ab*c+

Scan character Stack content Post vector a * b + c ) End of Expn # # #*( #*( #*(+ #*(+ #*( #* # A A Ab Ab Abc abc+ abc+ abc+* Post[0]=a Post[1]=b Post[2]=c Post[3]=+ Post[4]=* Post[5]=\0

Input Expression a*(b+c)

Postfix Expression abc+*

**Write a C function to convert valid Infix expression to Postfix expression CONVERT() { char ch,stk[80]; int top=0,i=0,j=0; int p1,p2; stk[top]='#'; while(infix[i] != '\0') { ch=infix[i]; if(isalnum(ch)) { postfix[j]=ch; j++; }

else { if(ch == '#') { while(top!= -1) { postfix[j]=stk[top]; j++; top--; } } else { if(ch == ')') { while(stk[top]!='(') { postfix[j]=stk[top]; j++; top--; } top--; } else { p1=INPR(ch); p2=STKPR(stk[top]); while(p1<=p2) { postfix[j]=stk[top]; j++; top--; p2=STKPR(stk[top]); } top++; stk[top]=ch; } } } i++; } postfix[j]='\0'; }

** Write a C program to convert valid Infix expression to postfix Expression #include <stdio.h> #include <string.h> char infix[80],postfix[80]; main() { int l; clrscr(); printf("enter the infix expression terminated by #\n"); scanf("%s",infix); printf("infix expression is %s\n",infix); CONVERT(); printf("The postfix expression is %s\n",postfix); } CONVERT() { char ch,stk[80]; int top=0,i=0,j=0; int p1,p2; stk[top]='#'; while(infix[i] != '\0') { ch=infix[i]; if(isalnum(ch)) { postfix[j]=ch; j++; } else { if(ch == '#') { while(top!= -1) { postfix[j]=stk[top]; j++; top--; } }

else { if(ch == ')') { while(stk[top]!='(') { postfix[j]=stk[top]; j++; top--; } top--; } else { p1=INPR(ch); p2=STKPR(stk[top]); while(p1<=p2) { postfix[j]=stk[top]; j++; top--; p2=STKPR(stk[top]); } top++; stk[top]=ch; } } } i++; } postfix[j]='\0'; } INPR(c) char c; { switch(c) { case '*': case '/': return(2); case '+': case '-': return(1); case '(':return(3); } }

STKPR(c) char c; { switch(c) { case '*': case '/': return(2); case '+': case '-': return(1); case '(': case '#': return(0); } } Conversion of Infix Expression to Prefix Expression: The essential of converting expression from infix to prefix is solve the expression easily. The conversion also BODMAS rule of evaluation of infix expression. The prefix expression is used by LISP programming language. The important input for conversion is The infix expression must contain only single character operand or digit The operator must be valid arithmetic operator The expression may or may not contain parentheses The given infix expression is assumed to be valid.

The following is important method to be consider in converting an infix expression to prefix expression Initialize top of stack with a special symbol say # Find length of the given infix expression. Scan the infix expression from right to left and get a character one by one Check the character is operand or not If the character is operand then copy the same to prefix vector directly If character is an operator the check for precedence value of the same with top character of stack content. 7. If precedence of the character is greater than precedence of top character of the stack then push that infix character to the stack. 8. If precedence of the character is lesser than precedence of top character of the stack then delete top character of the stack and copy to postfix vector and repeat the step 6 until precedence of infix character is greater than precedence of the top character of the stack. 9. Repeat the step 3 through step 8 until the null character reach in the infix expression. 1. 2. 3. 4. 5. 6.

10. Once if first character is reached then copy all the character of the stack and copied to prefix vector. The following table gives precedence value of infix character and top character stack precedence Symbol + * / ( # Infix character precedence 1 2 3 Top character of stack precedence 1 2 0 0

Consider a simple Infix expression to convert it into a prefix expression. The following table gives much information how the conversion is taking place. Infix Expn Scan character Stack content Pre vector Step a+b*c 1 2 3 4 5 6 7 8 c * b + + a # # #* #* # #+ #+ # c c bc *bc *bc a*bc +a*bc Pre[5]=\0 Pre[4]=c Pre[3]=b Pre[2]=* Pre[1]=a Pre[0]=+

Input Expression a+b*c Step Infix Expn a*b+c 1 2 3 4 5 6 7 8

Postfix Expression +a*bc

Scan character Stack content Pre vector c + b * a # # #+ #+ #+* #+* #+ # c c bc bc abc *abc +*abc Pre[5]=\0 Pre[4]=c Pre[3]=b Pre[2]=a Pre[1]=* Pre[0]=+

Input Expression a*b+c

Postfix Expression +*abc

Step Infix Expn a*(b+c) 1 2 3 4 5 6 7 8 8 9

Scan character Stack content Pre vector ) c + b ( * a End of Expn # #) #) #)+ #)+ #) # #* #* # Pre[7]=\0 Pre[6]=c Pre[5]=b Pre[4]=+ Pre[3]=a Pre[2]=* Pre[1]= Pre[0]=

c c bc +bc +bc +bc a+bc *a+bc

Input Expression a*(b+c)

Postfix Expression *a+bc

**Write a C function to convert valid Infix expression to Prefix Expression CONVERT() { char ch,stk[80]; int top=0,i=0,j=0; int p1,p2; stk[top]='#'; j=strlen(infix)-1; prefix[j+1]='\0'; for(i=strlen(infix)-1;i>=0;i--) { ch=infix[i]; if(isalnum(ch)) { prefix[--j]=ch; } else { if(ch == '#') { while(top!= -1) { prefix[--j]=stk[top--]; } while(j>0) prefix[--j] = ' '; }

else { if(ch == '(') { while(stk[top]!=')') { prefix[--j]=stk[top--]; } top--; } else { p1=INPR(ch); p2=STKPR(stk[top]); while(p1<=p2) { prefix[--j]=stk[top--]; p2=STKPR(stk[top]); } top++; stk[top]=ch; } } } } } ** Write a C program to convert valid Infix expression to prefix Expression #include <stdio.h> #include <string.h> char infix[80],prefix[80]; main() { int l; clrscr(); printf("enter the infix expression starts by #\n"); scanf("%s",infix); printf("infix expression is %s\n",infix); CONVERT(); printf("The prefix expression is %s\n",prefix); }

CONVERT() { char ch,stk[80]; int top=0,i=0,j=0; int p1,p2; stk[top]='#'; j=strlen(infix)-1; prefix[j+1]='\0'; for(i=strlen(infix)-1;i>=0;i--) { ch=infix[i]; if(isalnum(ch)) { prefix[--j]=ch; } else { if(ch == '#') { while(top!= -1) { prefix[--j]=stk[top--]; } while(j>0) prefix[--j] = ' '; } else { if(ch == '(') { while(stk[top]!=')') { prefix[--j]=stk[top--]; } top--; } else { p1=INPR(ch); p2=STKPR(stk[top]); while(p1<=p2) { prefix[--j]=stk[top--]; p2=STKPR(stk[top]); } top++; stk[top]=ch;

} } } } } INPR(c) char c; { switch(c) { case '*': case '/': return(2); case '+': case '-': return(1); case ')': return(3); } } STKPR(c) char c; { switch(c) { case '*': case '/': return(2); case '+': case '-': return(1); case ')': case '#': return(0); } }

Arithmetic Expression evaluation: The postfix expression or suffix expression can be evaluated by using stack as an application. The input for evaluating the postfix expression is 1. The postfix expression must consist of only single character as operand or single digit 2. The operator in the expression must be valid arithmetic operator

The procedure for evaluating the postfix expression is as follows 1. 2. 3. 4. Scan the Postfix expression left to right get character one by one if character is operand then push that character on to the stack if character is operator the delete top two element of the stack perform the required operation. 5. Push the partial result on to the stack 6. repeat the step 1 through step 5 until end of expression reach 7. if end of expression is reach then pop content of the stack i.e final result of postfix expression

Write C program to evaluate give Postfix expression

#include <stdio.h> #include <math.h> #include <string.h> int top= -1; char POSTFIX[80]; int STK[80]; main() { int i; int res,op1,op2; char ch; printf("Enter the postfix expression as digits \n"); scanf("%s",POSTFIX); printf("Postfix Expression is %s \n",POSTFIX); for(i=0;i < strlen(POSTFIX);i++) { ch=POSTFIX[i]; if(ISDIGIT(ch)) PUSH(ch - '0'); else { op2 = POP(); op1 = POP(); res = OP(ch,op1,op2); PUSH(res); } } res=POP(); printf("The result of the expression is %d\n",res); }

PUSH(item) int item; { top=top+1; STK[top]=item; } POP() { int item; item=STK[top]; top = top-1; return(item); } OP(c,x,y) char c; int x,y; { switch(c) { case '+': return(x+y); case '-': return(x-y); case '*': return(x*y); case '/': return(x/y); case '^': return(pow(x,y)); } } ISDIGIT(c) char c; { return( c >='0' && c <= '9'); }

Recursion: Recursion is a process of calling a function by itself and terminate due to terminal condition. Recursion process will use stack as a storage structure to store parameters which is passed during function calling. This storage is temporary and it will be return after function executes return statement. Many problems like FACTORIAL , GCD, FIBONACCI etc., use the recursion technique to solve. This approach is useful for both the definition of mathematical functions (recurrence relation) and for the definition of data structure. Recurrence relation to find a factorial of a number: 1 N! = N * (N-1)! N >0 if N <=0

Recursive function to factorial of a number: FACT(n) Int n; { int f; if(n <= 0) return(1); else f= n * FACT(n-1); }

Write a program to find a factorial of a number using the recursion

#include <stdio.h> #include <math.h> main() { int n,f; printf("Enter the value of n\n"); scanf("%d",&n); printf("Value of n is %d\n",n); f=FACT(n); printf("Factorial of %d is %d\n",n,f); }

FACT(m) int m; { int fa; if(m <= 0) return(1); else { fa=m*FACT(m-1); return(fa); } }

Recurrence relation to find a GCD of two number: M GCD(m,n) = GCD(n,m%n) otherwise Recursive function to GCD of two number: GCD(m,n) Int m,n; { int g; if(n = = 0) return(m); else GCD(n,m%n); } Write a program to find a gcd of two numbers using the recursion if n=0

#include <stdio.h> main() { int m,n,g; printf("Enter the two numbers\n"); scanf("%d %d",&m,&n); printf("The two numbers are \n"); printf("M=%d N=%d\n",m,n); g=GCD(m,n); printf("The GCD of %d and %d is %d\n",m,n,g);

GCD(x,y) int x,y; { int r; if(y==0) return(x); else { r=GCD(y,x%y); return(r); }

Recurrence relation to find a FIBONACCI SEQUENCE : 1 FIBO(n) = FIBO(n-1) + FIBO(n-2) if n = 0 or n=1 otherwise

Recursive function to find a Fibonacci sequence : FIBO(n) Int n; { int fib; if((n = = 0) || (n = = 1)) return(1); else fib=FIBO(n-1)+FIBO(n-2); return(fib); } Write a program to find a Fibonacci sequence using the recursion

#include <stdio.h> main() { int i,n; printf("Enter the value of n\n"); scanf("%d",&n); printf("Fibonacci series is \n"); for(i=0;i<n;i++) printf("%d ",FIBO(i)); printf("\n");

} FIBO(m) int m; { int f; if(m == 0) return(0); else { if(m==1) return(1); else f=FIBO(m-1)+FIBO(m-2); return(f); } }

Recurrence relation to find a Product of n natural numbers in a vector A[n] PROD(a,n) = A[n]*PROD(a,n-1) otherwise if n = 1

Recursive function to find a Product of n natural numbers in a vector : PROD(a,n) Int a[],n; { int p; if(n = = 1) return(a[n]); else { p = a[n] * PROD(a,n-1); return(p); } } Write a program to find a Product of n natural numbers in a vector using the recursion

#include <stdio.h> main() { int n,a[80],i; long int p; printf("Enter the size of the vector \n"); scanf("%d",&n); printf("Enter the element of the vector\n");

for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("The given vetor element is \n"); for(i=1;i<=n;i++) printf("%d\n",a[i]); p=PROD(a,n); printf("The product of the vector element is \n"); printf("%ld\n",p); } PROD(x,m) int x[],m; { int p; if(m==1) return(x[m]); else { p=x[m] * PROD(x,m-1); return(p); } }

Recurrence relation to find a Sum of n natural numbers in a vector A[n] SUM(a,n) = A[n]+PROD(a,n-1) otherwise if n = 1

Recursive function to find a Sum of n natural numbers in a vector : SUM(a,n) Int a[],n; { int s; if(n = = 1) return(a[n]); else { s = a[n] + PROD(a,n-1); return(s); } }

Write a program to find a Sum of n natural numbers in a vector using the recursion

#include <stdio.h> main() { int n,a[80],i; int s; printf("Enter the size of the vector \n"); scanf("%d",&n); printf("Enter the element of the vector\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("The given vetor element is \n"); for(i=1;i<=n;i++) printf("%d\n",a[i]); s=SUM(a,n); printf("The product of the vector element is \n"); printf("%d\n",s);

SUM(x,m) int x[],m; { int p; if(m==1) return(x[m]); else { p=x[m] + SUM(x,m-1); return(p); }

Recursive function to find a Largest of n natural numbers in a vector : LARGE(a,n); Int a[],n; { int y; if(n = = 1) return(a[1]); else { y = LARGE(a,n-1); if( a[n] >= y) return(a[n]; else return(y); } }

Write a program to find a Largest of n natural numbers in a vector using the recursion

#include <stdio.h> main() { int n,a[80],i; int l; printf("Enter the size of the vector \n"); scanf("%d",&n); printf("Enter the element of the vector\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("The given vetor element is \n"); for(i=1;i<=n;i++) printf("%d\n",a[i]); l=LAR(a,n); printf("The largest element of the vector element is \n"); printf("%d\n",l); } LAR(x,m) int x[],m; { int p; if(m==1) return(x[m]); else { p=LAR(x,m-1); if(x[m] >= p) return(x[m]); else return(p); }

Comparison between Iteration and Recursion Iteration 1. Compactness in program writing is difficult. 2. Required less storage space for parameters 3. Required less time for Execution 4. Divide and conquer technique Recursion Compactness in program writing is easy Required more storage space for parameters Required more time for Execution Top Down technique

Queue
A Queue is defined as linear list in which insertion is taking place at one end and deletion is taking place at the other end . The end where insertion is taking place is called as rear end. The end where deletion is taking place is called as front end. The Queue is a linear data structure where element which is inserted first is deleted first , hence queue is also called as FIFO structure(First In First Out).The fig. Shown below is an sample type queue.

Basically queue is nothing but an array or a vector with a maximum capacity of size. Basic operation on queue: The important operation performed on the queue is INSERTION and DELETION . The insertion operation is called inserting an element onto rear end of the queue and deletion operation is called as deleting front element of the queue.. The pointer used to perform insertion and deletion operation on a queue is REAR and FRONT respectively. The following is the example which explains how insertion and deletion opearation can be done on the queue. Queue fig 4 location A queue can be represented as a simple one dimensional vector and also using structure. 1. Using vector definition: Int que[80] Here que is a vector of reserved with 80 location to store integer data. Out of which user can define any number of location form 0 to 80 as size of stack. The elements in above queue can be accessed as simple as vector access. 2. Using structure definition: Struct que{ Int Int Int st[80]; front; rear;

}; Typedef struct que QUEUE; Here QUEUE represents a stack of type struct que. Member of structure can be accessed as QUEUE . item[rear] or QUEUE.item[front].

Implementation of Insertion function:

INSERT() { int ele; printf(Enter the element to be insert \n); scanf(%d,&ele); if(rear >= n) { printf(Queue is Over flow\n); return; } else { rear++; QUEUE[rear]=ele; if(front = = 0) front = 1; } Implementation of Deletion function:

DELETE() { int ele; if(front = = 0) { printf(Queue is Under flow \n); return(0); } else { ele = QUEUE[front]; if(front = = rear) { front=0; rear=0; } else front++; return(ele); } }

Write a program in C to simulate or to implement a queue using a static vector or one dimensional array of size n

#include <stdio.h> int LQ[80],front=0,rear=0; int n; main() { int choice=0,l; printf("enter the size of the linear Q\n"); scanf("%d",&n); while(choice!=4) { printf("1 INSERT \n"); printf("2 DELETE \n"); printf("3 DISPLAY \n"); printf("4 EXIT \n"); printf("Enter the choice\n"); scanf("%d",&choice); switch(choice) { case 1: INSERT(); printf("LQ after insertion \n"); DISPLAY(); break; case 2: l=DELETE(); printf("DELETED element is %d\n",l); printf("LQ after deletion\n"); DISPLAY(); break; case 3: printf("Content of the LQ\n"); DISPLAY(); break; case 4: exit(0); } } } INSERT() { int ele,t; printf("Enter the element to be insert\n"); scanf("%d",&ele); if(rear == n) { printf("Linear Q is Full \n"); return; } else { rear++; LQ[rear]=ele; } if(front==0) front=1; }

DELETE() { int p; if(front == 0) { printf("Linear Q is Empty \n"); return(0); } p=LQ[front]; if(front==rear) { front=0; rear=0; } else front++; } return(p);

DISPLAY() { int i; if(front==0) { printf("Linear Q is empty \n"); return; } else { for(i=front;i<=rear;i++) { printf("LQ[%d]=%d \n",i,LQ[i]); } } }

Write a program in C to simulate or to implement a queue using a structure in C

#include <stdio.h> struct q{ int item[10]; int front; int rear; }; typedef struct q que; que QUEUE; int n; main() { int choice = 0,l; printf("Enter the size of the queue\n");

scanf("%d",&n); while(choice!=4) { printf("1. INSERT \n"); printf("2. DELETE \n"); printf("3. DISPLAY \n"); printf("4. Exit \n"); printf("Enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: INSERT(); printf("The queue after insertion \n"); DISPLAY(); break; case 2: l=DELETE(); printf("The deleted element is %d\n",l); printf("The queue after the deletion\n"); DISPLAY(); break; case 3: printf("the content of the queue is \n"); DISPLAY(); break; case 4: exit(0); } } } INSERT() { int ele; printf("Enter the element\n"); scanf("%d",&ele); if(QUEUE.rear == n) { printf("Queue is overflow\n"); return; } QUEUE.rear++; QUEUE.item[QUEUE.rear]=ele; if(QUEUE.front == 0) QUEUE.front=1; } DELETE() { int ele; if(QUEUE.front == 0) { printf("Queue is underflow\n"); return(0); } ele=QUEUE.item[QUEUE.front]; if(QUEUE.front == QUEUE.rear) { QUEUE.front=0; QUEUE.rear=0; } else QUEUE.front++;

return(ele);

DISPLAY() { int i; if(QUEUE.front == 0) { printf("Queue is empty \n"); return; } printf("FRONT -> "); for(i=QUEUE.front;i<=QUEUE.rear;i++) printf("%d-> ",QUEUE.item[i]); printf("REAR\n"); }

CIRCULAR QUEUE: The major disadvantage of linear queue is the wastage of memory location . The above wastage of memory location can be over come by implementing the circular queue. The following example explains functions of circular queue . Consider a circular queue of size n=5 initially circular queue is empty with front and rear value is equal to 0. The following operation explains function of the circular queue. Implementation of Circular queue Insertion function: CQINSERT() { int ele; int temp;

printf(Enter the element to be insert \n); scanf(%d,&ele); temp=rear; if(rear = = n) rear=1; else rear++; if(front = = rear) { printf(Circular Queue is Over flow\n); rear=temp; } else { CQ[rear]=ele; if(front = = 0) front=1; } }

Implementation of Circular queue Delete function: CQDELETE() { int ele; if(front = = 0) { printf(The Circular Queue is Under flow\n); return(0); } ele=CQ[front]; if(front = = n) front = 1; else { if(front = = rear) { front = 0; rear = 0;

} else front++; } return(ele); }

Write a program in C to simulate or to implement a circular queue using a static vector or one dimensional array of size n

#include <stdio.h> int CQ[80],front=0,rear=0; int n; main() { int choice=0,l; printf("enter the size of the circular Q\n"); scanf("%d",&n); while(choice!=4) { printf("1 INSERT \n"); printf("2 DELETE \n"); printf("3 DISPLAY \n"); printf("4 EXIT \n"); printf("Enter the choice\n"); scanf("%d",&choice); switch(choice) { case 1: INSERT(); printf("CQ after insertion \n"); DISPLAY(); break; case 2: l=DELETE(); printf("DELETED element is %d\n",l); printf("CQ after deletion\n"); DISPLAY(); break; case 3: printf("Content of the CQ\n"); DISPLAY(); break; case 4: exit(0); } } } INSERT() { int ele,t; printf("Enter the element to be insert\n"); scanf("%d",&ele); t=rear; if(rear == n)

rear=1; else rear++ if(rear!=front) CQ[rear]=ele; else { printf("CQ is over flow\n"); rear=t; } if(front==0) front=1; } DELETE() { int p; if(front == 0) { printf("CQ is underflow\n"); return(0); } p=CQ[front]; if(front==n) front=1; else { if(front==rear) { front=0; rear=0; } else front++; } return(p); } DISPLAY() { int t; t=front; while(rear!=front) { printf("CQ[%d]=%d ",front,CQ[front]); if(front==n) front=1; else front++; } printf("CQ[%d]=%d ",front,CQ[front]); printf("\n"); front=t; }

Write a program in C to simulate or to implement a circular queue using a structure in C

PRIORITY QUEUE: An ordinary queue is works on the principle FIFO principle . In a priority queue insertion depends on the element priority. Elements are deleted either in increasing or decreasing order of priority rather than in the order in which they have arrived in the queue. A priority queue is a collection of elements each one having an assigned priority. Insertion and deletion are two basic operations to be done on this queue. The deletion operation is different from ordinary queue . The deleletion operation is classified in two types 1. Ascending priority queue (Min Priority Queue) (Elements with a minimum priority is to be deleted first) 2. Descending priority queue(Max Priority Queue) ( Elements with a maximum priority is to be deleted first)

Implementation of Priority queue Insertion function:

QINSERT(Q,p,x) queue Q[]; int p,x; { if(Q[p].rear == 4) { printf("Queue %d is overflow\n",p); return; } Q[p].rear++; Q[p].items[Q[p].rear] = x; if(Q[p].front == 0) Q[p].front=1; return(Q); } Implementation of Priority queue Deletion function:

QDELETE(Q) queue Q[]; {

int temp = 0; int empty , p,l; for(p=1;p<=n;p++) { if(Q[p].front == 0) { printf("the Q[%d] is empty\n",p); l=0; } else { temp=Q[p].items[Q[p].front]; l=1; if(Q[p].front == Q[p].rear) { Q[p].front=0; Q[p].rear=0; } else Q[p].front++; break; } } if(l==1) printf("Element %d is deleted from %d queue\n",temp,p); } Write a program in C to simulate or to implement a Priority queue using a structure in C

#include <stdio.h> #define n 3 struct que{ int items[5]; int front; int rear; }; typedef struct que queue; main() { queue q[n]; int ele,choice=0; int l,i; int pri; clrscr(); for(i=1;i<=n;i++) { q[i].front=0; q[i].rear=0; }

while(choice!=4) { printf("1. insert \n"); printf("2. delete \n"); printf("3. display \n"); printf("4. exit \n"); printf("enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the priority and element \n"); scanf("%d %d",&pri,&ele); QINSERT(q,pri,ele); printf("the content of the priority Q after insertion\n"); QDISPLAY(q); break; case 2: QDELETE(q); printf("the content of priority Q after deletion is \n"); QDISPLAY(q); break; case 3: printf("The content of the Priority Q is \n"); QDISPLAY(q); break; case 4: exit(0); } } } QINSERT(Q,p,x) queue Q[]; int p,x; { if(Q[p].rear == 4) { printf("Queue %d is overflow\n",p); return; } Q[p].rear++; Q[p].items[Q[p].rear] = x; if(Q[p].front == 0) Q[p].front=1; return(Q); } QDELETE(Q) queue Q[]; { int temp = 0; int empty , p,l; for(p=1;p<=n;p++) { if(Q[p].front == 0) { printf("the Q[%d] is empty\n",p); l=0; } else { temp=Q[p].items[Q[p].front]; l=1;

if(Q[p].front == Q[p].rear) { Q[p].front=0; Q[p].rear=0; } else Q[p].front++; break; } } if(l==1) printf("Element %d is deleted from %d queue\n",temp,p); } QDISPLAY(Q) queue Q[]; { int i,p; for(p=1;p<=n;p++) { printf("Queue : %d\n",p); if(Q[p].front == 0) printf("Empty\n"); else { for(i=Q[p].front;i<=Q[p].rear;i++) printf("%d ",Q[p].items[i]); printf("\n"); } } printf("\n \n"); }

LINKED LIST Linked List is a data structure which is collection of zero or more nodes or locations where each node has some information. Between each node in the list there exists a logical relationship so that given the address of first node, any node in that list can be obtained. The above linked list is completely different from linear data structure with respect to implementation and also in representation of the same. Advantage of Arrays (Static allocation): 1. Data access is fast. : Using the array , we can access any data item efficiently just specifying the index of location. 2. It is simple to under stand and implement.: Arrays are simple to understand and use Disadvantage of Arrays (Static allocation):

1. The size of the array is fixed : In static memory allocation a fixed amount of memory is allocated before start of execution. The memory requirement for most of the application cannot be predicted while writing the program. 2. Array items are stored contiguously: Some times enough memory location may not be available. There are situations where a number of junk memory locations are available. 3. Insertion and Deletion is difficult: To insert an element in the nth location we have to move nth location element already present to (n+1)th location . Similarly for deletion also.

Advantage of Linked List (Dynamic allocation): 1. Size of the list is not fixed: The linked list size can grow and shrink depending on data. 2. Data can be stored in non contiguous block of memory : Data can be stored in any location connected through logically 3. Insertion and Deletion is easy: Insertion and deletion can be easily done. Disadvantage of Linked list (Dynamic allocation): 1. Requires extra space to store data: The each node has a special field called link or next field to hold address of next node. 2.Data access is slow: The list has to traverse from the first node to the end of the list or the required position is found because we know only address so the first node

Representation of Linked List in the memory: Consider a Linked List , the first node is represented by pointer variable START. The following figure will gives the complete picture of the list. 10
415h

20

350h

30

556h

40 556h

START= 1010h 415h 350h The above list will be stored in the memory as follows 20 3150h 0 0415h 40 NULL

0418h

10 0415 h

1010h 3150h

A node is nothing but a memory location with an address express in hexadecimal format. A typical node structure is as follows Info part Address part

Info part: Used store value or the information may be any type. Info part may be one or more in a node. Address part: Used to store address of the next node or a next location. Address part is only one in a node. Representation of Node in C language : A node or location can be represented in C language using the structure as Self Referential Structure (SRS). A Self Referential Structure (SRS) can be defined as a structure with any one member of a structure represents its parent structure is called a Self Referential Structure (SRS). The same can be represented in C language as Struct node { int info; struct node *next; }; Different notations for accessing data in a node : The information present in a node can be accessed using the following notations in C language. START = 0506h Info next

1. START a pointer to the address of the node. 2. START - > Info used to access a information part of the node.

3. START -> next used to hold address of a next node in the list. 4. NULL a special address with value 0.

Dynamic Memory Allocation to a Node: The nodes in a list grow or shrink dynamically. Initially assume that there are four nodes in a list, if we want to connect some more nodes to a list by adding a node at run time. A node can created dynamically using the syntax of C language is NEW = (NODE *) malloc(sizeof(NODE)) Where NEW is pointer variable NODE is a Structure General syntax of allocating a node is Ptr. Variable = < data type as node *> malloc(sizeof(data type as node))

Consider a typical linked list with five nodes. A first node of the list is represented by pointer variable START. The information stored in the list is of type integer. The following notations gives representation and access of list. 10
415h

20

350h

30

556h

40 556h

START= 1010h

415h

350h

A linked list is classified in to two types 1. Linear Linked List. 2. Non Linear Linked List

Linked List Operations: The operations that can be performed on the linked list is

Creation Insertion Deletion Display Search

Linear Linked List: A Linear Linked list is one which is having a definite traversal path between each node. A linear linked list is classified in to 1. 2. 3. 4. Singly Linked List Circular Single Linked List Doubly Linked List Circular Doubly Linked List

Singly Linked List : A singly linked list is linear linked list where each node has designated field called info field used to store information or data and link or next field used to store address of the next node. If there exist a only one link towards one particular direction from first node to last node. Also traversing can be done only in one direction but not both direction is called singly linked list. The address part of last node is NULL, it indicates end of a list. The following figure explains Singly linked list.

Memory representation of singly linked list 0 0415h 0418h 1010h 3150h 20 40 10 30 3150 NULL 0415h 0418

Pictorial representation of singly linked list

Operations on Singly Linked List: The basic fundamental operation that can performed on the singly linked list are 1. 2. 3. 4. Creating a singly linked list Inserting a node into the singly linked list Deleting a node from the singly linked list Display the content of the list.

Creating a singly linked list This part explains how a nodes can be linked together to form a singly linked list. Creation function first create a node and copy the information to the node. Creation function adds a node one by one at a time to create a singly linked list. A singly linked list can be created by two method 1. By adding a node at the front of the existing list or as a first node. 2. By adding a node at end of the existing list or as a last node .

By adding a node at the front of the existing list or as a first node. 1. Adding of node at the front of the list if START is NULL a. Empty list START = NULL b. New node with info x A NEW node or location created with an address 0505h NEW = 0505h NEW->info = x NEW->next = NULL NEW

c. New node when START = = NULL x START

The following code explains how a new node can be added at the front of list or as a first node when list empty START=NEW

2. The following figure explains how a node can be added at the front to the existing list or as a first node START

a. Linked List NEW = 1010h NEW->info = x NEW->next = NULL x b. New node x NEW 1010h 10 START 0505h c. New node added at front Following code gives method of inserting a node at the front of existing list or as a first node of a list. NEW -> next = START START = NEW By adding a node at the End of the existing list or as a last node. 1. Adding of node as a last node of the list if START is NULL c. Empty list START = NULL d. New node with info x A NEW node or location created with an address 0505h NEW = 0505h NEW->info = x NEW->next = NULL NEW NEW 1010h

20 0406h

c. New node as first when START = NULL x START

The following code explains how a new node can be added at the front of list or as a first node when list empty START=NEW 2. The following figure explains how a node can be added as a last node to the existing list or as a last node 10 START 0505h a. Linked List NEW = 1010h NEW->info = x NEW->next = NULL x b. New node 10 START 0505h 20 0406h c. New node added at front Following code gives method of inserting a node as a last node of an existing list or as a last node of a list. 1. Initialize first node of the list START to TEMP TEMP=START 2. Visit or traverse all the nodes in the list until end of list is reach using a loop by keep tracking previous node in some other variable as while(TEMP != NULL) { PREV=TEMP; TEMP=TEMP -> next } 3. Connect the new node NEW as a last node TEMP=START NEW 1010h 0406h 0406h 20

x NEW 1010h

While(TEMP!=NULL) { PREV=TEMP; TEMP=TEMP->next; } PREV->next=NEW;

C function to create a singly linked list by connecting a node towards back end. CREATE() { NODE *TEMP,*NEW; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { TEMP->next=NEW; TEMP=NEW; } scanf("%d",&ele); } } C function to create a singly linked list by connecting a node towards front end. CREATE() { NODE *TEMP,*NEW; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele;

NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { NEW->next=START; START=NEW; } scanf("%d",&ele); } } Inserting a node into the singly linked list: This part explains how a insertion can be done to an existing list. There are different type of insertion to an existing list. Inserting a node as first node in an existing list. Inserting a node as last node in an existing list. Inserting a node in between list with a given position in existing list. Inserting a node infront of given information in an existing list Inserting a node at the back end of an given information in an existing list.

C function to insert a node as a first node in an existing list INSERT( ) { NODE *NEW, *TEMP; int ele; printf(Enter the element to be insert \n); scanf(%d,&ele); NEW=(NODE *)malloac(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START = = NULL) { START=NEW; TEMP=NEW; } else { NEW->next = START; START=NEW;

} } C function to insert a node as a Last node in an existing list INSERT( ) { NODE *NEW, *TEMP, *PREV; int ele; printf(Enter the element to be insert \n); scanf(%d,&ele); NEW=(NODE *)malloac(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START = = NULL) { START=NEW; TEMP=NEW; } else { TEMP=START; While(TEMP != NULL) { PREV=TEMP; TEMP=TEMP->next; } PREV->next = NEW; } } C function to insert a node in between the existing list with a given position INSERT() { NODE *TEMP,*PREV,*NEW; int ele,i,pos; printf("Enter the element and Position to be insert\n"); scanf("%d %d",&ele,&pos); NEW=(NODE *)malloc(sizeof(NODE *)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW;

} else { if(pos==1) { NEW->next=START; START=NEW; } else { i=1; TEMP=START; while((TEMP!=NULL) && (i<pos)) { PREV=TEMP; TEMP=TEMP->next; i++; } if(TEMP==NULL) { PREV->next=NEW; NEW->next=NULL; } else { PREV->next=NEW; NEW->next=TEMP; } } } } C function to insert a node infront of given information in an existing list INSERT() { NODE *TEMP,*PREV,*NEW; int ele,i,x; printf("Enter the element and info to be insert\n"); scanf("%d %d",&ele,&x); NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW;

} else { if(x==START->info) { NEW->next=START; START=NEW; TEMP=NEW; } else { TEMP=START; while((TEMP!=NULL) && (x!=TEMP->info)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { printf("Given information is not in the list \n"); } else { PREV->next=NEW; NEW->next=TEMP; } } } } C function to insert a node at the back end of an given information in an existing list INSERT() { NODE *TEMP,*PREV,*NEW; int ele,i,x; printf("Enter the element and info to be insert\n"); scanf("%d %d",&ele,&x); NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else

{ if(x==START->info) { NEW->next=START->next; START->next=NEW; } else { TEMP=START; while((TEMP!=NULL) && (x!=TEMP->info)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { printf("Given information is not in the list \n"); } else { NEW->next=TEMP->next; TEMP->next=NEW; } } } } Deleting a node from the singly linked list The objective of this function is to delete a node from the list. The deletion means moving pointer variable to next location address logically. In a list any node can be deleted as Deleting a first node in the list Deleting last node in the list Deleting node with given position in the list Deleting a node with a given information existing in the list

The following example explains a deletion process 10


415h

20

550h

30

345h

40

226h

50 226h

START=1010h 415h START=1010h START->next = 415h

550h

345h

To delete a first node the code can be return as

START=START->next Deleting last node in the list Traverse the list by initializing the first node to Pointer variable TEMP until list reached NULL by keeping track previous node with another pointer variable PREV as TEMP = START While(TEMP!=NULL) { PREV = TEMP TEMP=TEMP->next } C function to insert a node as a first node in an existing list DELETE( ) { NODE *TEMP; int ele; if(START = = NULL) { printf(List is empty \n); return(0); } else { ele = START->info; START=START->next; return(ele); } }

C function to delete a node as a Last node in an existing list DELETE( ) { NODE *TEMP, *PREV; int ele; if(START = = NULL) { printf(List is empty \n); return(0);

} else { TEMP=START; While(TEMP->next !=NULL) { PREV=TEMP; TEMP=TEMP->next; } ele=TEMP->info; PREV->next=NULL; Return(ele); } } C function to delete a node in between the existing list with a given position DELETE() { NODE *TEMP,*PREV; int ele,i,pos; printf("Enter the Position to be delete\n"); scanf("%d",&pos); if(START==NULL) { printf("List is Empty\n"); return(0); } else { if(pos==1) { ele=START->info; START=START->next; return(ele); } else { i=1; TEMP=START; while((TEMP!=NULL) && (i!=pos)) { PREV=TEMP; TEMP=TEMP->next; i++; } if(TEMP==NULL) {

printf("Given position is not in the List\n"); return(0); } else { ele=TEMP->info; PREV->next=TEMP->next; return(ele); } } } } C function to delete a node of an given information in an existing list DELETE() { NODE *TEMP,*PREV; int ele,i,p; printf("Enter the information to be delete\n"); scanf("%d",&ele); if(START==NULL) { printf("List is Empty\n"); return(0); } else { if(ele==START->info) { p=START->info; START=START->next; return(p); } else { TEMP=START; while((TEMP!=NULL) && (ele!=TEMP->info)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { printf("Given information is not in the List\n"); return(0); } else

{ p=TEMP->info; PREV->next=TEMP->next; return(p); } } } } Display content of the singly linked list This function will display the content of the list. This will uses while loop structure for display . C function to display Content of the singly linked list DISPLAY() { NODE *TEMP; if(START==NULL) { printf("List is Empty\n"); return; } else { TEMP=START; while(TEMP!=NULL) { printf("%d -> ",TEMP->info); TEMP=TEMP->next; } printf("NULL \n\n"); } }

Write a C Program to create a singly linked list using dynamic pointer variable and perform the following operation 1. Insert a node to list in given position. 2. Delete a node from list with given position 3. Display content of list And display suitable messages. #include <stdio.h> #include <malloc.h> struct node { int info; struct node *next; }; typedef struct node NODE; NODE *START=NULL; main() { int choice=0,l; clrscr(); while(choice!=5) { printf("1. CREATE\n"); printf("2. INSERT\n"); printf("3. DELETE\n"); printf("4. DISPLAY\n"); printf("5. EXIT\n"); printf("enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("List after ceation\n"); DISPLAY(); break; case 2: INSERT(); printf("List after insertion\n"); DISPLAY(); break; case 3: l=DELETE(); printf("Deleted element is %d\n",l); printf("List after deletion\n"); DISPLAY(); break; case 4: printf("Content of the List \n"); DISPLAY();

break; case 5: exit(0); } } } CREATE() { NODE *TEMP,*NEW; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { TEMP->next=NEW; TEMP=NEW; } scanf("%d",&ele); } } INSERT() { NODE *TEMP,*PREV,*NEW; int ele,i,pos; printf("Enter the element and Position to be insert\n"); scanf("%d %d",&ele,&pos); NEW=(NODE *)malloc(sizeof(NODE *)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { if(pos==1) {

NEW->next=START; START=NEW; } else { i=1; TEMP=START; while((TEMP!=NULL) && (i != pos)) { PREV=TEMP; TEMP=TEMP->next; i++; } if(TEMP==NULL) { PREV->next=NEW; NEW->next=NULL; } else { PREV->next=NEW; NEW->next=TEMP; } } } } DELETE() { NODE *TEMP,*PREV; int ele,i,pos; printf("Enter the Position to be delete\n"); scanf("%d",&pos); if(START==NULL) { printf("List is Empty\n"); return(0); } else { if(pos==1) { ele=START->info; START=START->next; return(ele); } else {

i=1; TEMP=START; while((TEMP!=NULL) && (i!=pos)) { PREV=TEMP; TEMP=TEMP->next; i++; } if(TEMP==NULL) { printf("Given position is not in the List\n"); return(0); } else { ele=TEMP->info; PREV->next=TEMP->next; return(ele); } } } } DISPLAY() { NODE *TEMP; if(START==NULL) { printf("List is Empty\n"); return; } else { TEMP=START; while(TEMP!=NULL) { printf("%d -> ",TEMP->info); TEMP=TEMP->next; } printf("NULL \n\n"); } }

Write a C Program to create a singly linked list using dynamic pointer variable and perform the following operation 1. Insert a node to list in front of the given information in the list. 2. Delete a node from list with given information 3. Display content of list And display suitable messages #include <stdio.h> #include <malloc.h> struct node { int info; struct node *next; }; typedef struct node NODE; NODE *START=NULL; main() { int choice=0,l; clrscr(); while(choice!=5) { printf("1. CREATE\n "); printf("2. INSERT\n "); printf("3. DELETE\n "); printf("4. DISPLAY\n "); printf("5. EXIT\n "); printf("enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("List after ceation\n"); DISPLAY(); break; case 2: INSERT(); printf("List after insertion\n"); DISPLAY(); break; case 3: l=DELETE(); printf("Deleted element is %d\n",l); printf("List after deletion\n"); DISPLAY(); break;

case 4: printf("Content of the List \n"); DISPLAY(); break; case 5: exit(0); } } } CREATE() { NODE *TEMP,*NEW; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { TEMP->next=NEW; TEMP=NEW; } scanf("%d",&ele); } } INSERT() { NODE *TEMP,*PREV,*NEW; int ele,i,x; printf("Enter the element and info to be insert\n"); scanf("%d %d",&ele,&x); NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; }

else { if(x = = START->info) { NEW->next=START; START=NEW; TEMP=NEW; } else { TEMP=START; while((TEMP!=NULL) && (x!=TEMP->info)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { printf("Given information is not in the list \n"); } else { PREV->next=NEW; NEW->next=TEMP; } } } } DELETE() { NODE *TEMP,*PREV; int ele,i,p; 0 printf("Enter the information to be delete\n"); scanf("%d",&ele); if(START==NULL) { printf("List is Empty\n"); return(0); } else { if(ele==START->info) { p=START->info; START=START->next; return(p); } else

{ TEMP=START; while((TEMP!=NULL) && (ele!=TEMP->info)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { printf("Given information is not in the List\n"); return(0); } else { p=TEMP->info; PREV->next=TEMP->next; return(p); } } } } DISPLAY() { NODE *TEMP; if(START==NULL) { printf("List is Empty\n"); return; } else { TEMP=START; while(TEMP!=NULL) { printf("%d -> ",TEMP->info); TEMP=TEMP->next; } printf("NULL \n\n"); } }

Write a C Program to create a singly linked list using dynamic pointer variable and perform the following operation 1. Insert a node to list at back of the given information in the list. 2. Delete a node from list with given information 3. Display content of list And display suitable messages
#include <stdio.h> #include <malloc.h> struct node { int info; struct node *next; }; typedef struct node NODE; NODE *START=NULL; main() { int choice=0,l; clrscr(); while(choice!=5) { printf("1. CREATE\n"); printf("2. INSERT\n"); printf("3. DELETE\n"); printf("4. DISPLAY\n"); printf("5. EXIT\n"); printf("enter the choice\n"); scanf("%d",&choice); switch(choice) { case 1:CREATE(); printf("List after ceation\n"); DISPLAY(); break; case 2:INSERT(); printf("List after insertion\n"); DISPLAY(); break; case 3:l=DELETE(); printf("Deleted element is %d\n",l); printf("List after deletion\n"); DISPLAY(); break; case 4:printf("Content of the List \n"); DISPLAY(); break; case 5:exit(0); } }

CREATE() { NODE *TEMP,*NEW; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { TEMP->next=NEW; TEMP=NEW; } } } INSERT() { NODE *TEMP,*PREV,*NEW; int ele,i,x; printf("Enter the element and info to be insert\n"); scanf("%d %d",&ele,&x); NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { if(x==START->info) { NEW->next=START->next; START->next=NEW; } else { TEMP=START; while((TEMP!=NULL) && (x!=TEMP->info)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { printf("Given information is not in the list \n"); } scanf("%d",&ele);

} } }

else { NEW->next=TEMP->next; TEMP->next=NEW; }

DELETE() { NODE *TEMP,*PREV; int ele,i,p; printf("Enter the information to be delete\n"); scanf("%d",&ele); if(START==NULL) { printf("List is Empty\n"); return(0); } else { if(ele==START->info) { p=START->info; START=START->next; return(p); } else { TEMP=START; while((TEMP!=NULL) && (ele!=TEMP->info)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { printf("Given information is not in the List\n"); return(0); } else { p=TEMP->info; PREV->next=TEMP->next; return(p); } } } }

DISPLAY() { NODE *TEMP; if(START==NULL) { printf("List is Empty\n"); return; } else { TEMP=START; while(TEMP!=NULL) { printf("%d -> ",TEMP->info); TEMP=TEMP->next; } printf("NULL \n\n"); } }

Write a C program to count no number of nodes in Singly linked List.

#include <stdio.h> #include <malloc.h> struct node { int info; struct node *next; }; typedef struct node NODE; NODE *FIRST=NULL; main() { int cnt; clrscr(); printf("Create the list \n"); FIRST=CREATE(); printf("The content of the list \n"); DISPLAY(FIRST); cnt=COUNT(FIRST); printf("The number of nodes in the list is %d\n",cnt);

CREATE() { NODE *TEMP,*NEW; NODE *START=NULL; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL;

} return(START);

if(START==NULL) { START=NEW; TEMP=NEW; } else { TEMP->next=NEW; TEMP=NEW; } scanf("%d",&ele);

DISPLAY(TEMP) NODE *TEMP; { if(TEMP==NULL) { printf("List is Empty\n"); return; } else { while(TEMP!=NULL) { printf("%d -> ",TEMP->info); TEMP=TEMP->next; } printf("NULL \n\n"); } } COUNT(P) NODE *P; { NODE *TEMP; int l=0; if(P==NULL) { printf("List is empty \n"); return(l); } else { while(P!=NULL) { l++; P=P->next; } return(l); } }

Write a C program to Reverse the direction of the singly linked list

#include <stdio.h> #include <malloc.h> struct node { int info; struct node *next; }; typedef struct node NODE; NODE *START=NULL; main() { int choice=0,l; clrscr(); while(choice!=4) { printf("1. CREATE \n"); printf("2. REVERSE \n"); printf("3. DISPLAY \n"); printf("4. EXIT \n"); printf("enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("List after ceation\n"); DISPLAY(); break; case 2: REVERSE(); printf("List after reverse direction \n"); DISPLAY(); break; case 3: printf("Content of the List \n"); DISPLAY(); break; case 4: exit(0); } }

CREATE() { NODE *TEMP,*NEW; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW;

TEMP=NEW; } else { TEMP->next=NEW; TEMP=NEW; } scanf("%d",&ele); } }

REVERSE() { NODE *TEMP,*PREV,*CUR; if(START==NULL) { printf("SLL is empty \n"); return; } else { CUR=NULL; TEMP=START; while(TEMP!=NULL) { PREV=CUR; CUR=TEMP; TEMP=TEMP->next; CUR->next=PREV; } START=CUR; } } DISPLAY() { NODE *TEMP; if(START==NULL) { printf("List is Empty\n"); return; } else { TEMP=START; while(TEMP!=NULL) { printf("%d -> ",TEMP->info); TEMP=TEMP->next; } printf("NULL \n\n"); } }

Write C program to concatenate the two given singly linked list and store a result in the third list.

#include <stdio.h> #include <malloc.h> struct node { int info; struct node *next; }; typedef struct node NODE; NODE *FIRST=NULL; NODE *SECOND=NULL; NODE *THIRD=NULL; main() { int choice=0,l; clrscr(); printf("Create the first list \n"); FIRST=CREATE(); printf("The content of the first list \n"); DISPLAY(FIRST); printf("Create the second list \n"); SECOND=CREATE(); printf("The content of the second list \n"); DISPLAY(SECOND); printf("Concatenatation is in progress \n"); THIRD=CONCAT(FIRST,SECOND); printf("The resultant list is \n"); DISPLAY(THIRD);

CREATE() { NODE *TEMP,*NEW; NODE *START=NULL; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { TEMP->next=NEW; TEMP=NEW; } scanf("%d",&ele); }

return(START);

DISPLAY(TEMP) NODE *TEMP; { if(TEMP==NULL) { printf("List is Empty\n"); return; } else { while(TEMP!=NULL) { printf("%d -> ",TEMP->info); TEMP=TEMP->next; } printf("NULL \n\n"); } } CONCAT(P,Q) NODE *P,*Q; { NODE *TEMP,*R; if(P==NULL) { R=Q; return(R); } else { if(Q==NULL) { R=P; return(R); } else { R=P; TEMP=NULL; while(P!=NULL) { TEMP=P; P=P->next; } TEMP->next=Q; return(R); } } }

Write a C program to create a ordered singly linked list using dynamic variable and pointers and perform the following operation 1. Insertion Insert a node in a proper place in ordered list. 2. Delete Delete a node at given position in an existing list. 3. Display content of the ordered linked list.

#include <stdio.h> #include <malloc.h> struct node { int info; struct node *next; }; typedef struct node NODE; NODE *START=NULL; main() { int choice=0,l; clrscr(); while(choice!=5) { printf("1. CREATE\n"); printf("2. INSERT\n"); printf("3. DELETE\n"); printf("4. DISPLAY\n"); printf("5. EXIT\n"); printf("enter the choice\n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("List after ceation\n"); DISPLAY(); break; case 2: INSERT(); printf("List after insertion\n"); DISPLAY(); break; case 3: l=DELETE(); printf("Deleted element is %d\n",l); printf("List after deletion\n"); DISPLAY(); break; case 4: printf("Content of the List \n"); DISPLAY(); break; case 5: exit(0); } }

CREATE() { NODE *TEMP,*NEW,*PREV; int ele;

printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { if(ele < START->info) { NEW->next=START; START=NEW; TEMP=NEW; } else { TEMP=START; while((TEMP!=NULL) && (ele > TEMP->info)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { PREV->next=NEW; NEW->next=TEMP; } else { PREV->next=NEW; NEW->next=TEMP; } } } scanf("%d",&ele); } } INSERT() { NODE *TEMP,*PREV,*NEW; int ele,i; printf("Enter the element to be insert\n"); scanf("%d",&ele); NEW=(NODE *)malloc(sizeof(NODE )); NEW->info=ele; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else

{ if(ele < START->info) { NEW->next=START; START=NEW; TEMP=NEW; } else { TEMP=START; while((TEMP!=NULL) && (ele > TEMP->info)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { PREV->next=NEW; NEW->next=TEMP; } else { PREV->next=NEW; NEW->next=TEMP; } } } } DELETE() { NODE *TEMP,*PREV; int ele,i,p; printf("Enter the Information to be delete\n"); scanf("%d",&ele); if(START==NULL) { printf("List is Empty\n"); return(0); } else { if(ele==START->info) { p=START->info; START=START->next; return(p); } else { TEMP=START; while((TEMP!=NULL) && (ele!=TEMP->info)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { printf("Given position is not in the List\n"); return(0);

} else { p=TEMP->info; PREV->next=TEMP->next; return(p); } } } }

DISPLAY() { NODE *TEMP; if(START==NULL) { printf("List is Empty\n"); return; } else { TEMP=START; while(TEMP!=NULL) { printf("%d -> ",TEMP->info); TEMP=TEMP->next; } printf("\n\n"); } }

Write a C program to represent a polynomial using singly linked list and display the same

#include <stdio.h> #include <malloc.h> struct node { int coeff; int px; struct node *next; }; typedef struct node NODE; NODE *START=NULL; main() { int choice=0,l; clrscr(); while(choice!=3) { printf("1. CREATE Polynomial\n"); printf("2. DISPLAY\n"); printf("3. EXIT\n");

} }

printf("enter the choice\n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("Polynomial after ceation\n"); DISPLAY(); break; case 2: printf("Content of the List \n"); DISPLAY(); break; case 5: exit(0); }

CREATE() { NODE *TEMP,*NEW,*PREV; int ele,x; printf("Enter the coefficient terminated by 0 and power of x\n"); scanf("%d %d",&ele,&x); while(ele != 0) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->coeff=ele; NEW->px=x; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { if(x > START->px) { NEW->next=START; START=NEW; TEMP=NEW; } else { TEMP=START; while((TEMP!=NULL) && (x < TEMP->px)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { PREV->next=NEW; NEW->next=TEMP; } else { PREV->next=NEW; NEW->next=TEMP; } }

} scanf("%d %d",&ele,&x); } } DISPLAY() { NODE *TEMP; if(START==NULL) { printf("Empty Polynomial \n"); return; } else { TEMP=START; while(TEMP!=NULL) { printf("|%d| %d| -> ",TEMP->coeff,TEMP->px); TEMP=TEMP->next; } printf(" NULL \n\n"); } }

Write the C program to create a polynomial using singly linked list and evaluate the same.

#include <stdio.h> #include <malloc.h> struct node { int coeff; int px; struct node *next; }; typedef struct node NODE; NODE *START=NULL; main() { int choice=0,res; clrscr(); while(choice!=4) { printf("1. CREATE Polynomial printf("2. EVALUATE printf("3. DISPLAY printf("4. EXIT printf("enter the choice scanf("%d",&choice); switch(choice) {

\n"); \n"); \n"); \n"); \n");

case 1:

} } }

CREATE(); printf("Polynomial after ceation\n"); DISPLAY(); break; case 2: res=EVALUATE(); printf("SUM of polynomial is %d\n",res); break; case 3: printf("Content of the List \n"); DISPLAY(); break; case 4: exit(0);

CREATE() { NODE *TEMP,*NEW,*PREV; int ele,x; printf("Enter the coefficient terminated by 0\n"); scanf("%d %d",&ele,&x); while(ele != 0) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->coeff=ele; NEW->px=x; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { if(x > START->px) { NEW->next=START; START=NEW; TEMP=NEW; } else { TEMP=START; while((TEMP!=NULL) && (x < TEMP->px)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { PREV->next=NEW; NEW->next=TEMP; } else { PREV->next=NEW; NEW->next=TEMP; } } }

} }

scanf("%d %d",&ele,&x);

EVALUATE() { NODE *TEMP,*CUR; int r=0,x; printf("Enter the value of x\n"); scanf("%d",&x); TEMP=START; for(CUR=TEMP;CUR!=NULL;CUR=CUR->next) r=r+CUR->coeff * POWER(x,CUR->px); return(r); } POWER(p,q) int p,q; { int prod,i; prod=1; for(i=1;i<=q;i++) prod=prod*p; return(prod); }

DISPLAY() { NODE *TEMP; if(START==NULL) { printf("Empty Polynomial \n"); return; } else { TEMP=START; while(TEMP!=NULL) { printf("|%d| %d| -> ",TEMP->coeff,TEMP->px); TEMP=TEMP->next; } printf(" NULL \n\n"); } }

Write C program to create a singly linked list with dynamic variable and pointers to store following information in each node job_id (integer type) Job_name(char type), job_desc(char type).The opertations to supported are a. b. c. d. Insert Inssert a node at the front of the list Delete Delete a node with a given position in the list Search Search for given job_id in the list Display

#include <stdio.h> #include <malloc.h> struct node { int job_id; char job_name[80]; char job_desc[80]; struct node *next; }; typedef struct node NODE; NODE *START=NULL; main() { int choice=0,l,p; clrscr(); while(choice!=6) { printf("1. CREATE\n"); printf("2. INSERT\n"); printf("3. SEARCH\n"); printf("4. DELETE\n"); printf("5. DISPLAY\n"); printf("6. EXIT\n"); printf("enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("List after ceation\n"); DISPLAY(); break; case 2: INSERT(); printf("List after insertion\n"); DISPLAY(); break; case 3: p=SEARCH(); if(p==1) { printf("the given job_id is present in the list \n"); } else {

\n");

printf("the given job_id is not present in the list } printf("List after search is \n"); DISPLAY(); break; case 4: l=DELETE(); printf("Deleted node with job_id is %d\n",l); printf("List after deletion\n"); DISPLAY(); break; case 5: printf("Content of the List \n"); DISPLAY(); break; case 6: exit(0);

} } }

CREATE() { NODE *TEMP,*NEW; int id; char name[80],desc[80]; printf("Enter the jobid jobname jobdesc "); printf("terminated by jobid as -999\n"); scanf("%d %s %s",&id,name,desc); while(id != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->job_id=id; strcpy(NEW->job_name,name); strcpy(NEW->job_desc,desc); NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { TEMP->next=NEW; TEMP=NEW; } scanf("%d %s %s",&id,name,desc); } } INSERT() { NODE *TEMP,*PREV,*NEW; int id; char name[80],desc[80]; printf("Enter the jobid name desc to be insert \n"); scanf("%d %s %s",&id,name,desc); NEW=(NODE *)malloc(sizeof(NODE *)); NEW->job_id=id; strcpy(NEW->job_name,name); strcpy(NEW->job_desc,desc); NEW->next=NULL;

if(START==NULL) { START=NEW; TEMP=NEW; } else { NEW->next=START; START=NEW; } } DELETE() { NODE *TEMP,*PREV; int id,ele; printf("Enter the job_id to be delete\n"); scanf("%d",&id); if(START==NULL) { printf("List is Empty\n"); return(0); } else { if(id == START->job_id) { ele=START->job_id; START=START->next; return(ele); } else { TEMP=START; while((TEMP!=NULL) && (id!=TEMP->job_id)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) { printf("Given job_id is not in the List\n"); return(0); } else { ele=TEMP->job_id; PREV->next=TEMP->next; return(ele); } } } }

DISPLAY() { NODE *TEMP; if(START==NULL) { printf("List is Empty\n");

return; } else { TEMP=START; while(TEMP!=NULL) { printf("%d %s %s-> ",TEMP->job_id,TEMP->job_name,TEMP>job_desc); TEMP=TEMP->next; } printf("\n\n"); } } SEARCH() { NODE *TEMP,*PREV; int id; printf("Enter the job id to be search\n"); scanf("%d",&id); if(START == NULL) { printf("The list empty\n"); } else { if(id == START->job_id) { return(1); } else { TEMP=START; while((TEMP!=NULL) && (id!=TEMP->job_id)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) return(0); else return(1); } } }

Write C program to create a singly linked list with dynamic variable and pointers to store following information in each node stud_id (integer type) stud_name(char type), sem (integer type).The opertations to supported are a. Insertion operation 1. At the front of the a list 2. At the back of the list 3. At any position in the list b. Deleting a node based on on student id and display proper Messages

c. Search node for given stud_id and update information if any or Display proper messages d. Display content of the list.

#include <stdio.h> #include <malloc.h> struct node { int stud_id; char stud_name[80]; int sem; struct node *next; }; typedef struct node NODE; NODE *START=NULL; main() { int choice=0,l,p; clrscr(); while(choice!=5) { printf("1. CREATE\n"); printf("2. INSERT\n"); printf("3. SEARCH\n"); printf("4. DELETE\n"); printf("5. DISPLAY\n"); printf("6. EXIT\n"); printf("enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("List after ceation\n"); DISPLAY(); break; case 2: INSERT(); printf("List after insertion\n"); DISPLAY(); break; case 3: p=SEARCH(); if(p==1) { printf("the given job_id is present in the list \n"); } else { printf("the given job_id is not present in the list \n"); } printf("List after search is \n"); DISPLAY(); break; case 4: l=DELETE();

printf("Deleted node with job_id is %d\n",l); printf("List after deletion\n"); DISPLAY(); break; case 5: printf("Content of the List \n"); DISPLAY(); break; case 6: exit(0); } } CREATE() { NODE *TEMP,*NEW; int id,s; char name[80]; printf("Enter the studid studname sem"); printf(" terminated by studid as -999\n"); scanf("%d %s %d",&id,name,&s); while(id != -999) { NEW = (NODE *)malloc(sizeof(NODE)); NEW->stud_id=id; strcpy(NEW->stud_name,name); NEW->sem=s; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { TEMP->next=NEW; TEMP=NEW; } scanf("%d %s %d",&id,name,&s); } } INSERT() { NODE *TEMP,*PREV,*NEW; int id,s,pos,i; char name[80]; printf("Enter the studid name sem to be insert \n"); scanf("%d %s %d",&id,name,&s); printf("Enter the position to be insert \n"); scanf("%d",&pos); NEW=(NODE *)malloc(sizeof(NODE)); NEW->stud_id=id; strcpy(NEW->stud_name,name); NEW->sem=s; NEW->next=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } }

else { if(pos == 1) { NEW->next=START; START=NEW; } else { TEMP=START; i=1; while((TEMP!=NULL) && (i!=pos)) { PREV=TEMP; TEMP=TEMP->next; i++; } if(TEMP==NULL) { PREV->next=NEW; NEW->next=TEMP; } else { PREV->next=NEW; NEW->next=TEMP; } } } } DELETE() { NODE *TEMP,*PREV; int id,ele; printf("Enter the stud_id to be delete\n"); scanf("%d",&id); if(START==NULL) { printf("List is Empty\n"); return(0); } else { if(id == START->stud_id) { ele=START->stud_id; START=START->next; return(ele); } else { TEMP=START; while((TEMP!=NULL) && (id!=TEMP->stud_id)) { PREV=TEMP; TEMP=TEMP->next; } if(TEMP==NULL) {

printf("Given stud_id return(0); } else { ele=TEMP->stud_id; PREV->next=TEMP->next; return(ele); } } } }

is not in the List\n");

DISPLAY() { NODE *TEMP; if(START==NULL) { printf("List is Empty\n"); return; } else { TEMP=START; while(TEMP!=NULL) { printf("%d %s %d-> ",TEMP->stud_id,TEMP->stud_name,TEMP->sem); TEMP=TEMP->next; } printf("\n\n"); } } SEARCH() { NODE *TEMP,*PREV; int id,s; char name[80]; printf("Enter the stud id to be search\n"); scanf("%d",&id); printf("Enter name and sem to be replace\n"); scanf("%s %d",name,&s); if(START == NULL) { printf("The list empty\n"); } else { if(id == START->stud_id) { strcpy(START->stud_name,name); START->sem=s; return(1); } else { TEMP=START; while((TEMP!=NULL) && (id!=TEMP->stud_id)) { PREV=TEMP;

TEMP=TEMP->next; } if(TEMP==NULL) return(0); else { strcpy(TEMP->stud_name,name); TEMP->sem=s; return(1); } } } }

Comparison between Arrays and Linked List Arrays 1. Arrays are used to store elements In contiguous locations. 2. The random access is possible in arrays 3. Memory allocation is done at Compilation time 4. The insertion & deletion operation requires To shift the existing element present in the Array 5. Adjacent element information is not stored In current element location Linked List Linked list used store the elements In arbitrary locations. Random access is not possible in Linked list. Memory allocation & deallocation is done at execution time. The insertion & deletion operation doesnt requires to shift element. The adjacent element information is stored in current element Location.

Circular Linked List. A Circular linked list is one in which address part of the last node is holding address of the first node. The pictorial representation of circular linked list is shown below. Fig Comparison between SLL and CSLL SLL 1. In SLL we can traverse only if we known Address of first node. 2. Nodes which follow NODE X is reachable but Nodes that are Preceded by NODE X is not reachable 3. In SLL we have to hold address of Previous Node to know the address. CSLL In CSLL we can also traverse if we Know address of first node. Nodes which follow NODE X is reachable but also Nodes that are Preceded by NODE X is also reachable Not necessary to hold address of Previous Node to know the address.

In a Circular linked list any node can be considered as the first node and its predecessor is considered as the last node. The following two conventions can be used: 1. A pointer variable FIRST used represent the starting node of the list. This can be used to get address of the last node. 2. A Variable LAST can be used to represent the last node and node that followed by last is represented by FIRST which is the first node of the list. Operations on Singly Linked List: The basic fundamental operation that can performed on the circular singly linked list are 1. 2. 3. 4. Creating a Circular Singly Linked List Inserting a node into the Circular Singly Linked List Deleting a node from the Circular Singly Linked List Display the content of the Circular Singly Linked List

Creating a Circular singly linked list This part explains how a nodes can be linked together to form a Circular singly linked list. Creation function first create a node and copy the information to the node. Creation function adds a node one by one at a time to create a Circular singly linked list. A Circular singly linked list can be created by two method 1. By adding a node at the front of the existing list or as a first node. 2. By adding a node at end of the existing list or as a last node . By adding a node at the front of the existing list or as a first node Here Circular linked list is represented first node by variable START and last node by variable LAST. Consider a node which is created recently represented by the pointer variable NEW as follows. 10 NEW = 1010h Initially if there is no node in the existing in list the START is pointing to NULL then this NEW node is act as first node in the list pointed by variable START. 10
1010h

START =NEW = LAST =1010h Create a one more node represented by NEW 20
0505h

NEW=0505h Connect this NEW node to the existing node as follows. The following code will gives method of connecting the node in front of existing node or list. New->next = START LAST->next = NEW; START = NEW
20 1010h

10

0505h

START=0505h By adding a node at end of the existing list or as a last node

LAST=1010h

Here Circular linked list is represented first node by variable START and last node by variable LAST. Consider a node which is created recently represented by the pointer variable NEW as follows.
10 1010h

NEW= 1010h

Initially if there is no node in the existing in list the START and LAST is pointing to NULL then this NEW node is act as first node in the list pointed by variable START. 10
1010h

START=LAST=NEW=1010h

Create a one more node represented by NEW 20 0505h NEW =0505h Connect this NEW node to the existing node as follows. The following code will gives method of connecting the node at back of existing node or list. LAST->next = NEW; NEW->next=START LAST=NEW 10 START 1010h 20 LAST 0505h

Write C function to create Circular Singly linked List Inserting a node to a Circular singly linked list

This part explains how a insertion can be done to an existing circular singly linked list. There are different type of insertion to an existing circular singly linked list. Inserting a node as first node in an existing circular singly linked list. Inserting a node as last node in an existing circular singly linked list. Inserting a node in between list with a given position in existing circular singly linked list. Inserting a node infront of given information in an existing circular singly linked list Inserting a node at the back end of an given information in an existing circular singly linked list.

Write C function to insert a node in between the list with a given position in existing circular singly linked list.

Deleting a node from the Circular singly linked list The objective of this function is to delete a node from the list. The deletion means moving pointer variable to next location address logically. In a list any node can be deleted as Deleting a first node in the list Deleting last node in the list Deleting node with given position in the list Deleting a node with a given information existing in the list

Write C function to insert a node in between the list with a given position in existing circular singly linked list. Write a C program to Circular Singly linked list by using dynamic variable and pointer and perform following operation 1. Insert Insert a node with given position in the list 2. Delete a node with given position 3. Display a content of the list

Write C program to represent long integer using Circular singly linked list Write a C program to merge two ordered singly linked list . Store the result in the third list

Double Linked List In a singly linked list each node contains the address of the next node. If there is one more field which may contain address of the previous node. Then it is possible to traverse the list in both forward and backward directions. A list where both forward and backward direction is possible should have two link field and such list will be called as Double Linked List. The links are represented by Llink and Rlink used to hold address of previous node and next node respectively. The pictorial representation is as shown below. fig Comparison between SLL and DLL SLL DLL

1. In SLL traversing is possible in only one In DLL traversing is possible in direction. two direction 2. In SLL to delete a specific node address In DLL the address of previous node of previous nodes should be known. Can be obtain with Llink of current node. 3. The SLL needs only single storage field The DLL needs additional storage (two) fields. node of the list.

Operations on Double Linked List: The basic fundamental operation that can performed on the Double linked list are 1. 2. 3. 4. Creating a Double Linked List Inserting a node into the Double Linked List Deleting a node from the Double Linked List Display the content of the Double Linked List

Creating a Double linked list This part explains how a nodes can be linked together to form a Double linked list. Creation function first create a node and copy the information to the node. Creation function adds a node one by one at a time to create a Double linked list. A Double linked list can be created by two method 3. By adding a node at the front of the existing list or as a first node. 4. By adding a node at end of the existing list or as a last node . By adding a node at the front of the existing list or as a first node Here Double linked list is represented first node by variable START. Consider a node which is created recently represented by the pointer variable NEW as follows. 10 NEW = 1010h Initially if there is no node in the existing in list the START is pointing to NULL then this NEW node is act as first node in the list pointed by variable START. 10 START =NEW = LAST =1010h Create a one more node represented by NEW 20 NEW=0505h Connect this NEW node to the existing node as follows. The following code will gives method of connecting the node in front of existing node or list. New->Rlink = START START->Llink = NEW; START = NEW 20
1010h 0505h 10

START=0505h

LAST=1010h

By adding a node at end of the existing list or as a last node Here Double linked list is represented first node by variable START . Consider a node which is created recently represented by the pointer variable NEW as follows. 10 NEW = 1010h Initially if there is no node in the existing in list the START is pointing to NULL then this NEW node is act as first node in the list pointed by variable START. 10 START =NEW = LAST =1010h Create a one more node represented by NEW 20 NEW=0505h Connect this NEW node to the existing node as follows. The following code will gives method of connecting the node in front of existing node or list. New->Rlink = TEMP TEMP->Llink = NEW; TEMP = NEW 10
0505h 1010h 20

START=1010h

TEMP=0505h

Write C function to create Double linked List Inserting a node to a Double linked list

This part explains how a insertion can be done to an existing circular singly linked list. There are different type of insertion to an existing circular singly linked list. Inserting a node as first node in an existing circular singly linked list. Inserting a node as last node in an existing circular singly linked list. Inserting a node in between list with a given position in existing circular singly linked list.

Inserting a node infront of given information in an existing circular singly linked list Inserting a node at the back end of an given information in an existing circular singly linked list.

Write C function to insert a node in between the list with a given position in existing Double linked list.

Deleting a node from the Double linked list The objective of this function is to delete a node from the list. The deletion means moving pointer variable to next location address logically. In a list any node can be deleted as Deleting a first node in the list Deleting last node in the list Deleting node with given position in the list Deleting a node with a given information existing in the list

Write C function to insert a node in between the list with a given position in existing Double linked list. Write a C program to Double linked list by using dynamic variable and pointer and perform following operation 1.Insert Insert a node with given position in the list 2.Delete a node with given position 3.Display a content of the list

TREE
A Graph can be defined as a ordered pair of vertex and edges. Ex: G = (V,E) Where G is an graph of Vertex V and an edges E 1
3

4 3 4 3 4

Fig a

Fig b

Fig c fig a fig c

A graph in which every edge is directed is called a directed graph

A graph in which every edge is undirected is called a undirected graph

A graph is said to be mixed graph if some of the edges is directed and some of the edge is directed fig b A graph is said to be simple graph if their exist a maximum of one edge between pair of vertex The total number of edges leaving the node is called as out degree of a node. The total number of edges enter in to a node is called as in degree of a node. In a graph an edge is start with one edge and end with same edge is called as circuit or cyclic graph

A Tree can be defined as acyclic graph with only one node having in degree 0 which is called as ROOT and all other node is having indegree one.
1 2 4 5 7 3 6

Node 1 is called ROOT of Tree and all other nodes are children of a root Nodes 2 4 5 7 are called left subtree nodes 3 6 are called right subtree in a tree.

A node in a tree with an out degree is 0 is called terminal node or leaf node. All non leaves node are called as Internal node in a tree and all leaves node are called external node in a tree. 1

5 1 0 1 5 1 1 1 6

6 1 2 1 3

7 1 4

8 9

The Predecessor of a node X in a tree all the node starting from ROOT to the node X The Successor of a node X in a tree is all the node from node X to Leaf node in a tree The Predecessor of a node 10 is 1, 2, 5 The Successor of a node 5 is 10,11,15,16 The level of a node in a tree is nothing but number of edges in a path from root to that node The height of a tree is one more than maximum level in a tree. The level of a node 11 in a tree is 3 The height of a tree 5 The different types of tree are i. ii. iii. Binary tree Strictly Binary tree Complete Binary tree.

Binary Tree: In a tree out degree of each node is less than or equal to two. Each node in the tree can have 0,1,2 children. An empty tree is also a binary tree. Ex:
2

1
3

Strictly Binary Tree: If the out degree of every node in a tree is either 0 or 2 children.

Ex:
2

1
3

Complete Binary tree: A Strictly binary tree in which the number of nodes at any level is i. Is 2i-1

Ex:
2

1
3

Storage representation of TREE The TREE can be stored with an information using the Double Linked List structure. The typical representation of structure is as follows Struct node { Int info; Struct *llink; Struct *rlink; };

Operations On TREE The different operation that can be performed on a TREE are

1. Creation 2. Insertion 3. Traversal 4. Search Creation of tree A TREE can be created and represented by using the Double Linked List with a direction as control field. This direction will decide the where next child has to be insert (either to left or right of a node ). Write a C function to create TREE Insertion of node to a existing TREE

A node can be insert in to tree with given direction field as control flag which checks where the node can be inserted in an existing tree structure (i.e Either to left or right of a node). Write a C function to insert a Node in to a tree Traversal of a TREE The Traversal is one of most common operation that can be performed on trees. In traversal method each node in the tree visited only once. The different method of traversal of tree are 1. Inorder 2. Preorder 3. Postorder Each traversal is expressed in recursive forms. Inorder : In this method tree can be traversed as follows 1.Traverse the Left subtree in inorder tree 2.Process the ROOT of a tree 3.Traverse the Right subtree in Inorder Common definition says Left ROOT Right

Post order : In this method tree can be traversed as follows 1.Traverse the Left subtree in Post order tree 2.Traverse the Right subtree in Post order

3. Process the ROOT of a tree Common definition says Left Right ROOT

Pre order: In this method tree can be traversed as follows 1.Process the ROOT of a tree 2.Traverse the Left subtree in Pre order tree 3.Traverse the Right subtree in Pre order Common definition says ROOT Left Right

Você também pode gostar