affiliate marketing
Showing posts with label IT2205 – Data Structures and Algorithm Lab. Show all posts
Showing posts with label IT2205 – Data Structures and Algorithm Lab. Show all posts

Monday, 12 December 2011

BACKTRACKING ALGORITHM – KNAPSACK PROBLEM


BACKTRACKING ALGORITHM – KNAPSACK PROBLEM

Aim:
            To write a C program to solve the knapsack problem using backtracking algorithm
Algorithm:
            Step 1: Declare the variables, array size and functions
            Step 2: Get the value of number of objects and size of knapsack
Step 3: Enter weight and profit of objects
Step 4: Assign the initial values
Step 5: Call the necessary function and display the profit
Step 6: End of program

Coding:

DIJKSTRA’S ALGORITHM



DIJKSTRA’S ALGORITHM
Aim
            To implement Dijkstra’s algorithm to find the shortest path.

Algorithm

            Step1: [Include all the header files]
            Step2: Call allSelected( )
            Step3: Call Shortpath( )
            Step4: Access the functions from main
            Step5: End

Algorithm For ALLSELECTED( )

            Step1: Initialise i=0
            Step2: Check whether i<max
            Step3: Check whether Selected[i]=0
                        Return 0
            Step4: Else Return 1
            Step5: Return

Algorithm For SHORTPATH( )

            Step1: Initialise i=0 ,  Check i<max
                       Distance[i]=INFINITE
            Step2: Assign selected[current].distance[0]=0,
                        Current=0
            Step3: While(!allSelected(Selected))
                        Perform(Selected[i]= =0)
                        Current=k
                        Selected[current]=1
                        Print k
Coding:

HASHING TECHNIQUE


HASHING TECHNIQUE

Aim:
            To implement a program using Hashing technique.

Algorithm:

            Step1: Include necessary header files
            Step2: Declare necessary variables
            Step3: Check the value of  *S
                        Then call Insert( )
                            Print “Enter the string”
                        Read S
            Step4: Check the value of *S
            Step5: Then print S by calling hGetVal( )
            Step6: Call PrintHash( )
            Step7: End

Algorithm For hINSERT( ):

            Step1: Allocate memory to pointer
            Step2: Assign index            hGetIndex ( )
            Step3: Assign Ptr            Key         Strdup(key)
                                    Ptr         Val         Val
                                    Ptr         next         h[index]
                                    h[index]        Ptr
            Step4: Print “h[index]=key”
            Step5: Return

Algorithm For hGETVALUE( ):

Step1: [Ptr=h[hGetIndex(key)]]
Step2: If[Ptr && strcmp(Ptr        key)]
                        Then Ptr          Ptr        next
Step3: If[Ptr],Check the value of Ptr
                        [Return Ptr        Val]
Step4: [Return -1]

Algorithm For PRINTHASH( ):

PRIORITY QUEUE USING HEAP


PRIORITY QUEUE USING HEAP
Aim:
            To implement priority queue using Heap in C program.
Algorithm:
            Step 1: [Include necessary header files]
            Step 2: [Define maxsize as 15]
            Step 3: [Declare necessary variables]
            Step 4: READ  option, opt
                        IF opt is 1 THEN CALL INSERT()
                        IF opt is 2 THEN CALL DELMAX()
                        IF opt is 3 THEN CALL DIS()
            Step 5: [END OF MAIN FUNCTION]

Algorithm For INSERT()

EXPRESSION TREE


EXPRESSION TREE
Aim:
                                    To write a C program to demonstrate an expression tree.
Algorithm for Main ()
                                    Step 1: [ INCLUDE NECESSARY HEADER FILES]
                                    Step 2: [READ X]
                                    Step 3:[ CALL EXPTREE(),CALL DISPLAY(), CALL INORDER(),CALL
 PREORDER(),CALL EVALUATE ()]
