Lab Report of Data Structure Lab Course Code: CSE-206: U N I V E R S I T Y
Lab Report of Data Structure Lab Course Code: CSE-206: U N I V E R S I T Y
Lab Report of Data Structure Lab Course Code: CSE-206: U N I V E R S I T Y
Signature
1. Write a c program that will check whether a given array
(list of numbers) sorted or not?
1 #include <stdio.h>
2 int main()
3 {
4 int n, s[1000], a = 1, d = 1, i;
5 printf("Enter The Number of array: ");
6 scanf("%d", &n);
7 for (i = 0; i < n; i++)
8 scanf("%d", &s[i]);
9 i = 0;
10 while ((a == 1 || d == 1) && i < n -1)
11 {
12 if (s[i] < s[i+1])
13 d = 0;
14 else if (s[i] > s[i+1])
15 a = 0;
16 i++;
17 }
18 if (a == 1)
19 printf("The array is sorted in ascending
order.\n");
20 else if (d == 1)
21 printf("The array is sorted in descending
order.\n");
22 else
23 printf("The array is not sorted.\n");
24 return 0;
25 }
2. Implement stack with push and pop operation.
1 #include<stdio.h>
2 int stack[100],choice,n,top,x,i;
3 void push(void);
4 void pop(void);
5 void display(void);
6 int main()
7 {
8 top=-1 ;
9 printf("\n Enter the size of STACK[MAX=100]:");
10 scanf("%d",&n);
11 printf("\n\t STACK OPERATIONS USING ARRAY");
12 printf("\n\t--------------------------------");
13 printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
14 do
15 {
16 printf("\n Enter the Choice:");
17 scanf("%d",&choice);
18 switch(choice)
19 {
20 case 1 :
21 {
22 push();
23 break;
24 }
25 case 2:
26 {
27 pop();
28 break;
29 }
30 case 3:
31 {
32 display();
33 break;
34 }
35 case 4:
36 {
37 printf("\n\t EXIT POINT ");
38 break;
39 }
40 default:
41 {
42 printf ("\n\t Please Enter a Valid
Choice(1/2/3/4)");
43 }
44 }
45 }
46 while(choice!=4);
47 return 0;
48 }
49 void push()
50 {
51 if(top>=n-1 )
52 {
53 printf("\n\tSTACK is over flow");
54
55 }
56 else
57 {
58 printf(" Enter a value to be pushed:");
59 scanf("%d",&x);
60 top++;
61 stack[top]=x;
62 }
63 }
64 void pop()
65 {
66 if(top<=-1 )
67 {
68 printf("\n\t Stack is under flow");
69 }
70 else
71 {
72 printf("\n\t The popped elements is %d",stack[top]);
73 top--;
74 }
75 }
76 void display()
77 {
78 if(top>=0)
79 {
80 printf("\n The elements in STACK \n");
81 for(i=top; i>=0; i--)
82 printf("\n%d",stack[i]);
83 printf("\n Press Next Choice");
84 }
85 else
86 {
87 printf("\n The STACK is empty");
88 }
89 }