lab manual pl.doc

Upload: shital-kolte

Post on 04-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 lab manual PL.doc

    1/82

    210246 PROGRAMMING LABORATORY

    LAB MANUAL

    Department of Computer Engineering

    Padmashree Dr. D.Y. Patil Institute of Engineering and Technology,

  • 7/30/2019 lab manual PL.doc

    2/82

    Pimpri, Pune

    I LAB SYLLABUS

    II LIST OF ASSIGNMENTS

    III LAB PLAN

    IV INDEX

    V PSEUDO CODE FOR LAB ASSIGNMENTS

  • 7/30/2019 lab manual PL.doc

    3/82

    LAB SYLLABUS

    Teaching Scheme Examination Scheme

    Practical: 4 Hrs/week Practical: 50

    marks

    Class:S.E Term Work: 25marks

    Suggested List of Assignments:

    1. Write a program to perform Set operations - Union, Intersection, Difference, Symmetric

    Difference etc.

    2. Write a program to perform various string operations such as Copy, Length, Reversing,

    Palindrome, Concatenation and to find occurrence substring etc with and without using library

    functions.

    3. Write a program to perform operations on matrices like addition, multiplication, saddle point,

    magic square ,inverse & transpose etc using functions &pointers.[Minimum 4 operations]

    4. Write a program to perform following operations on any database: Add, Delete, Modify,

    Display, Search & Sort etc.

    5. Implement Sorting Methods using functions- Bubble Sort, Selection Sort, Insertion Sort, and

    Shell Sort.

    6. Implement Sorting Methods using recursion- Quick Sort and Merge Sort.

    7. Implement Searching Methods-Sequential Search, Binary Search, Fibonacci Search and Index

    Sequential Search.[Minimum 3 searching methods]

    8. Represent polynomial using structures and write a menu driven program to perform Addition,

    Multiplication and Evaluation.

    9. Represent Sparse Matrix using array and perform Matrix Addition, Simple and Fast

    Transpose.10. Write a menu driven program to perform following operations on SLL/CDLL :Create, Insert

    Start, end, between, Search & delete, Reverse ,Display etc.

    11. Create two Singly Linked lists, sort one after creation and one while creation using Pointer

    manipulation. Merge these two lists into one list without creating anew node or swapping of the

    data.

  • 7/30/2019 lab manual PL.doc

    4/82

    12. Represent a polynomial using Circular Linked List and write a menu driven program to

    perform Addition, Multiplication and Evaluation.

    13. Implement Stack as an ADT using Array. Use this ADT to perform expression conversion

    and evaluation (infix to postfix, infix to prefix, prefix to infix, prefix to postfix, postfix to infix

    and postfix to prefix).

    14. Represent Circular Queue using Linked List and write a program to perform operations like

    Insert, Delete, Finding front and rear element.

    15. Implement the Mini Project of Student Database using Linked list for following

    requirements:

    a. Creation of Student Database in memory containing student ID, Name, Name Initials,

    Address, Contact No and Date of Birth .

    b. Insertion, Deletion, Modification of student record for a given student ID.

    c. Sorting on name initials and searching a particular student record on name initials

    Note: All Assignments to be implemented using GCC(preferably) and Time and

    Space Complexity is to be verified with theoretical findings.Students will submit Term Work in the form Journal that will include minimumabove 15 assignments. Each assignment will consist of Pseudo algorithm, program

    listing with proper documentation and printout of the output.Practical examination will based on the Term Work and questions will be asked to

    judge the understanding of the assignments performed at the time of the

    examination.

    Reference Books:

    1. R. Gilberg, B. Forouzan, Data Structures: A pseudo code approach with C, CenageLearning, ISBN 9788131503140.

    2. E. Horowitz , S.Sahani, S.Anderson-Freed Fundamentals of Data Structures in C,

    Universities Press ,2008 ,ISBN 10:81737160563. A. Aho, J. Hopcroft, J. Ulman, Data Structures and Algorithms, Pearson Education,

    1998, ISBN-0-201-43578-0

    4. Y. Langsam, M. Augenstin and A. Tannenbaum, Data Structures using C and C++, 2nd

    Edition, Prentice Hall of India, 2002, ISBN-81-203-1177-95. J. Tremblay, P. Soresan, An introduction to data structures with Applications, 2nd

    edition, Tata McGraw-Hill International Editions, 1984, ISBN-0-07-462471-7.

  • 7/30/2019 lab manual PL.doc

    5/82

    LIST OF ASSIGNMENTS

    Aim:

    To develop skills in design and implementation of data structures and their applications.

    1. Write a program to perform Set operations - Union, Intersection, Difference, Symmetric

    Difference etc.

    2. Write a program to perform various string operations such as Copy, Length, Reversing,

    Palindrome, Concatenation and to find occurrence substring etc with and without using libraryfunctions.

    3. Write a program to perform operations on matrices like addition, multiplication, saddle point,

    magic square ,inverse & transpose etc using functions &pointers.[Minimum 4 operations]

    4. Write a program to perform following operations on any database: Add, Delete, Modify,

    Display, Search & Sort etc.

    5. Implement Sorting Methods using functions- Bubble Sort, Selection Sort, Insertion Sort, and

    Shell Sort.

    6. Implement Sorting Methods using recursion- Quick Sort and Merge Sort.

    7. Implement Searching Methods-Sequential Search, Binary Search, Fibonacci Search and Index

    Sequential Search.[Minimum 3 searching methods]

    8. Represent polynomial using structures and write a menu driven program to perform Addition,

    Multiplication and Evaluation.

    9. Represent Sparse Matrix using array and perform Matrix Addition, Simple and Fast

    Transpose.

    10. Write a menu driven program to perform following operations on SLL/CDLL :Create, Insert

    Start, end, between, Search & delete, Reverse ,Display etc.

    11. Create two Singly Linked lists, sort one after creation and one while creation using Pointer

    manipulation. Merge these two lists into one list without creating anew node or swapping of the

    data.

    12. Represent a polynomial using Circular Linked List and write a menu driven program to

    perform Addition, Multiplication and Evaluation.

    13. Implement Stack as an ADT using Array. Use this ADT to perform expression conversion

    and evaluation (infix to postfix, infix to prefix, prefix to infix, prefix to postfix, postfix to infix

    and postfix to prefix).

  • 7/30/2019 lab manual PL.doc

    6/82

    14. Represent Circular Queue using Linked List and write a program to perform operations like

    Insert, Delete, Finding front and rear element.

    LAB PLAN

    Class: S.E (CSE)

    Semester and Year: Semester I 2012-13

    Subject: Programming LaboratoryTeaching Scheme: Practical: 4 hrs/week

    Examination Scheme: Practical: 50 Marks

    Term Work: 25 Marks

    Assignment

    No.

    Name of the Assignment References

    1 Write a program to perform Set operations - Union,

    Intersection, Difference, Symmetric difference etc.

    1) R. Gilberg, B. Forouzan,

    Data Structures: A pseudo

    code approach with C, Cenage

    Learning.

    2) E. Horowitz , S.Sahani,

    S.Anderson-Freed

    Fundamentals of Data

    Structures in C, Universities

    Press ,2008.

    2 Write a program to perform various string operations

    such as Copy, Length, Reversing, Palindrome,Concatenation and to find occurrence substring etc

    with and without using library functions.

    -do-

    3 Write a program to perform operations on matrices

    like addition, multiplication, saddle point, magic

    -do-

  • 7/30/2019 lab manual PL.doc

    7/82

    square, inverse & transpose etc using functions &

    pointers. [Minimum 4 operations]

    4 Write a program to perform operations on any

    database: Add, Delete, Modify, Display, Search&

    Sort etc.

    5 Implement Sorting Methods using functions- Bubble

    Sort, Selection Sort, Insertion Sort, and Shell Sort.

    -do-

    6 Implement Sorting Methods using recursion- Quick

    Sort and Merge Sort.

    -do-

    7 Implement Searching Methods-Sequential Search,

    Binary Search, Fibonacci Search and Index

    Sequential Search.[Minimum 3 searching methods]

    -do-

    8 Represent polynomial using structures and write a

    menu driven program to perform Addition,

    Multiplication and Evaluation.

    -do-

    9 Represent Sparse Matrix using array and perform

    Matrix Addition, Simple and Fast Transpose.

    -do-

    10 Write a menu driven program to perform following

    operations on SLL: Create, Insert Start, end,

    between, Search & delete, Reverse, Display etc.

    -do-

    11 Write a menu driven program to perform following

    operations on CDLL: Create, Insert Start, end,

    between, Search & delete, Reverse, Display etc.

    -do-

    12 Create two Singly Linked lists, sort one after creation

    and one while creation using Pointer manipulation.

    Merge these two lists into one list without creating a

    new node or swapping of the data.

    -do-

    13 Represent a polynomial using Circular Linked List

    and write a menu driven program to performAddition, Multiplication and Evaluation.

    -do-

    14 Implement Stack as an ADT using Array. Use this

    ADT to perform expression conversion and

    evaluation (infix to postfix, infix to prefix, prefix to

    -do-

  • 7/30/2019 lab manual PL.doc

    8/82

    infix, prefix to postfix, postfix to infix and postfix to

    prefix).

    15 Represent Circular Queue using Linked List and

    write a program to perform operations like Insert,

    Delete, Finding front and rear element.

    -do-

    16 Implement the Mini Project of Student Database

    using Linked list for following requirements:

    a. Creation of Student Database in memory

    containing student ID, Name, Name

    Initials, Address, Contact No and Date of Birth.

    b. Insertion, Deletion, Modification of student record

    for a given student ID.

    c. Sorting on name initials and searching a particular

    student record on name initials

    -do-

  • 7/30/2019 lab manual PL.doc

    9/82

    Assignment No: 1

    Title: Set operation

    Problem statement: Write a program in C to perform set operations such as Union, Intersection,

    Difference, and Symmetric Difference

    Aim: To learn concept of array, functions, conditional statements such as ifelse, Switch casestatement

    Input: Set A, Set B

    Output: Union of set A &B: Set C=AUB,

    Intersection of A &B: Set C=AB

    Difference: Set C=A-B

    Symmetric Difference: Set C=AB

    Theory:

    Union of two sets: Given two sets A and B, the union is the set that contains elements or objects

    that belong to either A or to B or to both

    Suppose A and B are two sets then AB= {x| x A or x B}

    Pseudo code for Union

    Declare i,j,k=0,flag=0;

  • 7/30/2019 lab manual PL.doc

    10/82

    Declare c[20];

    For (i=0;i

  • 7/30/2019 lab manual PL.doc

    11/82

    Intersection of A and B

    Pseudo code for Intersection

    Declare I,j,k=0;

    Declare C[20]

    For(i=0;i

  • 7/30/2019 lab manual PL.doc

    12/82

    Pseudo code for Difference

    Declare i ,j ,k=0,flag=0

    Declare array C[20]

    For (i=0 ; i

  • 7/30/2019 lab manual PL.doc

    13/82

    Pseudo code for symmetric difference Difference

    Declare I,j,k=0,flag=0

    Declare array C[20]

    For (i=0 ;i

  • 7/30/2019 lab manual PL.doc

    14/82

    If flag ==0

    {C[k] =b[j]

    Increment k}

    }

    Print elements of C

    Conclusion: Successfully performed set operations by using concept of array, functions, and

    conditional statements

    Assignment No: 2

    Title: String operations

    Problem statement: Write a program to perform various string operations such as Copy,

    Length, Reversing, Palindrome, and Concatenation and to find occurrence substring etc with and

    without using library functions.

  • 7/30/2019 lab manual PL.doc

    15/82

    Aim: How to handle string

    Input: Array of character i.e. string

    Output: Display the length of string, Concatenation of two strings, Reverse the string, copying

    string, finding occurrence of substring

    Theory:

    String Definition: string is nothing but the set of characters. C only has character type so a C

    string is defined as an array of characters or a pointer to characters.

    Null-terminated String

    String is terminated by a special character which is called as null terminator or null parameter

    (/0). So when you define a string you should be sure to have sufficient space for the null

    terminator.

    Declaring String

    As in string definition, we have two ways to declare a string. The first way is, we declare an

    array of characters as follows:

    char s[] = "string";

    And in the second way, we declare a string as a pointer point to characters:

    char* s = "string";

    Declaring a string in two ways looks similar but they are actually different. In the first way, you

    declare a string as an array of character, the size of string is 7 bytes including a null terminator.

    But in the second way, the compiler will allocate memory space for the string and the base

    address of the string is assigned to the pointer variable s.

    Looping Through a String

  • 7/30/2019 lab manual PL.doc

    16/82

    You can loop through a string by using a subscript. Here is an example of looping through a

    string using a subscript:

    // loop through a string using a subscript

    char s[] = "C string";int i;

    for (i = 0; i < sizeof(s);i++){

    printf("%c",s[i]);

    }

    1) Length of sting: Gives the total number of character in the string

    strlen function: The strlen function returns the length of the null-terminatedstrings in bytes

    a= strlength(hello)

    will give length 5

    Pseudo code for calculating length of string

    int length(char s1[])

    Declare len

    For(len=0;s1[len]!=\0;len++)

    {Return (len)}

    2) Reverse the string: It will reverse the string. It will change the order of the character i.e

    the Ist character will be the last and IInd will be IInd last and so on..

    Char Str[10]=hello

    Strreverse(hello)=olleh .

    Pseudo code for reversing the string

    Void reverse(char s1[])

    {

    char temp;

    int i,len;len=strlen(s1)-1;

  • 7/30/2019 lab manual PL.doc

    17/82

    for(i=0;i

  • 7/30/2019 lab manual PL.doc

    18/82

    4) Comparison between two string:strcmp(s1,s2)

    Pseudo code to compare two string

    int compare(char s1[], char s2[]){

    int i = 0;

    while( s1[i] == s2[i] )

    {if( s1[i] == '\0' || s2[i] == '\0' )

    break;

    i++;}

    if( s1[i] == '\0' && s2[i] == '\0' )

    return (0);else

    return (-1);

    }

    5) Concatenation of two string: strcat(s1,s2)

    Pseudo Code for concatenation

    Void concat(char s1[] char s2[])

    {int i,j;Assign i=0

    While(s1[i]!=\0)

    i++;For (j=0;s2[j]=\0;i++,j++)

    s1[i]=s2[j]

    s1[i]=\0

    }

    6) Occurrence of substring

    Pseudo Code for substring

    Int substring(char s1[],char s2[]){

    Int I,j,len1,len2;

    Len1=length(s1);

    Len2=length(s2);For(i=0;i

  • 7/30/2019 lab manual PL.doc

    19/82

    {

    For(j=0;s1[i+j]==s2[j]&&s2[j]!=\o;j++);

    If(s2[j]==\0)Return(i+1);

    }

    Rturn 0;}

    Conclusion: How to handle string has been understood and copy, length ,substring

    ,palindrome ,reverse has been implemented successfully

  • 7/30/2019 lab manual PL.doc

    20/82

    Assignment No: 3

    Title: Database creation using structure

    Problem statement: Write a program to perform following operations on any database: Add,

    Delete, Modify, Display, Search & Sort etc.

    Aim: How to declare structure and how to perform operations on database

    Input: Array of structure

    Output: Different operations like Add, Delete, Modify, Display, Search & Sort etc.

    Theory:

    Structure:

    Structure is user defined data type which is used to store heterogeneous data under unique

    name. Keyword 'struct' is used to declare structure.

    The variables which are declared inside the structure are called as 'members of structure'.

    Syntax:

    struct structure_nm

    {

    element 1;

    element 2;- - - - - - - - - - -

    - - - - - - - - - - -

    element n;}struct_var;

    Example:

    struct emp_info{

  • 7/30/2019 lab manual PL.doc

    21/82

    char emp_id[10];

    char nm[100];

    float sal;}emp;

    Instances of Structure:

    Instances of structure can be created in two ways as,

    Instance 1:

    struct emp_info{

    char emp_id[10];

    char nm[100];float sal;

    }emp;

    Instance 2:

    struct emp_info

    {

    char emp_id[10];char nm[100];

    float sal;

    };struct emp_info emp;

    Aceessing Structure Members :

    Structure members can be accessed using member operator '.' . It is also called as 'dot operator'

    or 'period operator'.

    structure_var.member;

    C Function for add

  • 7/30/2019 lab manual PL.doc

    22/82

    void add(emp e[20],int n)

    {

    printf(\nenter name of emplyee:);flushall();

    gets(e[n].name);

    printf(\nenter employee id:);scanf(%d,&e[n].empid);

    printf(\nenter salary of the emplyee:);

    scanf(%d,&e[n].salary);printf(\nenter phone no of the emplyee:);

    flushall();

    gets(e[n].ph);

    }

    C function for Search

    void search(emp e[20],int n){

    int i,id;

    printf(\nenter employee id to be searched:);

    scanf(%d,&id);

    for(i=0;i

  • 7/30/2019 lab manual PL.doc

    23/82

    {

    if(e[i].empid==id)

    {

    printf(\nrecord present);

    printf(\ngive new name:);

    flushall();

    gets(e[i].name);

    printf(\nenter new salary:);

    scanf(%d,&e[i].salary);

    printf(\nenter new phone no:);

    flushall();

    gets(e[i].ph);

    printf(\nrecord modified);

    return;

    }}

    printf(\nrecord not present);

    }

    C function for Delete

    nt delrec(emp e[20],int n)

    {

    int i,id;

    printf(\nenter employee id to be deleted:);

    scanf(%d,&id);

    for(i=0;i

  • 7/30/2019 lab manual PL.doc

    24/82

    while(i

  • 7/30/2019 lab manual PL.doc

    25/82

    Assignment No: 4

    Title: Matrix operations

    Problem statement:. . Write a program to perform operations on matrices like addition,

    multiplication, saddle point, magic square ,inverse & transpose etc using functions & pointers.

    Aim: To understand the concept of two dimensional array as well as various operations on

    matrix

    Input: Matrix-A, Matrix-B

    Output: Addition of two matrix, multiplication of matrix, simple and fast transpose,I nverse,

    saddle point ,magic square

    Theory:

    MATRIX OPERATION:

    A matrix is a rectangular array of numbers, symbols, or expressions, arranged

    in rows and columns.In this code you can provide limit for number of rows and columns.The

    user need to accept two matrices and can perform following operations-

    addition,multiplication,transpose,inverse and magic square and can find out whether saddle point

    exits.

  • 7/30/2019 lab manual PL.doc

    26/82

    LOGIC OF MAGIC SQUARE:

    For magic squares with an odd number of rows/columns (i.e. 33, 55, etc.), there is a simple

    algorithm to fill the squares. Magic squares with even number of rows/columns do NOT follow

    this algorithm.

    Start in the top row, middle square. Place the number 1 there. The rest of the squares are filled as

    follows.

    Choose the next cell by moving up and to the right. If the square falls outside the squares, the

    next number goes in the first cell of the row above it. If there is no row above it, drop down to

    the bottom of the next column and place the number there. If there is no column to the right, drop

    immediately below and place the number there. If the cell is already filled, drop immediately

    below and place the number there. Continue until all cells are filled. The number in the middle of

    the sequence should fall in the middle column of the middle row. The formula for the total of

    each row/column is: total = n * ((( n * n ) / 2 ) + 0.5)

    C function for addition of two matrix

    void add(int a[20][20],int b[20][20],int r,int c){

    int i,j,res[20][20];for(i=0;i

  • 7/30/2019 lab manual PL.doc

    27/82

    for(i=0;i

  • 7/30/2019 lab manual PL.doc

    28/82

    }

    }

    rmax[i]=max;}

    for(j=0;j

  • 7/30/2019 lab manual PL.doc

    29/82

    {

    int r,c;

    for(r=0;r

  • 7/30/2019 lab manual PL.doc

    30/82

    /*Get the sum of diagnols*/

    sum[6]=num[0][0]+num[1][1]+num[2][2];

    sum[7]=num[0][2]+num[1][1]+num[2][0];

    }

    /*Function to Check the sum of rows,cols and diagnols*/

    char check_matrix()

    {char c;

    int i;

    for(i=0;i

  • 7/30/2019 lab manual PL.doc

    31/82

    Assignment No: 5

    Title: Sorting techniques

    Problem statement:. Implement Sorting Methods using functions- Bubble Sort, Selection Sort,

    Insertion Sort, and Shell Sort. Implement Sorting Methods using recursion- Quick Sort and

    Merge Sort.

    Aim tech: To understand various sorting techniques and how recursion work

    Input: Unsorted array

    Output: Sorted array

    Theory

    Sorting algorithm is an algorithm that puts elements of a list in a certain order

    Different sorting techniques

  • 7/30/2019 lab manual PL.doc

    32/82

    Bubble sort

    Bubble sort is a simple sorting algorithm. The algorithm starts at the beginning of the data set. It

    compares the first two elements, and if the first is greater than the second, it swaps them. It continues

    doing this for each pair of adjacent elements to the end of the data set. It then starts again with the first

    two elements, repeating until no swaps have occurred on the last pass. This algorithm's average and worst

    case performance is O(n2), so it is rarely used to sort large, unordered, data sets. Bubble sort can be used

    to sort a small number of items (where its asymptotic inefficiency is not a high penalty). Bubble sort can

    also be used efficiently on a list of any length that is nearly sorted

    Pseudocode of bubble sort

    For I = 0 to N - 2

    For J = 0 to N - 2

    If (A(J) > A(J + 1)Temp = A(J)A(J) = A(J + 1)

    A(J + 1) = Temp

    End-IfEnd-For

    End-For

    Selection sort

    Selection sort is an in-place comparison sort. It has O(n2

    ) complexity, making it inefficient onlarge lists, and generally performs worse than the similar insertion sort. Selection sort is noted

    for its simplicity, and also has performance advantages over more complicated algorithms in

    certain situations.

    The algorithm finds the minimum value, swaps it with the value in the first position, and repeats

    these steps for the remainder of the list. It does no more than n swaps, and thus is useful where

    swapping is very expensive.

    Pseudo code of selection sort

    For I = 0 to N-1 do:Smallsub = I

    For J = I + 1 to N-1 do:

  • 7/30/2019 lab manual PL.doc

    33/82

    If A(J) < A(Smallsub)

    Smallsub = J

    End-IfEnd-For

    Temp = A(I)

    A(I) = A(Smallsub)A(Smallsub) = Temp

    End-For

    Insertion sort

    Insertion sortis a simple sorting algorithm that is relatively efficient for small lists and mostly

    sorted lists, and often is used as part of more sophisticated algorithms. It works by taking

    elements from the list one by one and inserting them in their correct position into a new sorted

    list. In arrays, the new list and the remaining elements can share the array's space, but insertion is

    expensive, requiring shifting all following elements over by one. Shell sort is a variant of

    insertion sort that is more efficient for larger lists.

    Pseudo code of Insertion sort

    For I = 1 to N-1J = I

    Do while (J > 0) and (A(J) < A(J - 1)

    Temp = A(J)A(J) = A(J - 1)

    A(J - 1) = Temp

    J = J - 1

    End-DoEnd-For

    Shell sort

    A Shell sort, different from bubble sort in that it moves elements numerous positions swapping

    Shell sortwas invented by Donald Shell in 1959. It improves upon bubble sort and insertion sort

    by moving out of order elements more than one position at a time. One implementation can be

    described as arranging the data sequence in a two-dimensional array and then sorting the

    columns of the array using insertion sort.

  • 7/30/2019 lab manual PL.doc

    34/82

    Pseudo code for shell sort

    input: an array a of length n with array elements numbered 0 to n 1

    inc round(n/2)

    while inc > 0 do:fori = inc .. n 1 do:

    temp a[i] j i

    whilej inc and a[j inc] > temp do:

    a[j] a[j inc] j j inc

    a[j] temp

    inc round(inc / 2.2)

    Conclusion: Implemented all sorting techniques successfully

    Assignment No: 6

    Title: Recursive Sorting techniques

    Problem statement: Implement Sorting Methods using recursion- Quick Sort and Merge Sort.

    Aim tech: To understand various sorting techniques and how recursion work

    Input: Unsorted array

    Output: Sorted array

    Theory

    Merge sort

    Merge sorttakes advantage of the ease of merging already sorted lists into a new sorted list. It

    starts by comparing every two elements (i.e., 1 with 2, then 3 with 4...) and swapping them if the

  • 7/30/2019 lab manual PL.doc

    35/82

    first should come after the second. It then merges each of the resulting lists of two into lists of

    four, then merges those lists of four, and so on; until at last two lists are merged into the final

    sorted list. Of the algorithms described here, this is the first that scales well to very large lists,

    because its worst-case running time is O(n log n).

    Pseudocode for merge sort

    function merge_sort(listm)

    // if list size is 1, consider it sorted and return it

    iflength(m) 1, so split the list into two sublists

    varlistleft, right

    varintegermiddle = length(m) / 2

    for each x in m before middleadd x to left

    for each x in m after or equal middleadd x to right

    // recursively call merge_sort() to further split each sublist

    // until sublist size is 1left = merge_sort(left)

    right = merge_sort(right)

    // merge the sublists returned from prior calls to merge_sort()

    // and return the resulting merged sublist return merge(left, right)

    In this example, the merge function merges the left and right sublists.

    function merge(left, right)

    varlistresult while length(left) > 0 or length(right) > 0

    iflength(left) > 0 and length(right) > 0

    iffirst(left) 0

    append first(left) to resultleft = rest(left)

    else iflength(right) > 0

    append first(right) to result

  • 7/30/2019 lab manual PL.doc

    36/82

    right = rest(right)

    end while

    return result

    Quicksort

    Quicksortis a divide and conquer algorithm which relies on a partition operation: to partition an

    array an element called apivotis selected. All elements smaller than the pivot are moved before

    it and all greater elements are moved after it. This can be done efficiently in linear time and in-

    place. The lesser and greater sublists are then recursively sorted. Efficient implementations of

    quicksort (with in-place partitioning) are typically unstable sorts and somewhat complex, but are

    among the fastest sorting algorithms in practice. Together with its modest O(log n) space usage,

    quicksort is one of the most popular sorting algorithms and is available in many standard

    programming libraries. The most complex issue in quicksort is choosing a good pivot element;

    consistently poor choices of pivots can result in drastically slower O( n) performance, if at each

    step the median is chosen as the pivot then the algorithm works in O(n log n). Finding the

    median however, is an O(n) operation on unsorted lists and therefore exacts its own penalty with

    sorting.

    Pseudocode for Quick sort

    void quicksort(int arr[],int low,int high){

    int pivot,j,temp,i;

    if(low

  • 7/30/2019 lab manual PL.doc

    37/82

    arr[j]=temp;

    }

    }

    temp=arr[pivot];

    arr[pivot]=arr[j];arr[j]=temp;

    quicksort(arr,low,j-1);quicksort(arr,j+1,high);

    }

    }

    Conclusion: Implemented recursive sorting techniques successfully

    Assignment No: 7

    Title: Searching techniques

    Problem statement: Implement Searching Methods-Sequential Search, Binary Search,

    Fibonacci Search and Index Sequential Search.[Minimum 3 searching methods]

    Aim tech: To understand various searching techniques.

    Input: List of elements

    Output: Position of searched element

    Theory:

  • 7/30/2019 lab manual PL.doc

    38/82

    Search operations

    Linear Search: In linear search or sequential search is a method for finding a particular

    value in a list, that consists of checking every one of its elements, one at a time and in

    sequence, until the desired one is found.

    Binary Search: In binary search or half-interval search algorithm finds the position of a

    specified value (the input key) within a sorted array. In each step, the algorithm

    compares the input key value with the key value of the middle element of the array. If the

    keys match, then a matching element has been found so its index, or position, is returned.

    Otherwise, if the sought key is less than the middle elements key, then the algorithm

    repeats its action on the sub-array to the left of the middle element or, if the input key is

    greater, on the sub-array to the right. If the remaining array to be searched is reduced to

    zero, then the key cannot be found in the array and a special Not found indication is

    returned.

    Fibonacci Search: In Fibonacci search technique is a method of searching a sorted

    array using a divide and conquer algorithm that narrows down possible locations with the

    aid of Fibonacci numbers. Compared to binary search, Fibonacci search examines

    locations whose addresses have lower dispersion. Therefore, when the elements being

    searched have non-uniform access memory storage (i.e., the time needed to access a

    storage location varies depending on the location previously accessed), the Fibonacci

    search has an advantage over binary search in slightly reducing the average time needed

    to access a storage location.

    C function for linear search

    void lsearch(int a[20],int n,int x)

    {int i,f=-1;

    printf(\n\n enter no which you want to search );scanf(%d,&x) ;

    for(i=0;i

  • 7/30/2019 lab manual PL.doc

    39/82

    {

    f=i+1;

    printf(\n no is found at position %d,f);break;

    }//end if

    }//end for

    if (f==-1){

    printf(\n no is not found);

    }//end if

    }//end search

    C Function for Binary Search

    /**/// FUNCON NAME : BSEARCH //

    // USE : USE FOR BINARY SEARCHING ///**/

    int bsearch(int a[20],int n,int x)

    { int l,h,mid;

    printf(\n\n enter no which you want to search );scanf(%d,&x);

    l=0;

    h=n-1;while(l

  • 7/30/2019 lab manual PL.doc

    40/82

    {

    l=mid+1;

    }

    }//end else

    }//end else 1

    }//end while

    return(-1) ;

    }// end bsearch

    C Function for Fibonacci Search

    /**/

    // FUNCON NAME : FSEARCH //

    // USE : USE FOR SEARCHING ///**/

    int fsearch(int a[20],int n,int x)

    { int fib[20],pos,k,mid=0;

    printf(\n\n enter no which you want to search );scanf(%d,&x);

    int fib[]={0,1,1,2,3,5,8,13,21,34,55};

    for(k=0;fib[k]=0){pos=mid+fib[k];

    if((pos>=n)&&(xa[pos])

    {

    mid=pos+1;k=k-2;

    }

    else{

    return(pos);

  • 7/30/2019 lab manual PL.doc

    41/82

    }

    }

    }//end whilereturn(-1);

    }//end fsearch

    /**/

    // FUNCON NAME : READ. //// USE : USE FOR READING ARRAY. //

    /**/

    int read(int n,int a[20]){

    int i;

    printf(\n enter no of element );

    scanf(%d,&n);

    printf(\n enter elements :);for(i=0;i

  • 7/30/2019 lab manual PL.doc

    42/82

    Input: Enter two Polynomials.

    Output: Polynomial and the addition, multiplication and evolution of the polynomial.

    Theory:

    Definition:

    By now, you should be familiar with variables and exponents, and you may have dealt with

    expressions like 3x4 or 6x. Polynomials are sums of these "variables and exponents" expressions.Each piece of the polynomial, each part that is being added, is called a "term". Polynomial terms

    have variables which are raised to whole-number exponents (or else the terms are just plain

    numbers); there are no square roots of variables, no fractional powers, and no variables in thedenominator of any fractions. Here are some examples:

    Here is a typical polynomial:

    A polynomial is an expression of finite length constructed from variables (also

    called indeterminate) and constants, using only the operations of addition,

    subtraction, multiplication, and non-negative integerexponents.

    For example,x2 x/4 + 7 is a polynomial, butx2 4/x + 7x3/2 is not, because its secondterm involves division by the variablex (4/x), and also because its third term contains an

    exponent that is not an integer (3/2).

    Functions:

    C function for Addition:

    void add(int n1, int n2,struct poly p1[10],struct poly p2[10]){

    int i,j,k;

    http://www.purplemath.com/modules/variable.htmhttp://www.purplemath.com/modules/exponent.htmhttp://en.wikipedia.org/wiki/Multiplicationhttp://en.wikipedia.org/wiki/Integerhttp://en.wikipedia.org/wiki/Exponentiationhttp://www.purplemath.com/modules/variable.htmhttp://www.purplemath.com/modules/exponent.htmhttp://en.wikipedia.org/wiki/Multiplicationhttp://en.wikipedia.org/wiki/Integerhttp://en.wikipedia.org/wiki/Exponentiation
  • 7/30/2019 lab manual PL.doc

    43/82

    struct poly p3[10];

    i=0;

    j=0;k=0;

    while(i

  • 7/30/2019 lab manual PL.doc

    44/82

    }//end while

    } //end if else

    printf(\n\n**** addition is );

    printpoly(p3,k);

    }//end add

    C function for Multiplication:

    void multi(int n1,int n2, struct poly p1[10],struct poly p2[10])

    {int i,j,k,l,c,e,f;

    struct poly p3[10];

    k=0;

    for(i=0;i

  • 7/30/2019 lab manual PL.doc

    45/82

    C function for Evaluate:

    void ele(int n4,struct poly p4[10]){

    int r,i,x;

    r=0;n4= read(p4);

    printf(\n your enter polynomial is );

    printpoly(p4,n4) ;printf(\n \n enter value of x);

    scanf(%d,&x);

    for(i=0;i

  • 7/30/2019 lab manual PL.doc

    46/82

    Fast Transpose.

    Aim: To Understand the Sparse Matrix and different operation performed on it.

    Input: Two Matrixes.

    Output: Display the Sparse Matrix ,Simple and Fast Transpose and the addition of two sparse

    matrix.

    Theory:

    Definition:

    A sparse matrix is a matrix populated primarily with zeros. The term itself was coined

    by Harry M. Markowitz. If the majority of elements differ from zero then it is common to refer

    to the matrix as a dense matrix

    A useful application of linear list is the representation of matrices that contain a preponderanceof zero elements. These matrices are called sparse matrices. Consider the matrix

    0 0 6 0 9 0 0

    2 0 0 7 8 0 4

    10 0 0 0 0 0 0

    0 0 12 0 0 0 0

    0 0 0 0 0 0 0

    0 0 0 3 0 0 5

    Of the 42 elements in the 6*7 matrix, only 10 are nonzero. One of the basic methods of storing

    is to store nonzero elements in a one-dimensional array and to identify each array element with

    row and column indices as shown in figure (a). The ith element of vector A is the matrix elementwith row and column indices ROW[i] and COLUMN[i]. Matrix elements are stored in row major

    order with zero elements removed. In a more efficient representation shown in figure (b), the

    ROW vector is changed so that its ith element is the index to the first of column indices for the

    elements in row i of the matrix.

    ROW COLUMN A

    1 1 3 6

    2 1 5 9

    3 2 1 2

    4 2 4 7

    5 2 5 8

    6 2 7 4

    7 3 1 10

    8 4 3 12

    9 6 4 3

    http://en.wikipedia.org/wiki/Harry_M._Markowitzhttp://en.wikipedia.org/wiki/Harry_M._Markowitz
  • 7/30/2019 lab manual PL.doc

    47/82

    10 6 7 5

    figure (a)

    COLUMN A

    1 3 6

    ROW 2 5 9

    1 1 3 1 2

    2 3 4 4 7

    3 7 5 5 8

    4 8 6 7 4

    5 0 7 1 10

    6 9 8 3 12

    row 9 4 3

    number 10 7 5

    column

    number

    FUNCTIONS:

    Void dispsparse(sp p[20])

    {

    int i;

    printf(\n ROW\tCOL\tVALUE);

    for(i=0;i

  • 7/30/2019 lab manual PL.doc

    48/82

    {

    res[k].row=p[j].col;

    res[k].col=p[j].row;

    res[k].val=p[j].val;

    k++;

    }

    }

    }

    res[0].row=p[0].col;

    res[0].col=p[0].row;

    res[0].val=p[0].val;

    printf(\nSimple Transpose:);

    dispsparse(res);

    }

    C function for fast transporse:

    void fasttrans(sp p[20])

    {

    int i,j,k;

    sp res[20];

    int cnt[20]={0};

    int pos[20];

    for(i=1;i

  • 7/30/2019 lab manual PL.doc

    49/82

    res[k].row=p[i].col;

    res[k].val=p[i].val;

    }

    res[0].col=p[0].row;

    res[0].row=p[0].col;

    res[0].val=p[0].val;

    printf(\nFast Transpose:);

    dispsparse(res);

    }

    C function for Addition:

    void addition(sp p[20],sp p1[20])

    {

    sp res[20];

    int i,j,k;

    i=j=k=1;

    while(i

  • 7/30/2019 lab manual PL.doc

    50/82

    k++;

    }

    else

    {

    if(p1[j].col < p[i].col)

    {

    res[k]=p1[j];

    j++;

    k++;

    }

    else

    {

    res[k].row=p[i].row;

    res[k].col=p[i].col;

    res[k].val=p[i].val+p1[j].val;i++;

    j++;

    k++;

    }

    }

    }

    }

    }

    while(i

  • 7/30/2019 lab manual PL.doc

    51/82

    Conclusion:Successfully performedSparse Matrix using array and perform Matrix

    Addition, Simple and Fast Transpose.

    Assignment No.: 10

    Title: Implementation Of SLL

    Problem Statement: Write a menu driven program to perform following operations on SLL:

    Create, Insert Start, end, between, Search & delete, Reverse, Display etc.

    Aim: To Learn the concept of SLL and different operation to perform on it.

    Input: Enter the data on the node.

    Output: Display the nodes in SLL and perform different operation.

    Theory:

    Definition:

    In a singly-linked list every element contains some data and a link to the next element,

    which allows keeping the structure. On the other hand, every node in a doubly-linked list also

    contains a link to the previous node. Linked list can be an underlying data structure to implement

    stack, queue or sorted list.

    On the other hand, simple linked lists by themselves do not allow random access to the

    data, or any form of efficient indexing. Thus, many basic operations such as obtaining the last

    node of the list (assuming that the last node is not maintained as separate node reference in the

    list structure), or finding a node that contains a given datum, or locating the place where a new

    node should be inserted may require scanning most or all of the list elements.

    Example:

    Sketchy, singly-linked list can be shown like this:

    Each cell is called a node of a singly-linked list. First node is called head and it's a

    dedicated node. By knowing it, we can access every other node in the list. Sometimes, last node,

    called tail, is also stored in order to speed up add operation.

    http://en.wikipedia.org/wiki/Random_accesshttp://en.wikipedia.org/wiki/Random_access
  • 7/30/2019 lab manual PL.doc

    52/82

    FUNCTION:

    C function for Create:

    num *create()

    { num *hn=NULL ,*cn=NULL ,*nn=NULL;int i,n;

    printf(\n enter no of node );

    scanf(%d,&n);

    printf(\n enter data\t);

    for(i=0;idata);

    nn->link=NULL;

    if(hn==NULL)

    {

    hn=nn;

    cn=nn;

    }//end if

    else

    {

    cn->link=nn;

    cn=nn;

    }}//end for

    return(hn);

    }//end create

    C function for Display:

    void display(num *hn)

    {

    num *cn=NULL;

    printf(\n data present in link list is );

    for(cn=hn;cn!=NULL;cn=cn->link){

    printf( %d,cn->data);

    }//end for

  • 7/30/2019 lab manual PL.doc

    53/82

    }//end display

    C function for Search:

    void search(num *hn)

    {num *cn=NULL;

    int x,f=0;

    printf(\n enter no you want to search );

    scanf(%d,&x);

    for(cn=hn;cn!=NULL;cn=cn->link)

    {

    if(x==cn->data)

    {

    f=1;

    }//end if

    }//end for

    if(f==1)

    {

    printf(\n number is present);

    }

    else

    {

    printf(\n no not present);}

    }//end search

    C function for Delete:

    num *deletenode(num *hn)

    {

    int n;

    num *cn=NULL,*pre=NULL;

    printf(\n enter no which u want to delete );scanf(%d,&n);

    for(cn=hn;cn!=NULL;cn=cn->link)

    {

  • 7/30/2019 lab manual PL.doc

    54/82

    if(n==cn->data)

    {

    if(cn==hn)

    {

    hn=hn->link;

    return(hn);

    }

    else

    {

    pre->link=cn->link;

    }

    free(cn);

    break;

    }//end ifpre=cn;

    }//end for

    return(hn);

    }//end fun

    C function for Reverse:

    num *reverse(num *hn)

    {

    num *p,*q,*r;

    p=hn;

    q=p->link;

    r=q->link;

    hn->link=NULL;

    printf(\n after call of reverse function);

    while(q!=NULL)

    { q->link=p;

    p=q;q=r;

    r=r->link;

    }//end while

    return(p);

  • 7/30/2019 lab manual PL.doc

    55/82

    }//end reverse

    C function for Insert:

    void ia(num *hn)

    {

    int no;

    num *cn,*nn;

    printf(\n insert no after which data is inserted );

    scanf(%d,&no);

    nn=create();

    for(cn=hn;cn!=NULL;cn=cn->link)

    {

    if(no==cn->data)

    {

    nn->link=cn->link;cn->link=nn;

    break;

    } //end if

    }//end for

    }//end insertafter fun

    num *ib(num *hn)

    {

    int no;

    num *cn,*nn,*pre=NULL;printf(\n insert no before which data is inserted );

    scanf(%d,&no);

    nn=create();

    for(cn=hn;cn!=NULL;cn=cn->link)

    {

    if(no==cn->data)

    {

    if(cn==hn)

    {

    nn->link=hn;

    hn=nn;

    }

    else

    {

  • 7/30/2019 lab manual PL.doc

    56/82

    nn->link=cn;

    pre->link=nn;

    }

    } //end if

    pre=cn;

    }//end for

    return(hn);

    }//end insert before

    num *is(num *hn)

    {

    num *nn;nn=create();

    nn->link=hn;

    hn=nn;

    return(hn);

    }//end fun

    void il(num *hn)

    {

    num *nn,*cn;

    nn=create();

    for(cn=hn;cn->link!=NULL;cn=cn->link)

    {

    }

    cn->link=nn;

    }//end fun

    void insert(num *hn)

    { int ch;

    do

    { printf(\n\n 1. insert start\n 2. insert last);

    printf(\n 3. insert before\n 4. insert after);

    printf(\n enter choice );

  • 7/30/2019 lab manual PL.doc

    57/82

    scanf(%d,&ch);

    switch(ch)

    {

    case 1 : hn=is(hn);

    display(hn);

    break;

    case 2 : il(hn);

    display(hn);

    break;

    case 3 : ib(hn);

    display(hn);

    break;

    case 4 : ia(hn);

    display(hn);

    break;

    case 5 : exit(0) ;

    default : printf(\n option not avaliable);

    }

    }while(ch!=5);

    }//end insert

    Conclusion:Successfully performedSingly Link List (SLL) and its operation.

  • 7/30/2019 lab manual PL.doc

    58/82

    Assignment No.: 11

    Title: Implementation Of CDLL

    Problem Statement: Write a menu driven program to perform following operations on CDLL:

    Create, Insert Start, end, between, Search & delete, Reverse ,Display etc.

    Aim: To Learn the concept of CDLL and different operation to perform on CDLL.

    Input: Enter the data on the node.

    Output: Display the nodes in CDLL and perform different operation.

    Theory:

    In the preceding section we saw that the running time of dequeue Head is O(1), but that therunning time of dequeue Tail is O(n), for the linked-list implementation of a deque. This is

    because the linked list data structure used, Linked List, is asingly-linked list. Each element in a

    singly-linked list contains a single reference--a reference to the successor (next) element of thelist. As a result, deleting the head of the linked list is easy: The new head is the successor of the

    old head.

    However, deleting the tail of a linked list is not so easy: The new tail is the predecessor of the

    original tail. Since there is no reference from the original tail to its predecessor, the predecessormust be found by traversing the linked list from the head. This traversal gives rise to the O(n)

    running time.

    In a doubly-linked list, each list element contains two references--one to its successor and one to

    its predecessor. There are many different variations of doubly-linked lists: Figure illustrates

    http://www.brpreiss.com/books/opus5/html/page165.html#figdeque2
  • 7/30/2019 lab manual PL.doc

    59/82

    three Figure (a) shows the simplest case: Two variables, say headand tail, are used to keep

    track of the list elements. One of them refers to the first element of the list, the other refers to the

    last. The first element of the list has no predecessor, therefore that reference is null. Similarly,the last element has no successor and the corresponding reference is also null. In effect, we have

    two overlapping singly-linked lists which go in opposite directions. Figure also shows the

    representation of an empty list. In this case the head and tail variables are both null.

    A circular, doubly-linked list is shown in Figure (b). A circular list is formed by making useof variables which would otherwise be null: The last element of the list is made the predecessor

    of the first element; the first element, the successor of the last. The upshot is that we no longer

    need both a head and tail variable to keep track of the list. Even if only a single variable is used,both the first and the last list elements can be found in constant time.

    Finally, Figure (c) shows a circular, doubly-linked list which has a single sentinel. This

    variation is similar to the preceding one in that both the first and the last list elements can be

    found in constant time. This variation has the advantage that no special cases are required when

    dealing with an empty list. Figure shows that the empty list is represented by a list withexactly one element--the sentinel. In the case of the empty list, the sentinel is both is own

    successor and predecessor. Since the sentinel is always present, and since it always has both asuccessor and a predecessor, the code for adding elements to the empty list is identical to that for

    adding elements to a non-empty list.

    of them.

    http://www.brpreiss.com/books/opus5/html/page165.html#figdeque2http://www.brpreiss.com/books/opus5/html/page165.html#figdeque2http://www.brpreiss.com/books/opus5/html/page165.html#figdeque2http://www.brpreiss.com/books/opus5/html/page165.html#figdeque2http://www.brpreiss.com/books/opus5/html/page165.html#figdeque2http://www.brpreiss.com/books/opus5/html/page165.html#figdeque2
  • 7/30/2019 lab manual PL.doc

    60/82

    Function:

    C function for Create:

    node *create()

    { node *head,*p;int i,n;

    head=NULL;

    printf(\n Enter no of data:);scanf(%d,&n);

    printf(\nEnter the data:);

    for(i=0;inext=head->prev=head;

    }

    else{

  • 7/30/2019 lab manual PL.doc

    61/82

    p=(node*)malloc(sizeof(node));

    p->next=head->next;

    p->prev=head;head->next->prev=p;

    head->next=p;

    head=p;}

    scanf(%d,&(head->data));

    }return(head);

    }

    C function for Insert:

    node *insert_b(node *head,int x)

    { node *p;

    p=(node*)malloc(sizeof(node));p->data=x;

    if(head==NULL){

    head=p;

    head->next=head->prev=head;}

    else

    {

    p->next=head->next;p->prev=head;

    head->next->prev=p;head->next=p;}

    return(head);

    }node *insert_e(node *head,int x)

    { node *p;

    p=(node*)malloc(sizeof(node));p->data=x;

    if(head==NULL)

    {

    head=p;head->next=head->prev=head;

    }

    else{

    p->next=head->next;

    p->prev=head;

  • 7/30/2019 lab manual PL.doc

    62/82

    head->next->prev=p;

    head->next=p;

    head=p;}

    return(head);

    }

    node *insert_in(node *head,int x)

    { node *p,*q;

    int y;p=(node*)malloc(sizeof(node));

    p->data=x;

    printf(\Insert after which number ? : );scanf(%d,&y);

    //locate the lthe data y

    q=head->next;do

    {

    if(y==q->data)

    break;q=q->next;

    }while(q != head->next);

    if(q->data==y){

    p->next=q->next;

    p->prev=q;

    p->next->prev=p;p->prev->next=p;

    }

    elseprintf(\nData not found );

    return(head);

    }node *delete_b(node *head)

    {

    node *p,*q;if(head==NULL)

    {

    printf(\nUnderflow.Empty Linked List);return(head);

    }

    if(head->next==head)//delete the only node

    {p=head;

  • 7/30/2019 lab manual PL.doc

    63/82

    free(p);

    head=NULL;

    }else

    {

    p=head->next;p->prev->next=p->next;

    p->next->prev=p->prev;

    free(p);}

    return(head);

    }

    C function for Delete:

    node *delete_e(node *head)

    {

    node *p,*q;

    if(head==NULL){

    printf(\nUnderflow.Empty Linked List);

    return(head);}

    if(head->next==head)//delete the only node

    {

    p=head;free(p);

    head=NULL;}

    else

    {

    p=head;head=p->prev;

    p->prev->next=p->next;

    p->next->prev=p->prev;free(p);

    }

    return(head);

    }

    node *delete_in(node *head)

    {

  • 7/30/2019 lab manual PL.doc

    64/82

    node *p,*q;

    int x,i;

    if(head==NULL){

    printf(\nUnderflow.Empty Linked List);

    return(head);}

    printf(\nEnter the data to be deleted : );

    scanf(%d,&x);p=head->next;

    do

    {

    if(p->data==x)break;

    p=p->next;

    } while(p!=head->next);

    if(p->data!=x){

    printf(\nUnderflow..data not found);return(head);

    }

    if(p==head)head=head->prev;

    if(p->next==p)

    {

    head=NULL;free(p);

    }

    else{

    p->prev->next=p->next;

    p->next->prev=p->prev;free(p);

    }

    return(head);

    }

    C function for Search:

    void search(node *head)

    { node *p;

    int data,loc=1;printf(\nEnter the data to be searched: );

    scanf(%d,&data);

    p=head->next;

  • 7/30/2019 lab manual PL.doc

    65/82

    do

    {

    if(p->data==data)break;

    p=p->next;

    loc++;} while(p!=head->next);

    if(p->data !=data)

    printf(\nNot found:);

    elseprintf(\nFound at location=%d,loc);

    }

    Conclusion:Successfully performed CDLL and its operation.

  • 7/30/2019 lab manual PL.doc

    66/82

    Assignment No.: 12

    Title: Operation on Singly Linked List

    Problem Statement:

    Create two Singly Linked lists, sort one after creation and one while creation using

    Pointer manipulation. Merge these two lists into one list without creating a new node orswapping of the data.

    Aim: To learn the sorting and merging in two SLL.

    Input: Enter the data on the node, create the SLL..

    Output: Display the sorting and merging in SLL.

    Theory:

    Definition:

    Create two singly linked lists sort one after creation & one while creation. Merge these two lists

    into one list without creating a new node.

    In this code, user enters number of nodes and then enters elements of the nodes in an unsortedanner and for second linked list user enters elements in sorted manner and this code merges these

    two linked list without creating a new node.

    Function:

    C function for Merge:

    node *merge(node *l1,node *l2)

    { node *l,*p;

    l=NULL;while(l1!=NULL && l2!=NULL)

    { if(l1->data < l2->data)

    { if(l==NULL){ l=p=l1;

    l1=l1->next;

    }

    else{ p->next=l1;

  • 7/30/2019 lab manual PL.doc

    67/82

    l1=l1->next;

    p=p->next;

    }}

    else

    { if(l==NULL){ l=p=l2;

    l2=l2->next;

    }else

    { p->next=l2;

    l2=l2->next;

    p=p->next;}

    }

    }

    if(l1!=NULL){ if(l==NULL)

    l=l1;else

    p->next=l1;

    }if(l2!=NULL)

    { if(l==NULL)

    l=l2;

    elsep->next=l2;

    }

    return(l);}

    C function for create normal node:

    node *create_normal(){ node *head=NULL,*p;

    int n,x,i;

    printf(\nNumber of nodes:);

    scanf(%d,&n);printf(\nEnter data:);

    for(i=1;inext=NULL;

  • 7/30/2019 lab manual PL.doc

    68/82

    }

    else

    {p->next=(node*)malloc(sizeof(node));p=p->next;

    p->next=NULL;

    }p->data=x;

    }

    return(head);}

    C function for Create sorted :

    node *create_sorted()

    { node *head=NULL,*p,*q;

    int n,x,i;printf(\nNumber of nodes:);

    scanf(%d,&n);printf(\nEnter data:);

    for(i=1;idata=x;

    p->next=NULL;

    if(head==NULL || xdata){ p->next=head;

    head=p;}else

    { q=head;

    while(q->next !=NULL && x>q->next->data)q=q->next;

    p->next=q->next;

    q->next=p;}

    }

    return(head);

    }void print(node *head)

    { printf(\n);

    while(head != NULL){ printf(%5d,head->data);

    head=head->next;

  • 7/30/2019 lab manual PL.doc

    69/82

    }

    }

    C function for Sort:

    void sort(node *head){ int i,j,n,temp;

    node *p;

    /*counting number of nodes*/for(n=0,p=head;p!=NULL;p=p->next)

    n++;

    for(i=1;i p->next->data)

    { temp=p->data;

    p->data=p->next->data;p->next->data=temp;

    }p=p->next;

    }

    }}

    Conclusion:Successfully performed the sorting and merging operation on

    SLL.

  • 7/30/2019 lab manual PL.doc

    70/82

    Assignment No.: 13

    Title Implementation of Polynomial Using CLL

    Problem Statement: Represent a polynomial using Circular Linked List and write a menu

    driven program to perform Addition, Multiplication and Evaluation.

    Aim: To understand the polynomial and how to store it using Circular Linked List and perform

    different operation.

    Input: Enter two Polynomials.

    Output: Polynomial and the addition, multiplication and evolution of the polynomial.

    Theory:

    Definition:

    A polynomial is an expression of finite length constructed from variables (also

    called indeterminate) and constants, using only the operations addition,subtraction, multiplication, and non-negative integerexponents.

    For example,x2 x/4 + 7 is a polynomial, butx2 4/x + 7x3/2 is not, because its second

    term involves division by the variablex (4/x), and also because its third term contains an

    exponent that is not an integer (3/2).

    Function:

    C function for Create:

    /*Function Name-createnewnode

    Function Use- to createnew node

    */POLY* createnewnode()

    {

    POLY *nn=NULL;

    http://en.wikipedia.org/wiki/Multiplicationhttp://en.wikipedia.org/wiki/Integerhttp://en.wikipedia.org/wiki/Exponentiationhttp://en.wikipedia.org/wiki/Multiplicationhttp://en.wikipedia.org/wiki/Integerhttp://en.wikipedia.org/wiki/Exponentiation
  • 7/30/2019 lab manual PL.doc

    71/82

    nn=(POLY*)malloc(sizeof(POLY));

    if(nn==NULL)

    {printf(\n insufficient memory ! );

    exit(0);

    }return(nn);

    }//end createnewnode

    /*Function Name- create

    Function Use- to create no of node

    */POLY* create()

    {

    POLY *hn=NULL,*nn=NULL,*cn=NULL;int i,n;

    printf(\n Enter how many terms- );

    scanf(%d,&n);

    for(i=0;ic,&nn->e);

    if(hn==NULL)

    {hn=nn;cn=nn;

    }

    else{

    cn->link=nn;

    cn=nn;}

    nn->link=hn;

    }

    return(hn);}//end createpoly

    C function for Display:

  • 7/30/2019 lab manual PL.doc

    72/82

    void display(POLY *hn)

    {

    POLY *cn=NULL;cn=hn;

    printf(\n entered polynomial is );

    do{

    printf( %dx^%d ,cn->c,cn->e);

    if(cn->link != hn){

    printf( + );

    }

    cn=cn->link;}

    while(cn!=hn);

    }//end display

    C function for Evaluation:

    void evaluate(POLY *hn)

    {POLY *cn=hn;

    int x ,sum =0;

    printf(\n Enter value of x );scanf(%d,&x);

    do

    {

    sum=sum+(cn->c*pow(x,cn->e));cn=cn->link;

    }

    while(cn!=hn);printf(\n Evaluation is- %d,sum);

    }//end evaluate

    C function for Addition:

    /*

    Function Name- sameexpo

    Function Use-add co if ea are same

    -*/

    POLY* sameexpo(int c,int e,POLY *h3)

    {

    POLY *cn=NULL;

    cn=h3;if(h3==NULL)

  • 7/30/2019 lab manual PL.doc

    73/82

    {

    return(NULL);

    }

    do

    {if(e==cn->e)

    {cn->c=c+cn->c;

    return(cn);

    }cn=cn->link;

    }while(cn!=h3);

    return(NULL);

    }//end sameexpo

    /*

    Function Name- addlastnade

    Function Use- store addition*/

    POLY* addlastnode(POLY *nn,POLY *h3,POLY *last)

    {if(h3==NULL)

    {

    h3=nn;

    }else

    {

    last->link=nn;

    }nn->link=h3;

    return(h3);

    }//end addlast

    /*Function Name addpoly

    Function Use for addition of 2 polynomial

    */POLY *addpoly(POLY *h1, POLY *h2)

    {

    POLY *p=NULL,*q=NULL,*nn=NULL;

  • 7/30/2019 lab manual PL.doc

    74/82

    int c,e;

    POLY *h3=NULL;

    POLY *last=NULL;p=h1;

    q=h2;

    do{

    nn=createnewnode();

    h3=addlastnode(nn,h3,last);last=nn;

    if(p->e==q->e)

    {

    nn->c=p->c+q->c;nn->e=p->e;

    p=p->link;

    q=q->link;

    }else if(p->e > q->e)

    {nn->c=p->c;

    nn->e=p->e;

    p=p->link;}

    else

    {

    nn->c=q->c;nn->e=q->e;

    q=q->link;

    }} while(p->link!=h1 && q->link!=h2);

    //end while

    while(p!=h1)

    { nn= createnewnode();h3=addlastnode(nn,h3,last);

    last=nn;

    nn->c=p->c;

    nn->e=p->e;p=p->link;

    }

    while(q!=h2){

    nn=createnewnode();

    h3=addlastnode(nn,h3,last);

  • 7/30/2019 lab manual PL.doc

    75/82

    last=nn;

    nn->c=q->c;

    nn->e=q->e;q=q->link;

    }

    return(h3);}//end add poly

    C function for Multiplication:

    POLY* multipoly(POLY *h1,POLY *h2)

    {POLY *p=NULL,*q=NULL,*nn=NULL;

    int c=0,e=0;

    POLY *h3=NULL;

    POLY *last=NULL;

    p=h1;q=h2;

    do{

    do

    {c=p->c*q->c;

    e=p->e+q->e;

    nn=sameexpo(c,e,h3);

    if(nn!=NULL){

    nn->c=nn->c +c;}else

    {

    nn=createnewnode();h3=addlastnode(nn,h3,last);

    last=nn;

    nn->c=c;nn->e=e;

    }

    q=q->link;

    } while(q!=h2);p=p->link;

    } while(p!=h1);

    return(h3);}//end multipoly

  • 7/30/2019 lab manual PL.doc

    76/82

    Conclusion: Successfully performed Polynomial using Circular Linked List and different

    operations.

    Assignment No.: 14

    Title: Implementation Of Stack As An ADT using Array

    Problem Statement: Implement Stack as an ADT using Array. Use this ADT to perform

    expression conversion and evaluation (infix to postfix, infix to prefix, prefix to infix, prefix to

    postfix, postfix to infix and postfix to prefix)..

    Aim: To understand concept of stack, evaluation of prefix, postfix and infix and conversion of

    infix to postfix, infix to prefix, prefix to infix, prefix to postfix, postfix to infix and postfix to

    prefix.

    Input: Enter the string.

    Output: Display the different conversion of the string.

    Theory:

    Definition:

    Stack Operations:

    Infix Expression :

    Any expression in the standard form like 2*3-4/5 is an Infix(Inorder) expression.

    Postfix Expression :

    The Postfix(Postorder) form of the above expression is 23*45/-.

  • 7/30/2019 lab manual PL.doc

    77/82

    Infix to Postfix Conversion :

    In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is

    abc*+. The algorithm for the conversion is as follows :

    Scan the Infix string from left to right. Initialise an empty stack.

    If the scannned character is an operand, add it to the Postfix string. If the scanned

    character is an operator and if the stack is empty Push the character tostack. If the scanned character is an Operand and the stack is not empty, compare the

    precedence of the character with the element on top of the stack(topStack).

    If topStack has higher precedence over the scanned character Popthe stackelse Push the scanned character to stack. Repeat this step as long as stack is not

    empty and topStack has precedence over the character.

    Repeat this step till all the characters are scanned.

    (After all characters are scanned, we have to add any character that the stackmay have to

    the Postfix string.) If stack is not empty add topStack to Postfix stringand Pop the stack.Repeat this step as long as stack is not empty.

    Return the Postfix string.

    Function:

    C function for Evaluation of Postfix Expression:

    void evalpostfix()

    {

    char postfix[100];

    node *top;int i,x,result,ans,op1,op2;

    printf(\nEnter a String);

    scanf(%s,postfix);for(i=0;postfix[i]!=\0;i++)

    {

    if(isalpha(postfix[i]))

    {printf(\nEnter a Value);

    scanf(%d,&x);

    push(&top,x);}

    else

    {

  • 7/30/2019 lab manual PL.doc

    78/82

    op2=pop(&top);

    op1=pop(&top);

    result=evaluate(postfix[i],op1,op2);push(&top,result);

    }

    }

    ans=pop(&top);printf(Evaluation of Postfix Expression is:%d,ans);

    }

    C function for Infix To Postfix Conversion:

    void CITP(char infix[100],char postfix[100]){

    int j=0,i;

    node *top;

    char x;for(i=0;infix[i]!=\0;i++)

    {

    x=infix[i];if(isalpha(x))

    {

    postfix[j]=x;j++;

    }

    else if(x==(){push(&top,();

    }

    else if(x==)'){

    while((x=pop(&top))!=()

    {postfix[j]=x;

    j++;

    }

    }else

    {

    while(priority(top->data)>=priority(x)){

    postfix[j]=pop(&top);

    j++;

  • 7/30/2019 lab manual PL.doc

    79/82

    }

    push(&top,x);

    }

    }

    postfix[j]=\0;printf(\n);

    for(j=0;postfix[j]!=\0;j++){

    printf(%c,postfix[j]);

    }

    }int priority(char x)

    {

    if(x==()

    {return 0;

    }

    if(x==+'|| x==-'){

    return 1;

    }if(x==*'|| x==/')

    {

    return 2;}if(x==^')

    {

    return 3;}

    return 0;

    }

    C function for Infix To Prefix Conversion:

    void CITPrefix(char Infix[],char prefix[])

    {int i;//,//l=0;

  • 7/30/2019 lab manual PL.doc

    80/82

    //char rev[10];

    strrev(Infix);

    printf(Reversed String is:%s,Infix);

    for(i=0;Infix[i]!=\0;i++)

    {if(Infix[i]==()

    {Infix[i]=)';

    }

    else if(Infix[i]==)'){

    Infix[i]=(;

    }}

    Conclusion: Successfully evaluate the expression and convert in type different conversion.

    Assignment No.: 15

    Title: Implementation Of Circular Queue Using Linked List

    Problem Statement: Represent Circular Queue using Linked List and write a program to

    perform operations like Insert, Delete, Finding front and rear element.

    Aim: To Learn the concept of Circular Queue using Linked List and different operation to

    perform on it.

    Input: Enter the data on the node.

    Output: Display the nodes in Circular Queue using Linked List and perform different operation.

    Theory:

    Definition:

    A linked list is a data structure consisting of a group of nodes which together represent a

    sequence. Under the simplest form, each node is composed of a datum and a reference (in otherwords, a link) to the next node in the sequence; more complex variants add additional links. This

    structure allows for efficient insertion or removal of elements from any position in the sequence.

  • 7/30/2019 lab manual PL.doc

    81/82

    A Circular linked list is similar to the simple singly linked list except last node is connected to

    the first node i.e last node is linked to the first node and forms a circle so it is been called as

    circular linked list.

    Function:

    C function for Enqueue:

    void enqueue(node **R,int x)

    {

    node *p;p = (node *)malloc(sizeof(node));

    p->data = x;

    if(empty(*R))

    {

    p->next = p;*R = p;

    }else

    {

    p->next = (*R)->next;(*R)->next = p;

    (*R) = p;

    }

    }

    C function for Dqueue:

    int dequeue(node **R)

    {

    int x;node *p;

    p=(*R)->next;

    x=p->data;if(p->next == p)

    {

    *R = NULL;free(p);

    return(x);

    }(*R)->next = p->next;

    free(p);

  • 7/30/2019 lab manual PL.doc

    82/82

    return(x);

    }

    Conclusion:Successfully performed Circular Queue using Linked List and its different

    operation.