Algorithm for EXPTREE()

            Step 1: Read Character
            Step 2: IF Character        operator then
                        CALL PUSH_OP()
            Step 3: [IF Character has only numbers]
                        IF [ is ALnum( str[i]           1 )] THEN
                        CREATE Newnode
            Step 4: Check for ‘ NULL ‘ condition
            Step 5: ASSIGN priority
            Step 6: IF ( Priority !=0) THEN CALL POP_OP()
            Step 7: IF Character = ‘)’ THEN CALL PUSH_OP()

Algorithm for INORDER (tree t)

BINARY SEARCH TREE


BINARY SEARCH TREE
Aim
To write a C program to implement a stack using binary search tree.
Algorithm
1.                  [Include all the necessary header files.]
2.                  [Declare the structure with all necessary variables.]
3.                  Read x;
4.                  Call INORDER().
5.                  Call PREORDER().
6.                  Call POSTORDER().
7.                  Call display().
8.                   
Algorithm For INSERT(P,X)

POLYNOMIAL MANUPULATION


POLYNOMIAL MANUPULATION
Aim
To implement polynomial manipulation using doubly linked lists.
Algorithm
POLYADD(POLY1: POLY2:POLY)
HEAD:POLY
Step 1: Assign HEAD+=NULL
Step2: While (POLY !=null)
Step3: HEAD=INSERTNODE(HEAD,COPYNODE,(POLY1,1))
Step4: POLY1=POLY1àNEXT
Step5: [End of Step2 while structure]
Step6: While(POLY2 1=NULL)
Step7: HEAD =INSERTNODE(HEAD,COPYNODE(POLY2,1))
Step8: POLY2=POLY2àNEXT
Step9: [End of Step 6 while Structure]
Step10: Return HEAD
END POLYADD()

Algorithm for polynomial subtraction

POLYSUB(POLY1:POLY, POLY2:POLY)
HEAD:POLY
Step1: Assign HEAD=NULL
Step2: While(POLY1!=NULL)
Step3: HEAD=INSERTNODE(HEAD,COPYNODE(POLY1,1))
Step4: POLY1=POLY1à NEXT
Step5: [End of Step2 while Structure]
Step6:While(POLY2!=NULL)
Step7: HEAD=INSERTNODE(HEAD,COPYNODE(POLY2,-1))
Step8: POLY2=POLY2àNEXT
Step9: [End of Step 6 While Structure]
Step10: Return HEAD
END POLYSUB()

Coding:

DOUBLY LINKED LIST – LINKED LIST IMPLEMENTATION


DOUBLY LINKED LIST – LINKED LIST IMPLEMENTATION

      Aim:
         To write a program to implement doubly linked list using linked list.
      Algorithm:
         Step 1: Declare header and pointer variables
         Step 2: Display the choices
         Step 3: If choice is 1 the get the element to be inserted in beginning and call ins_beg function.
         Step 4: If choice is 2 the get the element to be inserted in the end and call the ins_end function
         Step 5: If choice is 3 then get the element to be deleted and call deletion function.
         Step 6: If choice is 4 then call display duncation
         Step 7: If choice is default the exit the program
         Step 8: Terminate the program execution.
      Program:

LINKED LIST IMPLEMENTATION OF SINGLY LINKED LIST


LINKED LIST IMPLEMENTATION OF SINGLY LINKED LIST
Aim:
   To write a program to implement singly linked list using linked list.
Algorithm:
   Step 1: initialize the list as null
   Step 2: Display linked list operations insert, delete and display the result.
   Step 3: If choice is 1 the read element to be inserted and call the insert function
   Step 4: If choice is 2 then read element to be deleted and call the delete function
   Step 5: If choice is 3 then call display function
   Step 6: If choice is default the exit the program.
Prog Code:

LINKED LIST IMPLEMENTATION OF STACK


LINKED LIST IMPLEMENTATION OF STACK

Aim
            To demonstrate linked list implementation of stack using a C program.

Algorithm

            Step 1: [Include all the necessary header files]
            Step 2: [Declare the Variables]
            Step 3: Read operator
            Step 4: IF opt <---THEN
                        Step 4.1: READ n
                        Step 4.2: WHILE (n        n-1)
                                    Step 4.2.1: READ d
                                    Step 4.2.2: CALL INSERT( start , d)
                        Step 4.3: [End of while Structure]
            Step 5: IF opt         2 THEN
                        Step 5.1: READ x
                        Step 5.2: CALL del(start,x)
            Step 6: IF opt            3 THEN
                        Step 6.1: READ x
                        Step 6.2: CALL FIND
            Step 7: IF opt         4 THEN
                        Step 7.1: READ x
                        Step 7.2: CALL FINDPREVIOUS
Step 8: IF opt         5 THEN
                        Step 8.1: READ x
                        Step 8.2: CALL FINDNEXT(start, x)
Step 9: IF opt         6 THEN
            CALL len(Start)
Step 10: IF opt         7 THEN
            CALL printlist(Start)
Step 10: IF opt         8 THEN
            CALL erase (Start)
Step 12: [End of Main]

Algorithm For Find(struct node*p, int x, int *pos)
            Step 1: temp          p
            Step 2 :*pos         1
            Step 3: IF ( TEMP        NULL) THEN
                        RETURN NULL
                        ELSE
                        WHILE ( TEMP!= NULL && TEMP          DATA!= X)
                        ASSIGN 1 TO *POS+1 AND TEMP’S LLINK FIELD TO TEMP
                        RETURN THE TEMP

Algorithm for Previous (struct node*p, int x)
            Step 1: temp          p
            Step2: IF ( TEMP        NULL) THEN
                        RETURN NULL
                        ELSE
                        WHILE (TEMP        LINK != NULL && TEMP       LINK      DATA!= X)
                       

ASSIGN TEMP’S LINK FIELD TO TEMP
                        RETURN THE TEMP

Algorithm For Find next(struct node*p, int x)

            Step 1: temp          p
            Step2: IF ( TEMP        NULL) THEN
                        RETURN NULL
                        ELSE
                        WHILE (TEMP        LINK != NULL && TEMP      DATA!= X)
                        ASSIGN TEMP’S LLINK FIELD TO TEMP
                        RETURN THE TEMP’S LINK FIELD


Coding:

ARRAY IMPLEMENTATION OF CIRCULAR QUEUE


ARRAY IMPLEMENTATION OF CIRCULAR QUEUE
Aim
                        To write a c program using arrays for implementing circular queue data structure.

Algorithm

                        Step 1: [Include All Header Files Required]
                        Step 2: [Define the array size as 5 and declare front and rear pointers]
                        Step 3: Declare the functions isEmpty() , isFull(), enqueue(), size(),dequeue(), peek() and
 view()]
                        Step 4: [Call the functions]
                                    Choice :1  CALL enqueue()
                                    Choice :2  CALL deenqueue()
Choice :3  CALL peek()
Choice :4  CALL size()
Choice :5  CALL view()

Algorithm for isEmpty( )

                        Step 1: [Check for underflow]
                                    If ( front         -1 and rear          -1 )
                                    RETURN -1
                        Step 2: Else RETURN 0
                                    [Finish the process]


Algorithm for isFull( )

Step 1: [Check for overflow]
                                    If (= (rear+1)% qsize         front )
                                    RETURN -1
                        Step 2: Else RETURN 0
                                    [Finish the process]

Algorithm for Enqueue( )

STEP 1: If[front= =rear]
                        Initialize front=rear=0
STEP 2: else rear=(rear+1)% qsize
                         Set queue[rear] =value
                         [return]

Algorithm for Dequeue( )

STEP 1: If[front = =rear]
                   1.1: temp=queue[front]
                  1.2: Initialize front=rear=-1
STEP 2:else
                 2.1: front=(front+1)% qsize
                        [return]




Algorithm for Peek( )

STEP 1:If [front= =rear]
STEP 1.1: temp=queue[front]
                            [return]
Algorithm for Size( )

STEP 1:If [front= =rear]
                     1.1: Set f=front
                     1.2: Set count=1
