Data Structures LAB - C Programs
Data Structures LAB - C Programs
[ 10CSL37 ]
by:
Prof.Syed Mustafa
DEPARTMENT OF INFORMATION
SCIENCE AND ENGINEERING
--------------------
HKBK COLLEGE OF ENGINEERING
Bengaluru - 560045
DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]
1. #include<stdio.h>
2. #include<conio.h>
3. #include<alloc.h>
4. struct node
5. {
6. int coef;
7. int power;
8. struct node *link;
9. };
10. typedef struct node * NODE;
22. return(head);
74. while(p1!=cp1)
75. {
76. attach(p3,p1->coef,p1->power);
77. p1=p1->link;
78. }
79. while(p2!=cp2)
80. {
81. attach(p3,p2->coef,p2->power);
82. p2=p2->link;
83. }
84. return(p3);
OUTPUT:
OUTPUT:
5. int s[SIZE];
6. int top=-1; /* Global declarations */
7.
8. void push(int elem) /* Function for PUSH operation */
9. {
10. s[++top]=elem;
11. } /* end of push */
29. switch(ch)
30. {
31. case '+':push(a+b);
32. break;
33. case '-':push(a-b);
34. break;
35. case '*':push(a*b);
36. break;
37. case '/':push(a/b);
38. break;
39. }/*end switch*/
OUTPUT:
1. #include<stdio.h>
2. #include<conio.h>
3. #define size 5
4. int q[size],front=0,rear=-1;
5. void insertQ( );
6. void deleteQ( );
7. void displayQ( );
30. void insertQ( ) /* insert an element at the rear end of the queue*/
31. {
32. int item;
33. if(rear==size-1)
34. printf("QUEUE IS FULL\n");
35. else
36. {
37. printf("Enter the element\n");
38. scanf("%d",&item);
39. q[++rear]=item;
40. }
41. }/* end of insertQ */
OUTPUT:
1. #include<stdio.h>
2. #include<conio.h>
3. #define size 5
12. for(;;)
13. {
14. printf("\nQUEUE\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT\n");
15. printf("\nEnter your choice\n");
16. scanf("%d",&choice);
17. switch(choice)
18. {
19. case 1: insertQ(q,&rear);
20. break;
30. void insertQ(int q[ ],int *rear) /* insert an element at the rear end of the queue*/
31. {
32. int item;
33. if(*rear==size-1)
34. printf("QUEUE IS FULL\n");
35. else
36. {
37. printf("Enter the element\n");
42. void deleteQ(int q[],int *front,int rear) /* deleting from the front end of queue*/
43. {
44. int item;
45. if(*front>rear)
46. printf("QUEUE IS EMPTY\n");
47. else
48. {
49. item=q[(*front)++];
50. printf("The deleted item =%d\n",item);
51. }
52. } /* end of deleteQ*/
53. void displayQ(int q[ ],int front,int rear) /* display the content of the queue */
54. {
55. int i;
56. if(front>rear)
57. printf("The queue is empty\n");
58. else
59. {
60. printf("The Elements of the Queue are :\n");
61. for(i=front;i<=rear;i++)
62. printf("%d ",q[i]);
63. }
64. } /* end of displayQ */
OUTPUT:
1. #include<stdio.h>
2. #include<conio.h>
3. struct sparse
4. {
5. int row;
6. int col;
7. int val;
8. };
27. if(k>=(m*n/2))
28. {
29. printf("Given matrix is not a sparse Matrix\n");
30. getch();
31. exit(0);
32. }
46. void searchsparse(struct sparse sp[ ]) /* searching for a key in sparse matrix */
47. {
48. int found=0,i,key,k;
49. k=sp[0].val;
50. printf("Enter the element to be searched in Sparse Matrix\n");
51. scanf("%d",&key);
52. for(i=1;i<=k;i++)
53. if(sp[i].val==key)
54. {
55. found=1;
56. break;
57. }
58. if(found)
59. {
60. printf("Search successful\nElement %d found at ",key);
61. printf("row: %d\t Column: %d\n", sp[i].row+1,sp[i].col+1);
62. }
63. else
64. printf("Search not Successful\n");
65. } /* end of searchsparse */
OUTPUT:
OUTPUT:
1. #include<stdio.h>
2. #include<conio.h>
3. /* Heap used here is Max Heap */
4. int heap[100],n; /* by default. n=0 */
OUTPUT:
1. #include<stdio.h>
2. #include<conio.h>
3. #include<alloc.h>
4. struct node
5. {
6. int val;
7. struct node *rlink;
8. struct node *llink;
9. };
22. NODE insertatbeg(NODE first) /*insert the node at the begining of the list*/
23. {
24. NODE cur=getnode();
25. if(first!=0)
26. {
27. cur->rlink=first;
28. first->llink=cur;
29. }
30. return cur;
31. } /* end of insert beg */
55. if(temp==0) /* if key not found ,temp will reach NULL, list ends */
56. {
57. printf("KEY IS NOT FOUND\n");
58. return first;
59. }
60. prev=temp->llink;
61. cur=getnode();
62. cur->rlink=temp;
63. temp->llink=cur;
66. prev->rlink=cur;
67. cur->llink=prev;
68. return(first);
77. while(temp!=0 && temp->val!=key) /* finding the node with the given data */
78. temp=temp->rlink;
79. if(temp==0) /* if key not found ,temp will reach NULL,list end */
80. {
81. printf("KEY IS NOT FOUND\n");
82. return first;
83. }
84. prev=temp->llink;
85. next=temp->rlink;
93. prev->rlink=next;
94. next->llink=prev;
95. free(temp);
96. return(first);
OUTPUT: