Created
April 1, 2023 19:14
-
-
Save Adwaith-Rajesh/e5e8e3c414aa4dd7be1f481f9181ec1e to your computer and use it in GitHub Desktop.
polynomial addition in zig using linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const stdout = std.io.getStdOut().writer(); | |
const Term = struct { | |
coef: f32, | |
exp: i32, | |
next: ?*Term = null, | |
}; | |
const Polynomial = struct { | |
terms: ?*Term = null, | |
curr: ?*Term = null, | |
allocator: std.mem.Allocator, | |
fn createTerm(self: Polynomial, coef: f32, exp: i32) !*Term { | |
var new_term = try self.allocator.create(Term); | |
new_term.coef = coef; | |
new_term.exp = exp; | |
new_term.next = null; | |
return new_term; | |
} | |
fn insertTerm(self: *Polynomial, coef: f32, exp: i32) !void { | |
var new_term = try self.createTerm(coef, exp); | |
if (self.terms == null) { | |
self.terms = new_term; | |
return; | |
} | |
var temp = self.terms; | |
while (temp.?.next != null) { | |
temp = temp.?.next; | |
} | |
temp.?.next = new_term; | |
} | |
fn destroy(self: *Polynomial) void { | |
var temp: ?*Term = undefined; | |
while (self.terms) |v| { | |
temp = self.terms; | |
self.terms = v.next; | |
self.allocator.destroy(temp.?); | |
} | |
} | |
fn displayPoly(poly: Polynomial) !void { | |
var temp = poly.terms; | |
if (temp == null) { | |
try stdout.print("0\n", .{}); | |
} | |
while (temp) |v| { | |
try stdout.print("{d:.2}x^{d}{s}", .{ v.coef, v.exp, if (v.next == null) "\n" else " + " }); | |
temp = v.next; | |
} | |
} | |
fn compare(a: i32, b: i32) i8 { | |
return if (a == b) 0 else (if (a > b) 1 else -1); | |
} | |
// operation | |
fn add(self: Polynomial, o: Polynomial) !Polynomial { | |
var new_poly = Polynomial{ .allocator = self.allocator }; | |
var self_temp = self.terms; | |
var o_temp = o.terms; | |
while (self_temp != null and o_temp != null) { | |
var stu = self_temp.?; // self_temp value unwrapper | |
var otu = o_temp.?; | |
// var c = if (stu.exp == otu.exp) 0 else (if (stu.exp > otu.exp) 1 else -1); | |
switch (compare(stu.exp, otu.exp)) { | |
0 => { | |
if (stu.exp == otu.exp) { | |
try new_poly.insertTerm(stu.coef + otu.coef, stu.exp); | |
self_temp = stu.next; | |
o_temp = otu.next; | |
} | |
}, | |
1 => { | |
try new_poly.insertTerm(stu.coef, stu.exp); | |
self_temp = stu.next; | |
}, | |
-1 => { | |
try new_poly.insertTerm(otu.coef, otu.exp); | |
o_temp = otu.next; | |
}, | |
else => unreachable, | |
} | |
} | |
while (self_temp) |v| { | |
try new_poly.insertTerm(v.coef, v.exp); | |
self_temp = v.next; | |
} | |
while (o_temp) |v| { | |
try new_poly.insertTerm(v.coef, v.exp); | |
o_temp = v.next; | |
} | |
return new_poly; | |
} | |
}; | |
pub fn main() !void { | |
const allocator = std.heap.page_allocator; | |
var p1 = Polynomial{ | |
.allocator = allocator, | |
}; | |
var p2 = Polynomial{ | |
.allocator = allocator, | |
}; | |
try p1.insertTerm(2.4, 3); | |
try p1.insertTerm(2, 2); | |
try p2.insertTerm(2.5, 4); | |
try p2.insertTerm(2, 2); | |
// try displayPoly(p1); | |
var p3 = try p1.add(p2); | |
try p1.displayPoly(); | |
try p2.displayPoly(); | |
try p3.displayPoly(); | |
p1.destroy(); | |
p2.destroy(); | |
p3.destroy(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if you think you can make it better, please do, I'm still a noob when it comes to zig, so any suggestions will be appreciated