Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
3.9 kB
1
Indexable
[17/12, 3:35 pm] cute boy: 1#include <stdio.h> 2 #include <stdlib.h>

3 struct node

4 {

5 int coeff; int exp;

6

struct node *next;

8 typedef node;

10 node *insert (node *head, int co, int ex);

12 node *create(node *head)

13 (

int n, i, coef, expo; printf("\nEnter the number of berns: ")

scanf("%d",&n);

for(i=0; i<n; i++) {

printf("\nEnter the coefficient for term %d: ",i+1); scanf("%d",&coef);

printf("\nEnter the exponent for term %d: ",i+1);

23

scanf("%d",&expo); head insert (head, coef, expo):

24 } 25 return head;

26}

27

28 node *insert(node head, int co. int ex)

29 [

30 node *temp:

31 node *newP = (node)malloc(sizeof(node));

32 newp->coeff = co; newp->exp = ex;

33

34

35

newp->next = NULL; if(head == NULL || ex> head->exp)
[17/12, 3:36 pm] cute boy: 37 38

newp->next = head; head = newP;

potyAddn.c

-/ds_070

40 41

} else

temp = head;

43 44

while (temp->next != NULL. && temp->next->exp >ex)

46 47

temp temp->next; newp->next = temp->next;

temp->next = newP:

49 return head;

50 }

52 void print (node *head)

53 (

if(head= NULL)

printf("\nEmpty polynomial. ");

else

{

node *temp = head; while(temp 1= NULL)

56 57

71

(

if(temp->coeff == 0)

temp = temp->next;

continue;

} else {

if (temp = NULL)

72

73

else

C -

if(temp->coeff=head->coeff || temp->next == NULL) printf("");
[17/12, 3:37 pm] cute boy: 81 82

83

84

85

86

87

87 88

89

98

91

92

93

94

95

96

07 97

98

99

100

181

102

103

104

105

186

107

108

109

110

111

112}

113

114 void 115 (

116 117

回

HIT/I

Fri 15:24

polyAddn.c -Ids_070

printf("\n");

if(temp->exp != 8)

if(temp->coeff !=1)

if(temp->coeff == -1) printf(" ");

else printf("%d", temp->coeff);

}

printf("x"); if(temp->exp != 1)

printf("d". temp->exp);

if(temp->coeff == B) printf("");

else if(temp->coeff <B) printf("%d",temp->coeff); printf("+%d", temp->coeff); else

} else

(

}

temp = temp->next;

printf("\n");

polyAdd(node headl, node *head2)

node *ptrl = headl; node *ptr2 - head2;
[17/12, 3:38 pm] cute boy: 117

119

128

121

121

122

123

124

125

125

126

127

128 129 129

130

138

131 132 1932 131

133

134

135

136

137

}

while (ptrl != NULL && ptr2 != NULL)

if(ptrl->exp=== ptr2->exp)

=

head3= insert (head3, ptrl->coeff + ptr2->coeff, ptrl->exp);

ptrl= ptrl->next;

ptr2 = ptr2->next;

(

HIT / IC

polyAddn.c -/ids_070

}

else if (ptrl->exp > ptr2->exp) head3= insert (head3, ptr1->coeff, ptrl->exp);

ptr1= ptrl->next;

}

else if(ptri->exp < ptr2->exp)

head3= insert (head3, ptr2->coeff, ptr2->exp);

ptr2 = ptr2->next;

}

138 while (ptrl !=NULL)

139 {

148

141

142 }

head3= insert(head3, ptrl->coeff, ptrl->exp); ptr1= ptri->next;

143 while (ptr2 !=NULL)

144 {

145

146

147 }

148

149

150 }

151

head3= insert (head3, ptr2->coeff, ptr2->exp); ptr2= ptr2->next;

printf("\nThe final polynomial after Addition is: "); print (head3);

152 int main()
[17/12, 3:38 pm] cute boy: 138 131

132 133 133

134

135 136

137 }

11 Text Editor

回

}

HIT/ICT

FU 15:23

polyAddn.c -Ids_070

ptrl= ptri->next;

else if(ptrl->exp < ptr2->exp)

head3= insert (head3, ptr2->coeff, ptr2->exp); ptr2 = ptr2->next;

}

while(ptrl !=NULL) 139 {

138

140

148

141 191 142

142 }

143

144 145

146

147

148 149

158)

151

head3= insert(head3, ptri->coeff, ptrl->exp); ptr1= ptrl->next;

while(ptr2 !=NULL)

(

}

head3= insert (head3, ptr2->coeff, ptr2->exp); ptr2 = ptr2->next;

printf("\nThe final polynomial after Addition is: "); print (head3);

152 int main()

153 (

154 155

node *head1 = NULL; node head2 = NULL;

156 printf("\nEnter the first polynomial: ");

157 158

headl= create (head1); printf("\nEnter the second polynomial: ");

head2 = create (head2);

159 160 162 printf("\nThe first polynomial is: "); printf("\nThe second polynomial as: ");

161

print (head1);

163 print (head2);

164

165

166)

polyAdd (head, head2);

return 8: