data structures lab c programs

20
DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ] DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING -------------------- HKBK COLLEGE OF ENGINEERING Bengaluru - 560045 by: Prof.Syed Mustafa

Upload: syed-mustafa

Post on 08-Jul-2015

158 views

Category:

Engineering


5 download

DESCRIPTION

Third Semester B.E CSE/ISE Data Structures Lab programs- C Programs

TRANSCRIPT

Page 1: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

DEPARTMENT OF INFORMATION

SCIENCE AND ENGINEERING

--------------------

HKBK COLLEGE OF ENGINEERING

Bengaluru - 560045

by:

Prof.Syed Mustafa

Page 2: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 1 of 19 DS Lab Programs

Subject Code: 10CSL37 I.A. Marks : 25 Hours/Week : 03 Exam Hours: 03 Total Hours : 42 Exam Marks: 50

1. Using circular representation for a polynomial, design, develop, and execute a

program in C to accept two polynomials, add them, and then print the resulting polynomial.

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;

11. void attach(NODE head,int coef,int power) /* attach-insert at end of list */

12. {

13. NODE temp;

14. NODE cur=(NODE)malloc(sizeof(struct node));

15. cur->coef=coef;

16. cur->power=power;

17. temp=head->link; /* actual first node */

18. while(temp->link!=head) /* to reach last node*/

19. temp=temp->link;

20. temp->link=cur;

21. cur->link=head;

22. return(head);

23. } /* end of attach-insert end */

24. void display(NODE head)

25. {

26. NODE cur=head->link;

27. printf ("%dx^%d",cur->coef,cur-> power);

28. cur=cur->link;

29. while(cur!=head)

30. {

31. printf (" + %dx^%d",cur->coef,cur->power);

32. cur=cur->link;

33. } /* end while */

34. }/* end of display */

Page 3: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 2 of 19 DS Lab Programs

35. NODE createpoly( ) /* create a list for every polynomial */

36. {

37. int n,i,coef,power;

38. NODE head=(NODE)malloc(sizeof(struct node));

39. head->link=head;

40. printf("Enter no of terms\n");

41. scanf("%d",&n);

42. printf("Give in Descending order of power\n");

43. for(i=1;i<=n;i++)

44. {

45. printf("Enter Co-Efficient and Power of term %d:\n",i);

46. scanf("%d%d",&coef,&power);

47. attach(head,coef,power);

48. }

49. return(head);

50. } /*end of createpoly */

51. NODE addpoly(NODE cp1,NODE cp2,NODE p3) /* addition of 2 polynomials*/

52. { 53. NODE p1=cp1->link;

54. NODE p2=cp2->link;

55. while(p1!=cp1 && p2!=cp2)

56. {

57. if(p1->power<p2->power)

58. {

59. attach(p3,p2->coef,p2->power);

60. p2=p2->link;

61. }

62. else if(p1->power>p2->power)

63. {

64. attach(p3,p1->coef,p1->power);

65. p1=p1->link;

66. }

67. else

68. {

69. attach(p3,p1->coef+p2->coef,p1-> power);

70. p1=p1->link;

71. p2=p2->link;

72. }

73. } /* end while*/

74. while(p1!=cp1)

75. {

76. attach(p3,p1->coef,p1->power);

77. p1=p1->link;

78. }

Page 4: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 3 of 19 DS Lab Programs

79. while(p2!=cp2)

80. {

81. attach(p3,p2->coef,p2->power);

82. p2=p2->link;

83. }

84. return(p3);

85. } /*end of addpoly*/

86. main() /* Main Program */

87. {

88. NODE poly1,poly2;

89. NODE poly3=(NODE)malloc(sizeof(struct node));

90. poly3->link=poly3;

91. clrscr();

92. printf("Enter the details for first polynomial\n");

93. poly1=createpoly();

94. printf("Enter the details for Second polynomial\n");

95. poly2=createpoly();

96. addpoly(poly1,poly2,poly3);

97. printf("\nThe given first polynomial is:\n");

98. display(poly1);

99. printf("\nThe given second polynomial is:\n");

100. display(poly2);

101. printf("\nThe resultant polynomial is:\n");

102. display(poly3);

103. getch();

104. }/* end main */

OUTPUT:

Page 5: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 4 of 19 DS Lab Programs

2. Design, develop, and execute a program in C to convert a given valid parenthesized infix arithmetic expression to postfix expression and then to print both the expressions. The expression consists of single character operands and the binary operators + (plus), -(minus), * (multiply) and / (divide).

1. #define SIZE 50 /* Size of Stack */

2. #include<stdio.h>

3. #include<conio.h>

4. #include <ctype.h>

5. char s[SIZE];

6. int top = -1; /* Global declarations */

7. void push(char item) /* Function for PUSH operation */

8. {

9. s[++top] = item;

10. } /* end of push */

11.

12. char pop( ) /* Function for POP operation */

13. {

14. return (s[top--]);

15. } /* end of pop*/

16. int pr(char ch) /* Function for precedence */

17. {

18. switch (ch)

19. {

20. case '(': return 1;

21. case '+':

22. case '-': return 2;

23. case '*':

24. case '/': return 3;

25. }

26. } /* end of pr */

27. void infixtopostfix(char infix[ ],char postfix[ ])

28. {

29. int i,k=0;

30. char ch;

31. for(i=0;infix[i]!=0;i++)

32. {

33. ch = infix[i];

34. if (ch == '(')

35. push(ch);

36. else if (isalnum(ch))

37. postfix[k++] = ch;

38. else if (ch == ')')

39. {

40. while (s[top] != '(')

41. postfix[k++] = pop();

Page 6: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 5 of 19 DS Lab Programs

42. pop(); /* Remove ( */

43. }

44. else

45. { /* Operator */

46. while (top!=-1 && pr(s[top]) >= pr(ch))

47. postfix[k++] = pop();

48. push(ch);

49. } /*end if*/

50. } /*end for*/

51. while (top != -1) /* Pop from stack till empty */

52. postfix[k++] = pop();

53. postfix[k] = '\0'; /* Make pofx as valid string */

54. } /* end of infixtopostfix*/

55. main() /* Main Program */

56. {

57. char infix[50], postfix[50];

58. clrscr();

59. printf("\n\nEnter the Infix Expression \n ");

60. scanf("%s", infix);

61. infixtopostfix(infix,postfix);

62. printf("\n\nGiven Infix Expression is: %s\n",infix);

63. printf("Postfix Expression is: %s\n",postfix);

64. getch();

65. } /*end of main*/

OUTPUT:

Page 7: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 6 of 19 DS Lab Programs

3. Design, develop, and execute a program in C to evaluate a valid postfix expression using stack. Assume that the postfix expression is read as a single line consisting of non-negative single digit operands and binary arithmetic operators. The arithmetic operators are +(add), - (subtract), * (multiply) and / (divide).

1. #define SIZE 50 /* Size of Stack */

2. #include <stdio.h>

3. #include<conio.h>

4. #include<math.h>

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 */

12. int pop( ) /* Function for POP operation */

13. { 14. return(s[top--]);

15. } /* end of pop */

16. int eval_post(char postfix[ ]) /* evaluating postfix expression*/

17. {

18. int i,a,b;

19. char ch;

20. for(i=0;postfix[i]!=0;i++)

21. {

22. ch=postfix[i];

23. if(ch<='9' && ch>='0')

24. push(ch-'0');

25. else

26. {

27. b=pop();

28. a=pop();

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*/

Page 8: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 7 of 19 DS Lab Programs

40. } /*end else- if*/

41. }/*end for*/

42. return pop();

43. }/*end eval_post*/

44. main( ) /* Main Program */

45. {

46. char postfix[50];

47. int result;

48. clrscr();

49. printf("Enter the Valid Postfix Expression\n");

50. scanf("%s",postfix);

51. result=eval_post(postfix);

52. printf("\n Given Postfix Expression: %s\n",postfix);

53. printf("\n Result after Evaluation: %d\n",result);

54. getch();

55. } /* end of main */

OUTPUT:

Page 9: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 8 of 19 DS Lab Programs

4. Design, develop, and execute a program in C to simulate the working of a queue of integers using an array. Provide the following operations:

a. Insert b. Delete c. Display

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( );

8. main( ) /* Main Program */

9. {

10. int i,item,choice;

11. clrscr();

12. for(;;)

13. {

14. printf("\nQUEUE\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT\n");

15. printf("Enter your choice\n");

16. scanf("%d",&choice);

17. switch(choice)

18. {

19. case 1: insertQ();

20. break;

21. case 2: deleteQ();

22. break;

23. case 3: displayQ();

24. break;

25. case 4: exit(0);

26. default: printf("Invalid choice\n");

27. } /* end of switch */

28. } /*end of of for */

29. } /*main end*/

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 */

Page 10: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 9 of 19 DS Lab Programs

42. void deleteQ( ) /* 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( ) /* 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:

Page 11: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 10 of 19 DS Lab Programs

USING PASS BY ADDRESS [POINTERS]

1. #include<stdio.h>

2. #include<conio.h>

3. #define size 5

4. void insertQ(int [],int*);

5. void deleteQ(int [],int*,int);

6. void displayQ(int [],int,int);

7. main( )/* Main Program */

8. {

9. int q[size],front=0,rear=-1;

10. int i,item,choice;

11. clrscr();

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;

21. case 2: deleteQ(q,&front,rear);

22. break;

23. case 3: displayQ(q,front,rear);

24. break;

25. case 4: exit(0);

26. default: printf("Invalid choice\n");

27. } /* end of switch */

28. } /*end of of for */

29. } /*main end*/

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");

Page 12: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 11 of 19 DS Lab Programs

38. scanf("%d",&item);

39. q[++*rear]=item;

40. }

41. } /* end of insertQ */

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:

Page 13: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 12 of 19 DS Lab Programs

5. [ Q.No: 9 ] Design, develop, and execute a program in C to read a sparse matrix of integer values and to search the sparse matrix for an element specified by the user. Print the result of the search appropriately. Use the triple <row, column, value> to represent an element in the sparse matrix.

1. #include<stdio.h>

2. #include<conio.h>

3. struct sparse

4. {

5. int row;

6. int col;

7. int val;

8. };

9. void readsparse(struct sparse sp[ ]) /* reading sparse matrix */

10. {

11. int i,j,m,n,val,k=0;

12. printf("Enter the order of sparse matrix\n");

13. scanf("%d%d",&m,&n);

14. printf("Enter the elements of sparse matrix\n");

15. for(i=0;i<m;i++)

16. for(j=0;j<n;j++)

17. {

18. scanf("%d",&val);

19. if(val!=0)

20. {

21. k++;

22. sp[k].row=i;

23. sp[k].col=j;

24. sp[k].val=val;

25. }

26. } /* end of ‘for j loop’*/

27. if(k>=(m*n/2))

28. {

29. printf("Given matrix is not a sparse Matrix\n");

30. getch();

31. exit(0);

32. }

33. /* first element has no of rows, cols and no of non-zero values */

34. sp[0].row=m;

35. sp[0].col=n;

36. sp[0].val=k;

37. } /* end of readsparse */

Page 14: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 13 of 19 DS Lab Programs

38. void displaysparse(struct sparse sp[ ]) /* displaying sparse matrix */

39. {

40. int i,k;

41. k=sp[0].val;

42. printf("The Given Sparse Matrix in triple format is:\n");

43. for(i=0;i<=k;i++)

44. printf("%d\t%d\t%d\n",sp[i].row,sp[i].col,sp[i].val);

45. } /* end of displaysparse*/

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 */

66. void main() /* Main program */

67. {

68. int m,n,val,key;

69. struct sparse sp[50];

70. clrscr();

71. readsparse(sp);

72. displaysparse(sp);

73. searchsparse(sp);

74. getch();

75. } /* end main */

OUTPUT:

Page 15: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 14 of 19 DS Lab Programs

OUTPUT:

Page 16: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 15 of 19 DS Lab Programs

6. [ Q.No: 10 ] Design, develop, and execute a program in C to create a max heap of integers by accepting one element at a time and by inserting it immediately in to the heap. Use the array representation for the heap. Display the array at the end of insertion phase. 1. #include<stdio.h>

2. #include<conio.h>

3. /* Heap used here is Max Heap */

4. int heap[100],n; /* by default. n=0 */

5. void push(int item) /*Insert an element into the heap */

6. {

7. /*Adjust its position*/

8. int i = ++n;

9. while(i!=1 && item>heap[i/2] ) /* check parent at i/2 */

10. {

11. heap[i] = heap[i/2]; /* swap parent */

12. i =i/2; /* move to parent */

13. }

14. heap[i] = item;

15. } /* end of push */

16. void main( ) /* Main program */

17. {

18. int size,i,item;

19. clrscr();

20. printf("Enter the total numbers\n");

21. scanf("%d",&size);

22. printf("Enter the elements\n");

23. for(i =1;i<=size;i++)

24. {

25. scanf("%d",&item);

26. push(item);

27. }

28. printf(“The elements of heap are:\n”);

29. for(i=1;i<=size;i++)

30. printf("%d\t",heap[i]);

31. getch();

32. } /* end of main */

OUTPUT:

Page 17: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 16 of 19 DS Lab Programs

7. [ Q.No: 11 ] Design, develop, and execute a program in C to implement a doubly linked list where each node consists of integers. The program should support the following operations: a. Create a doubly linked list by adding each node at the front. b. Insert a new node to the left of the node whose key value is read as an input. c. Delete the node of a given data if it is found, otherwise display appropriate message. d. Display the contents of the list. [ Note: Only either (a,b and d) or (a, c and d) may be asked in the Examination ] 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. };

10. typedef struct node * NODE;

11. NODE getnode( ) /* creating a new node */

12. {

13. int val;

14. NODE cur=(NODE)malloc(sizeof(struct node));

15. printf("Enter the Data\n");

16. scanf("%d",&val);

17. cur->val=val;

18. cur->rlink=0;

19. cur->llink=0;

20. return(cur);

21. } /* end of getnode */

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 */

Page 18: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 17 of 19 DS Lab Programs

32. void display(NODE first) /* display the double lin list */

33. {

34. if(first==0)

35. printf("LIST IS EMPTY\n");

36. else

37. {

38. printf("The LIST ELEMENTS are\n");

39. while(first!=0)

40. {

41. printf ("%d\t",first->val);

42. first=first->rlink;

43. } /* end while */

44. } /* end else */

45. } /* end of display */

46. NODE insertatleft(NODE first) /* insert a node to left of data node*/

47. {

48. int key;

49. NODE temp,prev,cur;

50. printf("Enter the key value to be searched\n");

51. scanf("%d",&key);

52. temp=first; /*if more than 1 node */

53. while(temp!=0 && temp->val!=key) /* finding the node with the given value */

54. temp=temp->rlink;

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;

64. if(first==temp) /*if first node [ temp->llink==0 ]*/

65. return(cur);

66. prev->rlink=cur;

67. cur->llink=prev;

68. return(first);

69. } /* end of insert end */

Page 19: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 18 of 19 DS Lab Programs

70. NODE deleteatdata(NODE first) /* deleting the given data node*/

71. {

72. int key;

73. NODE prev,next;

74. NODE temp=first;

75. printf("Enter the key value to be deleted\n");

76. scanf("%d",&key);

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;

86. if(temp->llink==0) /*if first node */

87. {

88. printf(" Node is deleted\n");

89. next->llink=0;

90. free(temp);

91. return(next);

92. }

93. prev->rlink=next;

94. next->llink=prev;

95. free(temp);

96. return(first);

97. }/* end of delete at data */

98. main( ) /* Main program */

99. {

100. int choice;

101. NODE first=0;

102. clrscr();

103. while(1)

104. {

105. printf("\nMENU\n1.INSERT AT FRONT\n2.INSERT AT LEFT USING DATA");

106. printf("\n3.DELETE NODE USING DATA\n4.DISPLAY LIST\n5.EXIT\n");

107. printf("Enter the CHOICE\n");

108. scanf("%d",&choice);

Page 20: Data structures lab  c programs

DATA STRUCTURES WITH C/C++ LABORATORY [ 10CSL37 ]

Prof. A. Syed Mustafa, HKBKCE. Page 19 of 19 DS Lab Programs

109. switch(choice)

110. {

111. case 1: first=insertatbeg(first);

112. break;

113. case 2: if(first==0)

114. printf("LIST IS EMPTY\n");

115. else

116. first=insertatleft(first);

117. break;

118. case 3: if(first==0)

119. printf("LIST IS EMPTY\n");

120. else

121. first=deleteatdata(first);

122. break;

123.

124. case 4: display(first);

125. break;

126. case 5: exit(0);

127. default:printf("Invalid choice\n");

128. }/* end switch */

129. } /* end while */

130. } /* end main */

OUTPUT: