exam programs

Upload: yuva-raj

Post on 10-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Exam Programs

    1/29

    AIM:

    To implement Stack using arrays.

    OBJECTIVE:

    To represent Stack using arrays and to perform operations like push, pop.

    HARDWARE REQUIREMENTS:

    PC (Pentium IV)

    SOFTWARE REQUIREMENTS:

    Turbo C Compiler

    PROGRAM:

    #include#include#define stack_size 5void push (int value);void pop();void peek();int size();void view();int stack[stack_size], top=-1;void main(){int x, data, item;clrscr();printf("\n Representation of Stack:");printf("\n1. Push, 2. Pop, 3. Peek, 4. Size, 5.View, 6. Exit");while(1){printf("\n Enter the choice:");scanf("%d", &x);switch(x){case 1:printf("\nEnter the element:");

    scanf("%d", &data);push(data);break;case 2:pop();break;case 3:peek();break;case 4:printf("\n stack size=%d", stack_size);printf("\n Current stack size=%d", size());

    break;

  • 8/8/2019 Exam Programs

    2/29

    case 5:view();break;default:printf("\n End of the program");

    exit(0);}}getch();}int isfull(){extern int stack[], top;if(top==stack_size-1)return(1);elsereturn(0);

    }int isempty()

    {extern int stack[], top;if(top==-1)return(1);elsereturn(0);}void push(int value){extern int stack[], top;if(isfull())printf("\n Stack is full");else{top++;stack[top]=value;}}void pop(){int value;

    extern int stack[], top;if(isempty())printf("\n Stack is empty");else{value=stack[top];printf("\n The popped value is %d", value);top--;}}void peek(){

    int item;extern int stack[], top;

  • 8/8/2019 Exam Programs

    3/29

    if(isempty(1))printf("\n Stack is empty");else{item=stack[top];

    printf("\n The peek of the stack is %d", item);}}int size(){extern int stack[], top;if(isempty())return(0);elsereturn(top+1);}void view()

    {extern int stack[], top;int f;if(isempty())printf("\n Stack is empty");else{printf("\n Content of the stack is .... \n top-->");for(f=top;f>=0;f--){printf("%d-->", stack[f]);}}if(isfull())printf("\n Stack is full");}OUTPUT:

    Representation of Stack:1. Push, 2. Pop, 3. Peek, 4. Size, 5. View, 6. Exit

    Enter the choice: 2Stack is empty

    Enter the choice: 1Enter the element: 10

    Enter the choice: 1Enter the element: 20

    Enter the choice: 1Enter the element: 30

    Enter the choice: 1

    Enter the element: 40

  • 8/8/2019 Exam Programs

    4/29

    Enter the choice: 1Enter the element: 50

    Enter the choice: 1Enter the element: 60

    Stack is full.Enter the choice: 3Peek of the stack is: 50

    Enter the choice: 4Stack size is 5Current stack size 5

    Enter the choice: 5Content of the stack is..

    Top-->50-->40-->30-->20-->10-->

    Stack is full.

    Enter the choice: 2The popped value is 50.

    Enter the choice: 6

    RESULT:The implementation of stack using array program is executed and the

    output is verified.

    IMPLEMENTATION OF QUEUE USING ARRAYS

    PROGRAM:

    #include#include#define qsize 5void enqueue(int value);void dequeue();void peek();int size();

    void view();int queue[qsize], front=-1, rear=-1;void main(){int choice, data, item;clrscr();printf("\n Representation of Linear Queue:");printf("\n 1. Enqueue, 2. Dequeue, 3. Peek, 4. Size, 5. View, 6. Exit");while(1){printf("\n Enter the choice:");scanf("%d", &choice);switch(choice){

  • 8/8/2019 Exam Programs

    5/29

    case 1:printf("\n Enter the element:");scanf("%d", &data);enqueue(data);break;

    case 2:dequeue();break;case 3:peek();break;case 4:printf("\n queue size = %d", qsize);printf("\n Current queue size = %d", size());break;case 5:view();

    break;default:printf("\n End of the programme");exit(0);getch();}}}//getch();int isfull(){extern int queue[], front, rear;if(rear==(qsize-1))return(1);elsereturn(0);}int isempty(){extern int queue[], front, rear;if(front==-1&&rear==-1)return(1);else

    return(0);}void enqueue(int value){extern int queue[], front, rear;if(isfull())printf("\n queue is full");else{if(isempty())front=rear=0;else

    rear=rear+1;queue[rear]=value;

  • 8/8/2019 Exam Programs

    6/29

    }}void dequeue(){int value;

    extern int queue[], front, rear;if (isempty())printf("\n queue is empty");else{value=queue[front];printf("\n The dequeued value is %d", value);}if(front==rear)front=rear=-1;elsefront=front+1;

    }void peek(){int item;extern int queue[], front, rear;if(isempty())printf("\n queue is empty");else{item=queue[front];printf("\n The peek of the queue is %d", item);}}int size(){extern int queue[], front, rear;if(isempty())return(0);elsereturn(rear-front+1);}void view(){

    extern int queue[], front, rear;int f;if(isempty()){printf("\n queue is empty");}else{printf("\n content of the queue is .......\n Front-->");for(f=front;f!=rear+1;f=f+1){printf("%d-->", queue[f]);

    }printf("Rear");

  • 8/8/2019 Exam Programs

    7/29

    }if(isfull())printf("\n queue is full");

    OUTPUT:

    Representation of Linear Queue:1. Enqueue, 2. Dequeue, 3. Peek, 4. Size, 5. View, 6. Exit

    Enter the choice: 2Queue is empty

    Enter the choice: 1Enter the element: 10

    Enter the choice: 1

    Enter the element: 20

    Enter the choice: 1Enter the element: 30

    Enter the choice: 1Enter the element: 40

    Enter the choice: 1Enter the element: 50

    Enter the choice: 1Enter the element: 60

    Queue is full.Enter the choice: 3Peek of the Queue is: 50

    Enter the choice: 4Queue size is 5Current Queue size 5

    Enter the choice: 5

    Content of the Queue is..Front-->50-->40-->30-->20-->10-->RearQueue is full.

    Enter the choice: 2The dequeued value is 50.

    Enter the choice: 6

    IMPLEMENTATION OF STACK USING LINKED LIST

  • 8/8/2019 Exam Programs

    8/29

    PROGRAM:

    #include

    #includetypedef struct stack{int data;struct stack *next;}node;void push(int value);void pop();void display();void peek();void size();int isempty();

    node *get_node();node *top;

    void main(){int x,data,value;top=NULL;clrscr();printf("\n Representation of stack using Linked List:");printf("\n 1.push, 2.pop, 3.peek, 4.size, 5.view, 6.exit");while(1)

    {printf("\n Enter your choice:");scanf("%d", &x);switch(x)

    {case 1:printf("\n Enter the element to push:");scanf("%d", &value);push(value);break;case 2:pop();

    break;case 3:peek();break;case 4:size();break;case 5:display();break;default:printf("\n End of the program");

    exit(0);getch();

  • 8/8/2019 Exam Programs

    9/29

    }}

    }

    void push(int value)

    {extern node *top;node *New;New=get_node();if(New==NULL)

    {printf("\n Memory not created");return;}

    New->data=value;New->next=top;top=New;

    }

    node *get_node(){node *temp;temp=(node*)malloc(sizeof(node));temp->next=NULL;return(temp);}

    void pop(){extern node *top;node *temp;temp=top;if(isempty())printf("\n The stack underflow on pop");else

    {printf("\n The popped value is %d", top->data);top=top->next;temp->next=NULL;free(temp);

    }}

    void peek(){extern node *top;if(isempty())printf("\n The stack is empty");elseprintf("\n The top most element is=%d", top->data);}

    void size(){

  • 8/8/2019 Exam Programs

    10/29

    extern node *top;node *temp;int count=0;temp=top;if(isempty())

    printf("\n The stack is empty");else{while(temp!=NULL)

    {count++;temp=temp->next;}

    printf("\n The current size is=%d", count);}

    }

    void display(){extern node *top;int i;node *temp;temp=top;if(isempty())printf("\n The stack is empty");else

    {printf("\n The stack is:\nTop");while(temp!=NULL)

    {printf("-->%d", temp->data);temp=temp->next;}

    }}

    int isempty(){extern node *top;if(top==NULL)

    return 1;elsereturn 0;}

    OUTPUT:

    Representation of stack using linked list:1.push, 2. pop, 3. peek, 4. size, 5. view, 6. exitEnter the choice: 1Enter the element to push:10

    Enter the choice: 1Enter the element to push:20

  • 8/8/2019 Exam Programs

    11/29

    Enter the choice: 1Enter the element to push:30

    Enter the choice: 2

    The popped value is 30

    Enter the choice: 1Enter the element to push:35

    Enter the choice: 3The top most element is=35

    Enter the choice: 4The current size is=4

    Enter the choice: 5

    The stack is:Top-->35-->20-->10

    Enter the choice: 2The popped value is 35

    Enter the choice: 2The popped value is 20

    Enter the choice: 2The popped value is 10

    Enter the choice: 2The stack underflow on pop

    Enter the choice: 6

    IMPLEMENTATION OF QUEUE USING LINKED LIST

    PROGRAM:

    #include

    #includetypedef struct queue{int data;struct queue *next;}node;void enqueue();void dequeue();void display();void size();void peek();node *get_node();

    node *front,*rear;void main()

  • 8/8/2019 Exam Programs

    12/29

    {int ch,data;front=NULL;rear=NULL;clrscr();

    printf("Linear Queue implementation using Linked List:");printf("\n1.ENQUEUE\n2.DEQUEUE\n3.VIEW\n4.SIZE\n5.PEEK\n6.EXIT");while(1){

    printf("\nEnter your choice...");scanf("%d",&ch);switch(ch){

    case 1:enqueue();break;case 2:dequeue();break;

    case 3:display();break;case 4:size();break;case 5:peek();break;default:printf("\nEnd of program");exit(0);

    }getch();

    }}void enqueue(){extern node *front,*rear;node *New;int val;New=get_node();if(New==NULL){

    printf("\nMemory not created");return;

    }

    printf("\nEnter the element to Enqueue:");scanf("%d",&val);New->data=val;New->next=NULL;if(rear==NULL){

    rear=New;front=New;

    }else{

    rear->next=New;

    rear=New;}

  • 8/8/2019 Exam Programs

    13/29

    }node *get_node(){node *temp;temp=(node*)malloc(sizeof(node));

    temp->next=NULL;return(temp);}void dequeue(){extern node *front,*rear;node *temp;int value;temp=front;if(isempty())printf("\nThe QUEUE is empty");else

    {value=front->data;if(front==rear){

    front=NULL;rear=NULL;

    }else{

    front=front->next;temp->next=NULL;free(temp);

    }printf("\nThe Dequeued Elelement of the QUEUE is =%d",value);

    }}void size(){extern node *front,*rear;int count=0;node *temp;temp=front;if(isempty())

    printf("\nThe QUEUE is empty");else{

    while(temp!=NULL){

    count++;temp=temp->next;

    }printf("\nThe size of the QUEUE is: %d",count);

    }}void peek()

    {extern node *rear,*front;

  • 8/8/2019 Exam Programs

    14/29

    if(isempty())printf("\nThe QUEUE is empty");elseprintf("\nThe Front element of the QUEUE is =%d",front->data);}

    void display(){extern node *front,*rear;int i;node *temp;temp=front;if(isempty())printf("\nThe QUEUE is empty");else{

    printf("\nFRONT-->");while(temp!=NULL)

    {printf("%d-->",temp->data);temp=temp->next;

    }printf("REAR");

    }}int isempty(){extern node *front,*rear;if(front==NULL)return 1;elsereturn 0;}

    OUTPUT:

    Linear Queue implementation using Linked List:1.ENQUEUE2.DEQUEUE3.VIEW4.SIZE

    5.PEEK6.EXITEnter your choice...1Enter the element to Enqueue:25

    Enter your choice...1Enter the element to Enqueue:30

    Enter your choice...1Enter the element to Enqueue:35

    Enter your choice...1

    Enter the element to Enqueue:40

  • 8/8/2019 Exam Programs

    15/29

    Enter your choice...3FRONT-->25-->30-->35-->40-->REAR

    Enter your choice...4The size of the QUEUE is: 4

    Enter your choice...5The Front element of the QUEUE is =25

    Enter your choice...2The Dequeued Elelement of the QUEUE is =25

    Enter your choice...4The size of the QUEUE is: 3

    Enter your choice...3FRONT-->30-->35-->40-->REAR

    Enter your choice...2The Dequeued Elelement of the QUEUE is =30

    Enter your choice...2The Dequeued Elelement of the QUEUE is =35

    Enter your choice...2The Dequeued Elelement of the QUEUE is =40

    Enter your choice...2The QUEUE is empty

    Enter your choice...4The QUEUE is empty

    Enter your choice...6End of program

    IMPLEMENTATION OF SINGLY LINEAR LINKED LIST

    PROGRAM:

    #include#include#define TRUE 1#define FALSE 0typedef struct sll

    {int data;struct sll *next;}node;node *create(node *);node *insert(node *);node *delet(node **);node *search(node *,int);void count(node *);

  • 8/8/2019 Exam Programs

    16/29

    void display(node *);node *get_node();

    void main(){

    int choice,val;node *head;clrscr();printf("\nRepresentation of singly linked list:");

    printf("\n1.create\n2.insert\n3.delete\n4.search\n5.display\n6.count\n7.exit");while(1)

    {printf("\n\nEnter your choice: ");

    scanf("%d",&choice);switch(choice)

    {case 1: head=create(head);

    break;case 2: head=insert(head);break;

    case 3: head=delet(&head);break;

    case 4: printf("Enter the element to search");scanf("%d",&val);search(head,val);break;

    case 5: display(head);break;

    case 6: count(head);break;

    default:printf("\nEnd of program");exit(0);

    }}

    }

    node *create(node *head){node *temp,*New;int val,flag;

    char ans;temp=NULL;flag=TRUE;do

    {printf("\nEnter the element: ");scanf("%d",&val);New=get_node();

    if(New==NULL)printf("\nMemory is not allocated");New->data=val;if(flag)

    {head=New;

  • 8/8/2019 Exam Programs

    17/29

    temp=head;flag=FALSE;}

    else{

    temp->next=New;temp=New;}

    printf("\nDo u want to continue:(y/n)");ans=getche();

    }while((ans=='Y')||(ans=='y'));printf("\n The singly linked list is created");return(head);

    }

    node *insert(node *head){

    node *temp,*New;int val,ch;char ans;temp=head;if(temp==NULL){printf("\n insertion not possible");

    return(0);}New=get_node();if(New==NULL)printf("\n Memory not allocated");printf("\n Enter the element to insert:");scanf("%d",&val);New->data=val;New->next=NULL;printf("\n Where do u want insert:\n");printf("b.Beginning\nm.Middle\nl.Last");printf("\n Enter your choice:");ans=getche();switch(ans)

    {case 'b':New->next=temp;

    head=New;

    printf("\n Inserted at beginning");break;case 'm':printf("\n Enter the element after which u want to insert");

    scanf("%d",&val);temp=search(head,val);if(temp!=NULL)

    {New->next=temp->next;temp->next=New;temp=New;printf("\n Element is inserted");}

    break;case 'l':while(temp->next!=NULL)

  • 8/8/2019 Exam Programs

    18/29

    temp=temp->next;temp->next=New;New->next=NULL;printf("\n Element inserted at last");break;

    default:printf("\n Entered wrong choice");break;}

    return(head);}

    node *delet(node **head){int val;node *temp,*prev;temp=*head;if(temp==NULL)

    {printf("\n The list is empty");

    return(0);}

    printf("\n Enter the value u want to delete");scanf("%d",&val);temp=search(*head,val);if(temp!=NULL)

    {if(temp==*head)

    *head=temp->next;else

    {prev=*head;

    while(prev->next!=temp)prev=prev->next;prev->next=temp->next;}

    free(temp);printf("\n The element is deleted");}

    return(*head);}

    node *search(node *head,int val){node *temp;int found;temp=head;if(temp==NULL)

    {printf("\n The list is empty");

    return(NULL);}

    found=FALSE;

    while(temp!=NULL&&!found){

  • 8/8/2019 Exam Programs

    19/29

    if(temp->data!=val)temp=temp->next;elsefound=TRUE;

    }

    if(found){printf("\n The element is present in the list");return(temp);

    }else

    {printf("\n The element is not present in the list");

    return(NULL);}

    }

    void count(node *head){node *temp;int count=0; temp=head;if(temp==NULL)

    printf("\n The list is empty");else

    {while(temp!=NULL)

    {count++;temp=temp->next;}

    printf("\nThe size of list is= %d",count);}

    }

    void display(node *head){node *temp;temp=head;if(temp==NULL)

    {

    printf("\nThe list is empty");return;}while(temp!=NULL)

    {printf("%d-->",temp->data);

    temp=temp->next;}

    printf("NULL");}

    node *get_node()

    {node *temp;

  • 8/8/2019 Exam Programs

    20/29

    temp=(node*)malloc(sizeof(node));temp->next=NULL;return(temp);

    }OUTPUT:

    Representation of singly linked list:1.create2.insert3.delete4.search5.display6.count7.exit

    Enter your choice: 1Enter the element: 10

    Do u want to continue:(y/n)yEnter the element: 20

    Do u want to continue:(y/n)yEnter the element: 30

    Do u want to continue:(y/n)nThe singly linked list is created

    Enter your choice: 510-->20-->30-->NULL

    Enter your choice: 4Enter the element to search100

    The element is not present in the list

    Enter your choice: 4Enter the element to search20

    The element is present in the list

    Enter your choice: 2Enter the element to insert:100

    Where do u want insert:b.Beginningm.Middlel.LastEnter your choice:mEnter the element after which u want to insert20

    The element is present in the listElement is inserted

    Enter your choice: 510-->20-->100-->30-->NULL

    Enter your choice: 3Enter the value u want to delete20

  • 8/8/2019 Exam Programs

    21/29

    The element is present in the listThe element is deleted

    Enter your choice: 510-->100-->30-->NULL

    Enter your choice:7

    SHELL SORT

    AIM:To sorting the given numbers using Shell sort.

    OBJECTIVE:

    To sorting given numbers in ascending order using Shell sort.

    HARDWARE REQUIREMENTS:

    PC (Pentium IV)

    SOFTWARE REQUIREMENTS:

    Turbo C Compiler

    PROGRAM:#include#includevoid shell_sort(int a[],int n);void main(){int n,i,a[100];clrscr();printf("\t\t\n enter the size of the elements");scanf("%d",&n);printf("\t\t\n enter the elements");

    for(i=0;i

  • 8/8/2019 Exam Programs

    22/29

    int i,j,k,temp;for(k=n/2;k>0;k=k/2){for(i=k;i=k&&a[j-k]>temp;j=j-k){a[j]=a[j-k];}a[j]=temp;getch();}}}

    OUTPUT:

    Enter the number of elements: 5Enter the array elements one by one50 20 60 35 40

    The sorted list is : 2035405060

    Enter the number of elements: 6Enter the array elements one by one53 26 34 12 9 45

    The sorted list is : 91226344553

    RESULT:Thus the Shell sort program is executed and the output is verified.QUICK SORT:PROGRAM:#include#includevoid quicksort(int a[],int,int);void main(){

    int a[10],i;clrscr();printf("/n Enter the number of elements to sort\n");

    for(i=0; i

  • 8/8/2019 Exam Programs

    23/29

    scanf("%d",&a[i]);}quicksort(a,0,9);printf("\n The sorted list is");for(i=0; i

  • 8/8/2019 Exam Programs

    24/29

    405080

    Enter the number of content: 6

    Enter the numbers one by one95 32 14 58 45 12The sorted list is: 12

    1432455895

    HEAP SORT

    PROGRAM:

    #include#includevoid heap(int a[],int n);void create(int a[],int n);void main(){int a[50],i,n;clrscr();printf("enter the limit:");scanf("%d",&n);printf("Enter the elements");for(i=0;i

  • 8/8/2019 Exam Programs

    25/29

    }void heap(int a[],int n){int i,j,q,key,temp;create(a,n);

    for(q=n-1;q>=1;q--){temp=a[0];a[0]=a[q];a[q]=temp;i=0;key=a[0];

    j=1;if(a[j+1]a[j])

    j=j+1;while(jkey))

    {a[i]=a[j];i=j;

    j=2*i;if((j+1)a[j])

    j=j+1;else if(j>n-1)

    j=n-1;a[i]=key;}}}

    OUTPUT:

    Enter the number of content: 5Enter the elements:16181143

    40

    The sort list using HEAP is:

    11 16 18 40 43

    Enter the number of content: 6Enter the elements:18191042

    4112

  • 8/8/2019 Exam Programs

    26/29

    The sort list using HEAP is:

    10 12 18 19 41 42

    MERGE SORT

    PROGRAM:

    #include#includevoid mergesplit(int a[],int first,int last);void merge(int a[],int f1,int l1,int f2,int l2);int a[25],b[25];void main(){

    int i,n;clrscr();printf("enter the limit:");scanf("%d",&n);printf("Enter the elements");for(i=0;i

  • 8/8/2019 Exam Programs

    27/29

    }while(i

  • 8/8/2019 Exam Programs

    28/29

    for(i=0;i

  • 8/8/2019 Exam Programs

    29/29

    printf("\t\t\n ENTER THE ELEMENTS");

    for(i=0;i