Data Struct File

Embed Size (px)

Citation preview

  • 8/3/2019 Data Struct File

    1/30

    DATA STRUCTURES USING C LAB

    SUBMITTED BY:

    \

    INDEX

  • 8/3/2019 Data Struct File

    2/30

    S No. Program Remarks/Sign

    1.

    2.

    3.

    4.

    5.

    6.

    7.

    WAP to implement operations on singledimensional arrays.

    (i) Insert an element in an array in a

    particular position.(ii) Delete a particular element from the

    array.(iii) Finding the largest element of the array.(iv) Searching for a particular element and

    displaying its position in the array.(v) Adding diagonal elements of an array.

    WAP to implement operations on two dimensionalarrays.

    (i) Implementing Matrix Addition

    (ii) Implementing Matrix Multiplication(iii) Finding the Transpose of a Matrix(iv) Adding all the diagonal elements of the

    Matrix

    WAP to implement operations on stacks usingarrays.

    (i) PUSH(ii) POP

    WAP to implement operations on queues usingarrays.

    (i) QINSERT (ii) QDELETE

    WAP to implement various Searching mechanisms.(i) Linear Search(ii) Binary Search

    WAP to implement various sorting mechanisms:(i) Bubble Sort (ii) Insertion Sort(iii) Selection Sort

    WAP to implement following operations on linkedlists:

    (i) Insertion at the beginning of the list.(ii) Count the nodes in the list.(iii) Deleting from the beginning of the list.

  • 8/3/2019 Data Struct File

    3/30

    (iv) Deleting from the end of the list.(v) Insertion at the end of the list.(vi) Searching for a node in a list.

  • 8/3/2019 Data Struct File

    4/30

    1. Write a program to implement operations on single dimensionalarrays. (i) Insert an element in an array in a particular position.(ii) Delete a particular element from the array.(iii) Finding the largest element of the array.

    (iv) Searching for a element and displaying its position in thearray.(v) Adding diagonal elements of an array.

    #include#includevoid main(){int array[50],i,j,k,n,item,option,large;clrscr();printf("Enter number of elements in array ");

    scanf("%d",&n);printf("\nEnter elements of array ");for(i=1;i=k;j--)array[j+1]=array[j];array[k]=item;n=n+1;printf("Displaying New Array: ");for (i=1;i

  • 8/3/2019 Data Struct File

    5/30

    if (array[i]==item)j=i;for (k=j;k

  • 8/3/2019 Data Struct File

    6/30

    Enter New Element 6Enter Position to Insert New Element 3Displaying New Array: 1 2 6 3 4 5

    (v)adding the diagonal elements of matrix#include#includevoid main(){int m1[10][10],r,c,i,j,sum=0;clrscr();printf("enter the no of rows of matrices\n");scanf("%d",&r);printf("enter the no of columns of matrices\n");scanf("%d",&c);printf("enter the matrix\n");

    for(i=0;i

  • 8/3/2019 Data Struct File

    7/30

    2. WAP to implement operations on two dimensional arrays.(i) Implementing Matrix Addition(ii) Implementing Matrix Multiplication(iii) Finding the Transpose of a Matrix(iv) Adding all the diagonal elements of the Matrix

    #include#includevoid main(){int a[10][10],b[10][10],c[10][10],m,n,p,i,j,k,option,sode=0;clrscr();printf("Choose An Option:\n1.Add Two Matrices \n2.Multiply Two Matrices \n3.FindTranspose Of A Matrix \n4.Add Diagonal Elements Of A Matrix\n");scanf("%d",&option);switch (option)

    {case 1:{/* Matrix Addition */printf("\nEnter no. of rows and columns in matrices A & B: ");scanf("%d%d",&m,&n);printf("\nEnter elements of matrix A\n");for (i=1;i

  • 8/3/2019 Data Struct File

    8/30

    scanf("%d%d",&p,&n);printf("\nEnter elements of matrix A:\n");for (i=1;i

  • 8/3/2019 Data Struct File

    9/30

    /* SUM OF DIAGONAL ELEMENTS */printf("\nEnter no. of rows and columns in matrix A: ");scanf("%d%d",&m,&n);printf("\nEnter elements of matrix A:\n");for (i=1;i

  • 8/3/2019 Data Struct File

    10/30

    3. WAP to implement operations on stacks using arrays.(iii) PUSH(iv) POP

    #include#include#includevoid main(){int stack[50],top,maxstk,i,n,choice,item;clrscr();printf("Enter maximum no. of elements in stack: ");scanf("%d",&maxstk);printf("\nEnter no. of elements originally in stack: ");scanf("%d",&n);if (n>maxstk)

    {printf("\nOverflow.");exit;}printf("\nEnter stack elements: ");for (i=1;i

  • 8/3/2019 Data Struct File

    11/30

    top=top-1;printf("\nDisplaying Modified Stack: ");for(i=1;i

  • 8/3/2019 Data Struct File

    12/30

    4. WAP to implement operations on queues using arrays.(iii) QINSERT(iv) QDELETE

    #include#include#includevoid main(){int q[50],front,rear,i,n,choice,item;clrscr();printf("Enter maximum no. of elements in queue: ");scanf("%d",&n);front=0;rear=0;for (i=1;i

  • 8/3/2019 Data Struct File

    13/30

    if (front==rear)front=rear=0;else if (front==n)front=1;elsefront=front+1;

    }printf("\nDisplaying Queue: ");for(i=1;i

  • 8/3/2019 Data Struct File

    14/30

    5. WAP to implement various Searching mechanisms.(i) Linear Search(ii) Binary Search

    LINEAR SEARCH

    #include#includevoid main(){int a[50],i,n,item,loc=0;clrscr();printf("Enter number of elements in array: ");scanf("%d",&n);printf("\nEnter array elements: ");for (i=1;i

  • 8/3/2019 Data Struct File

    15/30

    BINARY SEARCH

    #include#includevoid main(){

    int a[50],i,n,item,mid,beg,end,loc=0;clrscr();printf("Enter no. of elements in array: ");scanf("%d",&n);printf("\nEnter array elements: ");for(i=1;i

  • 8/3/2019 Data Struct File

    16/30

    6. WAP to implement various Sorting mechanisms.(i) Bubble Sort (ii) Insertion Sort(iii) Selection Sort

    BUBBLE SORT

    #include#includevoid bubble(int arr[], int size);void main(){int a[50],i,n;clrscr();printf("Enter number of elemens in array ");scanf("%d",&n);

    printf("\nEnter array elements : \n");for (i=1;i

  • 8/3/2019 Data Struct File

    17/30

    Enter number of elements in array 10Enter array elements:33 66 44 22 55 11 77 99 88 50

    Original array is:33 66 44 22 55 11 77 99 88 50

    Array after bubble sort is:11 22 33 44 50 55 66 77 88 99

    INSERTION SORT

    #include#include#includevoid main(){

    int ar[50],n,t,i,j;clrscr();printf("enter the no of elements");scanf("%d",&n);printf("enter array elements");for(i=0;i

  • 8/3/2019 Data Struct File

    18/30

    **********OUTPUT*********

    enter the no of elements 4enter array elements 68

    25array after paas 1 is2865array after pass 2 is2568array after pass 3 is2568array after pass 4 is2568the sorted array is shown as 2568

    SELECTION SORT

    #include#includevoid main(){int ar[50],n,small,t,pos,i,j;clrscr();printf("enter the no of elements");scanf("%d",&n);printf("enter array elements");for(i=0;i

  • 8/3/2019 Data Struct File

    19/30

    printf("\narray after pass %d is\n",i+1);for(j=0;j

  • 8/3/2019 Data Struct File

    20/30

    (iii) Deleting from the beginning of the list.(iv) Deleting from the end of the list.(v) Insertion at the end of the list.(vi) Searching for a node in a list.

    /*insertion at beginning of list*/

    #include#includestruct node{int info;node *next;}*start,*newptr,*save,*ptr;void display(node*);

    void main(){start=NULL;int inf;char ch='y';while(ch=='y'){clrscr();printf("enter information of node\n");scanf("%d",&inf);printf("creating new node");ptr=new node;

    ptr->info=inf;ptr->next=NULL;if(ptr!=NULL){printf("\nnew node created succesfully\n.press enter to continue");getch();}else{printf("cannot create new node");getch();}

    printf("\nnow inserting this node at beg of list\n press enter to continue\n");getch();if(start==NULL)start=ptr;else{save=start;start=ptr;ptr->next=save;

  • 8/3/2019 Data Struct File

    21/30

    }printf("now the list is\n");display(start);printf("\nenter y to enter more nodes and n to exit\n");scanf("%s",&ch);getch();

    }}void display(node *np){while(np!=NULL){printf("%d ",np->info);np=np->next;}printf("\n");}

    ********OUTPUT********

    enter information of node7creating new nodenew node created succesfyllypress enter to continuenow inserting this node at beg of listpress enter to continuenow the list is7enter y to enter more nodes and n to exityenter information of node9creating new nodenew node created succesfyllypress enter to continuenow inserting this node at beg of listpress enter to continuenow the list is9 7

    /*counting the nodes in the list*/

    #include#includestruct node{

  • 8/3/2019 Data Struct File

    22/30

    int info;node *next;}*start,*newptr,*save,*ptr,*rear;void display(node*);void main(){

    start=rear=NULL;int inf;char ch='y';while(ch=='y'){clrscr();printf("enter information of node\n");scanf("%d",&inf);ptr=new node;ptr->info=inf;ptr->next=NULL;if(ptr==NULL)

    {printf("cannot create new node");getch();}if(start==NULL){start=ptr;rear=ptr;}else{rear->next=ptr;rear=ptr;}printf("\nenter y to enter more nodes and n to exit\n");scanf("%s",&ch);}printf("now the list is\n");display(start);getch();}void display(node *np){int n=0;while(np!=NULL){printf("%d ",np->info);np=np->next;n++;}printf("\n no of nodes are%d",n);}

  • 8/3/2019 Data Struct File

    23/30

    *******OUTPUT*******

    enter information of node5enter y to enter more nodes and n to exit

    yenter information of node6enter y to enter more nodes and n to exit7enter information of node9enter y to enter more nodes and n to exitnnow the list is5 6 7 9no of nodes are 4

    /*Deleting from the beginning of the list*/

    #include#includestruct node{int info;node *next;}*start,*newptr,*save,*ptr,*rear;void display(node*);void main(){start=rear=NULL;int inf;char ch='y';while(ch=='y'){clrscr();printf("enter information of node\n");scanf("%d",&inf);ptr=new node;ptr->info=inf;ptr->next=NULL;if(ptr==NULL){printf("cannot create new node");getch();}if(start==NULL){start=ptr;

  • 8/3/2019 Data Struct File

    24/30

    rear=ptr;}else{rear->next=ptr;rear=ptr;

    }printf("\nenter y to enter more nodes and n to exit\n");scanf("%s",&ch);}clrscr();do{printf("now the list is\n");display(start);getch();printf("want to delete first node y/n");scanf("%s",&ch);

    if(ch=='y'){if(start==NULL)printf("underflow");else{ptr=start;start=start->next;delete ptr;}}}while(ch=='y');}void display(node *np){while(np!=NULL){printf("%d ",np->info);np=np->next;}printf("\n");}

    *********OUTPUT**********

    enter information of node5enter y to enter more nodes and n to exityenter information of node6

  • 8/3/2019 Data Struct File

    25/30

    enter y to enter more nodes and n to exit7enter information of node9enter y to enter more nodes and n to exitn

    now the list is5 6 7 9want to delete first node y/n ynow the list is6 7 9want to delete first node y/n n

    /*Deleting from the end of the list*/

    #include#includestruct node

    {int info;node *next;}*start,*newptr,*save,*ptr,*rear;void display(node*);void main(){start=rear=NULL;int inf;char ch='y';while(ch=='y'){clrscr();printf("enter information of node\n");scanf("%d",&inf);ptr=new node;ptr->info=inf;ptr->next=NULL;if(ptr==NULL){printf("cannot create new node");getch();}if(start==NULL){start=ptr;rear=ptr;}else{rear->next=ptr;rear=ptr;

  • 8/3/2019 Data Struct File

    26/30

    }printf("\nenter y to enter more nodes and n to exit\n");scanf("%s",&ch);}clrscr();do

    {printf("now the list is\n");display(start);getch();printf("want to delete last node y/n");scanf("%s",&ch);if(ch=='y'){if(start==NULL)printf("underflow");else{

    ptr=rear;rear=rear->next;delete ptr;}}}while(ch=='y');}void display(node *np){while(np!=NULL){printf("%d ",np->info);np=np->next;}printf("\n");}

    ********OUTPUT*********

    enter information of node5enter y to enter more nodes and n to exityenter information of node6enter y to enter more nodes and n to exit7enter information of node

  • 8/3/2019 Data Struct File

    27/30

    9enter y to enter more nodes and n to exitnnow the list is5 6 7 9want to delete last node y/n y

    now the list is5 6 7want to delete last node y/n n

    /*Insertion at the end of the list*/

    #include#includestruct node{int info;

    node *next;}*start,*newptr,*save,*ptr,*rear;void display(node*);void main(){start=rear=NULL;int inf;char ch='y';while(ch=='y'){clrscr();printf("enter information of node\n");scanf("%d",&inf);printf("creating new node");ptr=new node;ptr->info=inf;ptr->next=NULL;if(ptr!=NULL){printf("\nnew node created succesfully\n.press enter to continue");getch();}else{printf("cannot create new node");getch();}printf("\nnow inserting this node at end of list\n press enter to continue\n");getch();if(start==NULL){start=ptr;

  • 8/3/2019 Data Struct File

    28/30

    rear=ptr;}else{rear->next=ptr;rear=ptr;

    }printf("now the list is\n");display(start);printf("\nenter y to enter more nodes and n to exit\n");scanf("%s",&ch);getch();}}void display(node *np){while(np!=NULL){

    printf("%d ",np->info);np=np->next;}printf("\n");}

    *********OUTPUT*********enter information of node7creating new nodenew node created succesfyllypress enter to continuenow inserting this node at end of listpress enter to continuenow the list is7enter y to enter more nodes and n to exityenter information of node9creating new nodenew node created succesfyllypress enter to continuenow inserting this node at end of listpress enter to continuenow the list is7 9

    /*Searching for a node in the list*/

  • 8/3/2019 Data Struct File

    29/30

    #include#includestruct node{int info;

    node *next;}*start,*newptr,*save,*ptr,*rear;void display(node*);void main(){start=rear=NULL;int inf;char ch='y';while(ch=='y'){clrscr();printf("enter information of node\n");

    scanf("%d",&inf);ptr=new node;ptr->info=inf;ptr->next=NULL;if(ptr==NULL){printf("cannot create new node");getch();}if(start==NULL){start=ptr;rear=ptr;}else{rear->next=ptr;rear=ptr;}printf("\nenter y to enter more nodes and n to exit\n");scanf("%s",&ch);}printf("now the list is\n");display(start);getch();}void display(node *np){int item,n=0;while(np!=NULL){printf("%d ",np->info);

  • 8/3/2019 Data Struct File

    30/30

    np=np->next;n++;}printf("enter the item to be searched");scanf("%d",&item);while(np!=NULL)

    {if(item==np->info)printf("the item is at loc%d",n);else{printf("the item is not found");}}}

    ***********OUTPUT************

    enter information of node5enter y to enter more nodes and n to exityenter information of node6enter y to enter more nodes and n to exit7enter information of node9enter y to enter more nodes and n to exitnnow the list is5 6 7 9enter the item to be searched 6item is found at loc 2