+ All Categories
Home > Documents > Data Structures Lab Manual JNTUK r10 CSE

Data Structures Lab Manual JNTUK r10 CSE

Date post: 24-Nov-2015
Category:
Upload: rsvv-prasad-rao
View: 120 times
Download: 8 times
Share this document with a friend
Description:
DS lab manual
Popular Tags:
29
DataStructuresLabManual for II B.Tech. CSE and IT    I Sem JNTUWORLD                (JNTU, Kakinada) www.jntuworld.com www.jntuworld.com
Transcript
  • Data Structures Lab Manual

    for

    IIB.Tech.CSEandITISem

    JNTUWORLD

    (JNTU,Kakinada)

    www.jntuworld.com

    www.jntuworld.com

  • Exercise1Question: WriterecursiveprogrammewhichcomputesthenthFibonaccinumber,forappropriatevaluesofn.Analyzebehavioroftheprogramme.Obtainthefrequencycountofthestatementforvariousvaluesofn.

    #include int fibonacci(int n)

    { if(n==1)

    return 0; else if(n==2)

    return 1; else

    return fibonacci(n-1)+fibonacci(n-2); }

    int main() { int n,fib; printf("\n\n **************Program to find nth fibonacci number in the series************\n\n"); printf("Enter number of value in the series : "); scanf("%d",&n); fib=fibonacci(n); printf("\n\n\n\n\t\t %d th Value in the series is %d\n\n\n",n,fib); }

    output:

    www.jntuworld.com

    www.jntuworld.com

  • Exercise2Question: Writerecursiveprogrammewhichcomputesthefactorialofagivennumber.

    #include int Factorial(int n)

    { if(n==0)

    return 1; else

    return n*Factorial(n-1); }

    int main() { int n,fact; printf("\n\n Enter the number in the sequence : "); scanf("%d",&n); fact=Factorial(n); printf("\n\n\n Factorial value in the series is %d\n\n\n\n",fact); }

    output:

    www.jntuworld.com

    www.jntuworld.com

  • EXERCISE4Question1:Writecprogramtoimplementbubblesort,tosortagivenlistofintegersinascendingorder.

    #include void BubbleSort(int [], int); void Print(int[],int,int); void BubbleSort(int list[], int size)

    { int i,j,temp; for(i=0;i

  • for(i=0;i
  • Question2:Program to implement insertion sort to sort a given list of integers in ascending order.

    /* WRITE C PROGRAM TO IMPLEMENT INSERTION SORT, TO SORT A GIVEN LIST OF INTEGERS IN ASCENDING ORDER*/ #include void InsertionSort(int [], int); void Print(int[],int,int); void InsertionSort(int list[], int size)

    { int i,j,temp; for(i=1;i0&&temp

  • printf("\n\nList before sorting: "); Print(list,size,0); /* Sort the elements using Bubblesort */ InsertionSort(list,size); /* print the elements */ printf("\n\n Sorted list : "); Print(list,size,size-1); printf("\n\n\n"); }

    output:

    www.jntuworld.com

    www.jntuworld.com

  • Question2:AprogramtoimplementINSERTIONSORTtosortalistofnumbers.

    /* program to implement Selection Sort for sorting elements in ascending order*/ #include void SelectionSort(int [], int); void Print(int[],int,int); void SelectionSort(int list[], int size)

    { int i,j,temp; int largest; /* Find smallest element in the list and put in its appropriate position */ for(j=size-1;j>0;j--)

    { largest=j; for(i=0;i

  • for(i=0;i
  • EXERCISE5Question3:WriteaCprogramthatimplementmergesort,tosortagivenlistofintegersinascendingorder.Program: /* PROGRAM TO SORT A LIST OF NUMBERS USING MERGE SORT */

    #include void Merge(int list[],int temporary[],int left,int center,int rightEnd); void MergeSort(int list[],int temporary[],int left, int right); void Print(int list[],int size,int mark)

    { int i; for(i=0;i

  • tmppos++; left++; }

    else { temporary[tmppos]=list[right]; tmppos++; right++; }

    } /* when right list is exhausted */ while(left

  • printf("\n\n Sorted list : "); Print(list,size,0); printf("\n\n\n"); }

    OUTPUT:

    www.jntuworld.com

    www.jntuworld.com

  • EXERCISE6Question1: Write C programs that implement stack using arraysProgram:

    /* Write C program that implement stack using array */ #include //#include

    #define STACKSIZE 20

    /* STACK DATASTRUCTURE DEFINITION */

    typedef struct Stack { int elements[STACKSIZE]; int top; }Stack;

    /* STACK OPERATIONS */ void push(Stack *s, int element)

    { if(s->top==STACKSIZE) /* OVERFLOW CONDITION */

    printf("\n\n STACK OVERFLOW "); else

    s->elements[s->top++]=element; }

    int pop(Stack *s) { if(s->top==0)

    { printf("\n\n EMPTY STACK "); return -1; }

    else { --s->top; return s->elements[s->top]; }

    } int peek(Stack *s)

    { if(s->top==0)

    { printf("\n\n\n EMPTY STACK "); return -1;

    www.jntuworld.com

    www.jntuworld.com

  • } return s->elements[s->top-1]; }

    void print(Stack *s) { int i; printf("\n\n [ "); for(i=0;itop;i++)

    printf(" %d ",s->elements[i]); printf(" ]"); }

    int main() { Stack s1; int opt,x; s1.top=0; while(1)

    { printf("\n\n\n\t\t STACK OPERATIONS \n\n\n"); printf("\n\n 1. Push "); printf("\n\n 2. Pop "); printf("\n\n 3. Print Elements "); printf("\n\n 4. Peek "); printf("\n\n 5. Exit "); printf("\n\n Select the operation : ");scanf("%d",&opt); switch(opt)

    { case 1: printf("\n\n Enter the element to insert in stack :");

    scanf("%d",&x); push(&s1,x); break; case 2: printf("\n\n\n Popped element : %d",pop(&s1)); break; case 3: printf("\n\n Elements in stack : "); print(&s1); break; case 4: printf("\n\n Top element in the stack : %d",peek(&s1)); break; case 5: return;//exit(7);

    } }

    return 0; }

    www.jntuworld.com

    www.jntuworld.com

  • Program 2: Write C programs that implement stack using linked listProgram:

    /* LINKED LIST IMPLEMENTATION OF STACK */

    #include //#include #include

    /* Element definition */ typedef struct Element

    { int data; struct Element *link; }Element;

    /* Stack Defintion */ typedef struct Stack

    { Element *top; }Stack;

    Element* createNode(int data) { Element *node; node=malloc(sizeof(Element)); node->data=data; node->link=NULL; return node; }

    void push(Stack *s,int data) { Element *node; node=createNode(data); node->link=NULL; if(s->top==NULL)

    { s->top=node; }

    else { node->link=s->top; s->top=node; }

    }

    www.jntuworld.com

    www.jntuworld.com

  • void pop(Stack *s) { Element *temp; int x; if(s->top==NULL)

    printf("\n\n EMPTY STACK "); else

    { temp=s->top; s->top=s->top->link; temp->link=NULL; x=temp->data; printf("\n\n Deleted Element :%d",x); }

    } int peek(Stack *s)

    { return s->top->data; }

    void printStack(Stack *s) { Element *traverse=s->top; printf("\n\n STACK : [ "); while(traverse!=NULL)

    { printf(" %d ",traverse->data); traverse=traverse->link; }

    printf(" ] "); }

    void main() { Stack s; int opt,x; s.top=NULL; while(1)

    { printf("\n\n\n ============ STACK OPERATIONS

    =============\n\n\n"); printf("\n\n 1. Push "); printf("\n\n 2. Pop"); printf("\n\n 3. Print Stack "); printf("\n\n 4. Peek "); printf("\n\n 5. Exit"); printf("\n\n Select the operation : ");scanf("%d",&opt); switch(opt)

    www.jntuworld.com

    www.jntuworld.com

  • { case 1:printf("\n\n Enter element to insert ");

    scanf("%d",&x); push(&s,x); break;

    case 2: pop(&s); break; /* popping */

    case 3: printStack(&s); break;

    case 4: printf("\n\n Element of top of the stack : %d",peek(&s));

    break; case 5:return; }/* end of switch */

    }/* end of while loop */ }/* END OF THE PROGRAM */

    OUTPUT:

    www.jntuworld.com

    www.jntuworld.com

  • www.jntuworld.com

    www.jntuworld.com

  • EXERCISE7Question2:WritecprogramsthatimplementQueueusingarray

    Program/* Array implementation of Queues */ #include #define MAXQUEUE 20 typedef struct Queue

    { int elements[MAXQUEUE]; int front, rear; }Queue;

    void insert(Queue *q,int data) /* INSERTS AN ELEMENT INTO QUEUE */

    { if(q->rear==MAXQUEUE)

    printf("\n\n Queue overflow "); else

    { q->elements[q->rear]=data; q->rear++; }

    } int removeElement(Queue *q)

    { int x=0; if(q->front==q->rear)

    printf("\n\n Empty Queue "); else

    { x=q->elements[q->front]; q->front++; }

    return x; }

    void print(Queue q) { int i; printf("\n\n\n QUEUE : [ "); for(i=q.front;i

  • /* Initialize queue */ Queue q; int opt,x; q.front=0; q.rear=0; while(1)

    { printf("\n\n\n\t\t QUEUE OPERATIONS \n\n\n"); printf("\n\n 1. Insert element "); printf("\n\n 2. Remove element "); printf("\n\n 3. Print Elements "); printf("\n\n 4. Exit "); printf("\n\n Select the operation : ");scanf("%d",&opt); switch(opt)

    { case 1: printf("\n\n Enter the element to insert in Queue :");

    scanf("%d",&x); insert(&q,x); break; case 2: printf("\n\n\n Deleted element : %d",removeElement(&q)); break; case 3: printf("\n\n Elements in Queue: "); print(q); break; case 4: return;//exit(7);

    } }

    return 1; }

    Question3:WriteCprogramsthatimplementQueueusinglinkedlists.Program:

    /* WRITE A PROGRAM TO IMPLEMENT QUEUE USING LINKED LIST */ #include #include

    /* Element definition */ typedef struct Element

    { int data; struct Element *link; }Element;

    /* Queue Defintion */

    www.jntuworld.com

    www.jntuworld.com

  • typedef struct Queue { Element *front; Element *rear; }Queue;

    Element* createNode(int data) { Element *node; node=malloc(sizeof(Element)); node->data=data; node->link=NULL; return node; }

    void insert(Queue *q,int x) { Element *newnode; newnode=createNode(x); if(newnode==NULL)

    { printf("\n\n INSUFFICIENT MEMORY "); return; }

    if(q->front==NULL && q->rear==NULL) /* INSERT NODE IN EMPTY QUEUE */

    { q->front=newnode; q->rear=newnode; }

    else/* INSERT ELEMENT INTO NON EMPTY QUEUE */

    { q->rear->link=newnode; q->rear=newnode; }

    } void removeElement(Queue *q)

    { Element *temp; if(q->front==NULL)

    { printf("\n\n EMPTY QUEUE \n\n"); return; }

    temp=q->front; q->front=q->front->link;

    www.jntuworld.com

    www.jntuworld.com

  • temp->link=NULL; printf("\n\n Deleted Element : %d ",temp->data); free(temp); if(q->front==NULL) /* When all the elements are deleted */

    q->rear=NULL; }// End of removeElement()

    void print(Queue *q) { Element *traverse; traverse=q->front; printf("\n\n [FRONT] [ "); if(traverse==NULL)

    printf(" EMPTY QUEUE"); while(traverse!=NULL)

    { printf(" %d ",traverse->data); traverse=traverse->link; }

    printf(" ] [REAR ] \n\n\n"); }

    int main() { Queue q; int opt,x; q.front=NULL; q.rear=NULL; while(1)

    { printf("\n\n\n\t\t QUEUE OPERATIONS \n\n\n"); printf("\n\n 1. Insert element "); printf("\n\n 2. Remove element "); printf("\n\n 3. Print Elements "); printf("\n\n 4. Exit "); printf("\n\n Select the operation : ");scanf("%d",&opt); switch(opt)

    { case 1: printf("\n\n Enter the element to insert in Queue :");

    scanf("%d",&x); insert(&q,x); break; case 2: removeElement(&q); break; case 3: printf("\n\n Elements in Queue: "); print(&q); break;

    www.jntuworld.com

    www.jntuworld.com

  • case 4: return;//exit(7); }

    } return 1; }

    OUTPUT:

    www.jntuworld.com

    www.jntuworld.com

  • www.jntuworld.com

    www.jntuworld.com

  • EXERCISE 8

    Write program to implement linked list operations( Creation, Insertion, Deletion, reversing ).Program

    /* IMPLEMENTATION OF LINKED LIST */ #include #include typedef struct ListItem /* node definition */

    { int data; struct ListItem *link; }ListItem;

    typedef struct List { ListItem *root; }List;

    ListItem* createNode(int data) { ListItem *node; node=malloc(sizeof(ListItem)); node->data=data; node->link=NULL; return node; }

    void insertItem(List *l1,int item) { ListItem *temp,*traverse; temp=createNode(item); if(temp==NULL)

    { printf("\n\n Memory error"); return; }

    if(l1->root==NULL) { l1->root=temp; return; }

    else { traverse=l1->root; while(traverse->link!=NULL)

    traverse=traverse->link; traverse->link=temp;

    www.jntuworld.com

    www.jntuworld.com

  • } }

    void insertAt(List *l1,int pos, int item) { ListItem *temp,*traverse; int i=1; temp=createNode(item); if(temp==NULL)

    { printf("\n\n Memory error"); return; }

    if(poslink=l1->root; l1->root=temp; }

    else { traverse=l1->root; while(traverse->link!=NULL && ilink; i++; }

    if(i!=pos-1) { printf("\n\n Invalid position "); return; }

    temp->link=traverse->link; traverse->link=temp; }

    }

    void deleteItem(List *l1) { ListItem *temp,*traverse; if(l1->root==NULL)

    { printf("\n\n EMPTY LIST "); return;

    www.jntuworld.com

    www.jntuworld.com

  • } traverse=l1->root; if(traverse->link==NULL)

    { temp=traverse; printf("\n\n Deleted Item : %d ",temp->data); l1->root=NULL; free(temp); return; }

    while(traverse->link->link!=NULL) traverse=traverse->link;

    temp=traverse->link; traverse->link=NULL; printf("\n\n Deleted Item : %d ",temp->data); free(temp); }

    void display(List *l1) { ListItem *traverse; traverse=l1->root; printf("\n\n ROOT "); while(traverse!=NULL)

    { printf(" %d ",traverse->data); traverse=traverse->link; }

    printf(" END "); }

    void reversePrint(List *l1) { ListItem *traverse=NULL,*temp=NULL; temp=l1->root; while(temp!=traverse)

    { while(temp->link!=traverse)

    temp=temp->link; printf(" %d ",temp->data); traverse=temp; temp=l1->root; }

    } void reverseList(List *l1)

    { ListItem *trav,*temp,*newroot; if(l1->root==NULL || l1->root->link==NULL)

    www.jntuworld.com

    www.jntuworld.com

  • { return; }

    trav=l1->root; while(trav->link!=NULL) /* FIND THE NEW ROOT */

    trav=trav->link; newroot=trav; while(trav!=l1->root)

    { temp=l1->root;

    while(temp->link!=trav) { temp=temp->link; }

    trav->link=temp; trav=temp; }

    trav->link=NULL; l1->root=newroot; //printf(" \n\n\n %d ",newroot->data); }

    void main() { List l1; int opt; int x; int pos; l1.root=NULL; while(1)

    { printf("\n\n\t\t\t LINKED LIST OPERATIONS \n\n"); printf("\n\n\t\t\t 1. Add Item "); printf("\n\n\t\t\t 2. Delete Item "); printf("\n\n\t\t\t 3. Reverse List "); printf("\n\n\t\t\t 4. Show Items "); printf("\n\n\t\t\t 5. Reverse Print "); printf("\n\n\t\t\t 6. Insert Item "); printf("\n\n\t\t\t 7. Exit "); printf("\n\n Select the operation :"); scanf("%d",&opt); switch(opt)

    { case 1:

    printf("\n\n Enter the element to insert into list :");

    www.jntuworld.com

    www.jntuworld.com

  • scanf("%d",&x); insertItem(&l1,x); break;

    case 2: deleteItem(&l1); break;

    case 3:reverseList(&l1); break;

    case 4:display(&l1); break;

    case 5:reversePrint(&l1); break;

    case 6:printf("\n\n Enter the element to insert into list :"); scanf("%d",&x); printf("\n\n Enter the position :"); scanf("%d",&pos); insertAt(&l1,pos,x); break;

    case 7: return;

    } }

    }

    OUTPUT:

    www.jntuworld.com

    www.jntuworld.com


Recommended