You are on page 1of 11

Data structure

Programs 12,13
By:-Atul
Enrollment no:-01516401520
Btech(I.T 1st year)
Program 12:-implement addition of two Polynomial expressions using singly
linked list.
Code
#include <stdio.h>
#include <conio.h>
#include <malloc.h>

struct node
{
int coeff;
int pow;
struct node *next;
};

#define newnode (struct node *)malloc(sizeof(struct node))


//need two functions
//one to store polynomial
//two for adding polynomial

//helper function
void show(struct node* Node)
{
while (Node->next != NULL) {
printf("%dx^%d", Node->coeff, Node->pow);
Node = Node->next;
if (Node->coeff >= 0) {
if (Node->next != NULL)
printf("+");
}
}
}

void create_node(int x, int y, struct node** temp)


{
struct node *r, *z;
z = *temp;
if (z == NULL) {
r = newnode;
r->coeff = x;
r->pow = y;
*temp = r;
r->next = newnode;
r = r->next;
r->next = NULL;
}
else {
r->coeff = x;
r->pow = y;
r->next = newnode;
r = r->next;
r->next = NULL;
}
}

//add polynomials
void add_polynomial(struct node* poly1, struct node* poly2,
struct node* poly)
{
while (poly1->next && poly2->next) {
// If power of 1st polynomial is greater then 2nd,
// then store 1st as it is and move its pointer
if (poly1->pow > poly2->pow) {
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}

// If power of 2nd polynomial is greater then 1st,


// then store 2nd as it is and move its pointer
else if (poly1->pow < poly2->pow) {
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}

// If power of both polynomial numbers is same then


// add their coefficients
else {
poly->pow = poly1->pow;
poly->coeff = poly1->coeff + poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}

// Dynamically create new node


poly->next
= (struct node*)malloc(sizeof(struct node));
poly = poly->next;
poly->next = NULL;
}
while (poly1->next || poly2->next) {
if (poly1->next) {
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
if (poly2->next) {
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
poly->next
= (struct node*)malloc(sizeof(struct node));
poly = poly->next;
poly->next = NULL;
}
}

void operation(){
struct node *poly1 = NULL, *poly2 = NULL, *poly = NULL;

// Create first list of 3x^2 + 6x^1 + 5x^0


create_node(3, 2, &poly1);
create_node(6, 1, &poly1);
create_node(5, 0, &poly1);

//second polynomial 2x^1 + 6x^0


create_node(2, 1, &poly2);
create_node(6, 0, &poly2);

printf("1st Number: ");


show(poly1);

printf("\n2nd Number: ");


show(poly2);

poly = newnode;
// Function add two polynomial numbers
add_polynomial(poly1, poly2, poly);

// Display resultant polynomial


printf("\nAdded polynomial: ");
show(poly);
}

int main()
{
operation();

return 0;
}

OUTPUTS
1)

2)
Program 13:-implement linear search and selection sort in a single linked list
Code
#include <stdio.h>
#include <stdlib.h>

struct LinkNode
{
int data;
struct LinkNode *next;
};

// Singly linked list


struct SingleLL
{
struct LinkNode *head;
struct LinkNode *tail;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
// Create memory of head and tail Nodes
struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct
SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
sll->head = NULL;
}
return sll;
}
// Returns a new Node of linked list
struct LinkNode *createLinkNode(int data)
{
// Create dynamic node
struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct
LinkNode));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
// Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
// Add new Node at end of linked list
void addNode(struct SingleLL *sll, int data)
{
struct LinkNode *node = createLinkNode(data);
if (sll->head == NULL)
{
sll->head = node;
}
else
{
struct LinkNode *temp = sll->head;
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
// Append the node at last position
temp->next = node;
}
}
// Display linked list element
void display(struct SingleLL *sll)
{
if (sll->head == NULL)
{
printf("\n Empty linked list\n");
return;
}
struct LinkNode *temp = sll->head;
// iterating linked list elements
while (temp != NULL)
{
if (temp != sll->head)
{
printf(" →");
}
printf(" %d", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" → NULL\n");
}

// Swap the linked list node value


void swapData(struct LinkNode *a, struct LinkNode *b)
{
int temp = a->data;
a->data = b->data;
b->data = temp;
}
int linear_search(struct node *head, int data)
{
//traverse the list and compare list->data
int count = 1;
if (head == 0)
{
return -1;
}
while (head)
{
if (head->data == data)
{
return count;
}
else
{
count++;
}
head = head->next;
}
return -1;
}
// Sort the linked list using selection sort
void selectionSort(struct SingleLL *sll)
{

struct LinkNode*auxiliary = sll->head;


struct LinkNode*temp = NULL;
struct LinkNode*node = NULL;

// Execute linked list node


while(auxiliary != NULL)
{
node = auxiliary;
temp = auxiliary->next;

// Find the minimum node


while(temp != NULL)
{
if(node->data > temp->data)
{
node = temp;
}
// Visit to next node
temp = temp->next;
}
if(auxiliary->data > node->data)
{
// Transfer minimum value to initial position
// Swap the node value
swapData(auxiliary,node);
}
// Visit to next node
auxiliary = auxiliary->next;
}
}
int main()
{
// Create a empty linked list
struct SingleLL *sll = newLinkedList();

// Constructed linked list


// 2 → 5 → 0 → 8 → 2 → 4 → 9 → 7 → 0 → 1 → NULL
addNode(sll, 2);
addNode(sll, 5);
addNode(sll, 0);
addNode(sll, 8);
addNode(sll, 2);
addNode(sll, 4);
addNode(sll, 9);
addNode(sll, 7);
addNode(sll, 0);
addNode(sll, 1);

printf("\n Before sort \n");


display(sll);
// Sort linked list
selectionSort(sll);
// 0 → 0 → 1 → 2 → 2 → 4 → 5 → 7 → 8 → 9 → NULL
printf("\n After sort \n");
display(sll);
return 0;
}

Outputs
1)

2)

You might also like