0% found this document useful (0 votes)
7 views

1. Array and Ll Notes

The document contains two C programs. The first program creates and displays a simple linked list with three nodes containing integer data. The second program creates a polynomial linked list, allows user input for polynomial terms, displays the polynomial, and evaluates it for a given value of x.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

1. Array and Ll Notes

The document contains two C programs. The first program creates and displays a simple linked list with three nodes containing integer data. The second program creates a polynomial linked list, allows user input for polynomial terms, displays the polynomial, and evaluates it for a given value of x.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

// C Program to create a Linked List

#include <stdio.h>
#include <stdlib.h>
typedef struct Node {

int data;
struct Node *next;
} Node;
int main() {

Node *first = (Node *)malloc(sizeof(Node));


first->data = 10;

Node *second = (Node *)malloc(sizeof(Node));

second->data = 20;

Node *third = (Node *)malloc(sizeof(Node));


third->data = 30;

first->next = second;
second->next = third;
third->next = NULL;
printf("Linked List: "); Linked List: 10 20 30
Node* temp = first;
while(temp) {
printf("%d ", temp->data);
temp = temp->next;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct Node {
int coeff;
int exp;
struct Node * next;
}* poly = NULL;

void create() {
struct Node * t, * last = NULL;
int num, i;

printf("Enter number of terms: ");


scanf("%d", & num);
printf("Enter each term with coeff and exp:\n");

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


t = (struct Node * ) malloc(sizeof(struct Node));
scanf("%d%d", & t -> coeff, & t -> exp);
t -> next = NULL;
if (poly == NULL) {
poly = last = t;
} else {
last -> next = t;
last = t;
}
}
}

void Display(struct Node * p) {


printf("%dx%d ", p -> coeff, p -> exp);
p = p -> next;

while (p) {
printf("+ %dx%d ", p -> coeff, p -> exp);
p = p -> next;
}
printf("\n");
}

long Eval(struct Node * p, int x) {


long val = 0;

while (p) {
val += p -> coeff * pow(x, p -> exp);
p = p -> next;
}

return val;
}

int main() {
int x;
create();
Display(poly);

printf("Enter value of x: ");


scanf("%d", &x);

printf("%ld\n", Eval(poly, x));


return 0;
}

You might also like