STEP 2: If [front!=rear]
                      2.1: front=(front+1)%qsize
                      2.2: set count=count+1
                             [return]

Algorithm for View( )

STEP 1: If [front = =rear]
                         Write (“Queue is empty”)
STEP 2: else
                         [display elements]        

Coding:

IMPLEMENTATION OF LINEAR QUEUE USING ARRAYS


IMPLEMENTATION OF LINEAR QUEUE USING ARRAYS


Aim
To write a C-program to implement linear queue data structure using arrays.

Algorithm
STEP 1: Start
STEP 2: [Include all header files]
STEP 3: [Declare the variables]
STEP 4: [If n->1 call the function Enqueue( )]
STEP 5: [If n->2 call the function Dequeue( )]
STEP 6: [If n->3 call the function Peep( )]
STEP 7: [If n->4 call the function Size( )]
STEP 8: [If n->5 call the function View( )]
STEP 9: [else Exit( )]
STEP 10: Stop

Algorithm for Enqueue( )
STEP 1: If[front= =rear]
               Initialize front=rear=0
STEP 2: else rear=(rear+1)% qsize
               Set queue[rear] =value
               [return]
Algorithm for Dequeue( )
STEP 1: If[front = =rear]
          1.1: temp=queue[front]
          1.2: Initialize front=rear=-1
STEP 2:else
          2.1: front=(front+1)% qsize
                 [return]

Algorithm for Peep( )
STEP 1:If [front= =rear]
STEP 1.1: temp=queue[front]
                  [return]

Algorithm for Size( )

STEP 1:If [front= =rear]
          1.1: Set f=front
           1.2: Set count=1
STEP 2: If [front!=rear]
          2.1: front=(front+1)%qsize
          2.2: set count=count+1
                 [return]

Algorithm for View( )

STEP 1: If [front = =rear]
               Write (“Queue is empty”)
STEP 2: else
               [display elements]        
             



Coding:

CONVERSION OF INFIX EXPRESSION TO POSTFIX


CONVERSION OF INFIX EXPRESSION TO POSTFIX
Aim
To write a C-program to convert the given infix expression to its postfix format.

Algorithm

STEP 1: Start
STEP 2: Initialize the stack.
STEP 3: While (INSTR!= NULL)
STEP 4: CH= get the character from INSTR.
STEP 5: If( CH= = operand) then
               append CH int POSTSTR
               else if(CH = = ‘(‘) then
               push CH into stack
               else if(CH = =’)’) then
               pop the data from the stack and append the data into POSTSTR until we get                         
               ‘(‘ from the stack
               else
               while(precedence (TOP) >= precedence (CH))
               pop the data from stack and append the data into POSTSTR.
               [End of while structure]
               [End of if structure]

STEP 6: Push CH into the stack.
STEP 7: [End of second while structure]
STEP 8: pop all the data from stack and append data into POSTSTR.
STEP 9: Stop
Coding:

ARRAY IMPLEMENTATION OF STACK


ARRAY IMPLEMENTATION OF STACK
Aim
To write a C-program to implement stack using array data structure.
And perform the following stack operations
1.                              POP
2.                              PUSH
3.                              PEEP

Algorithm

STEP 1:Start

STEP 2:Initialize stack, will=1,i, num
STEP 3:Add element in stack
                                      PUSH(S,TOP,X)
                                                 3.a. [Check overflow condition]
                                                  If(TOP>=N) then
                                                  Write(“Stack is full”)
                                                3.b. [Insert element]
                                          [Increment TOP]
                                         TOP <- TOP+1
                                            S[TOP]<- X
3.c. [Finish the process]                 

STEP 4: Delete element in stack

                                      POP(S,TOP)
                                                 4.a. [Check for underflow condition]
                                                If(TOP <- 0) then
                                                Write(“Stack is empty”)
                                                4.b. [Delete element]
                                      [Decrement TOP]
                                      TOP<- TOP-1
                                      Delete S[TOP+1]
                                       4.c.[Finish the process]
STEP 5:Stop
Coding: