Prasit Reverse
Prasit Reverse
=> #include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void display (struct node *head)
{
if (head == NULL)
return;
display (head->next);
printf ("%d ", head->data);
}
int main ()
{
struct node *prev, *head, *p;
int n, i;
printf ("Enter size of Linked List: ");
scanf ("%d", &n);
head = NULL;
for (i = 0; i < n; i++)
{
p = malloc (sizeof (struct node));
printf ("Enter the data: ");
scanf ("%d", &p->data);
p->next = NULL;
if (head == NULL)
head = p;
else
prev->next = p;
prev = p;
}
display (head);
return 0;
}
2) Write a c program for merging two singly linked list.
#include<stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* new_Node(int data) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}
struct Node* mergeTwoLists(struct Node* head1, struct Node* head2) {
if (!head1) return head2;
if (!head2) return head1;
struct Node dummy;
struct Node* tail = &dummy;
while (head1 && head2) {
if (head1->data < head2->data) {
tail->next = head1;
head1 = head1->next;
} else {
tail->next = head2;
head2 = head2->next;
}
tail = tail->next;
}
tail->next = head1 ? head1 : head2;
return dummy.next;
}
void displayList(struct Node* head) {
while (head) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
struct Node* list1 = new_Node(1);
list1->next = new_Node(2);
list1->next->next = new_Node(5);
list1->next->next->next = new_Node(7);
struct Node* list2 = new_Node(3);
list2->next = new_Node(4);
list2->next->next = new_Node(6);
printf("Two sorted singly linked lists:\n");
displayList(list1);
displayList(list2);
struct Node* result = mergeTwoLists(list1, list2);
printf("\nAfter merging the said two sorted lists:\n");
displayList(result);
return 0;
}
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int coeff,pow;
struct node *next;
}*start1=NULL,*start2=NULL,*start=NULL,*start3=NULL,*p,*p1,*p
2,*temp,*a=NULL;
void main()
{
clrscr();
start1=(struct node *)malloc(sizeof(struct node));
start2=(struct node *)malloc(sizeof(struct node));
start3=(struct node *)malloc(sizeof(struct node));
printf("\nEnter 1st Polynomial\n");
create(start1);
printf("\nEnter 2nd Polynomial\n");
create(start2);
printf("\n1st Polynomial\n");
display(start1);
printf("\n2nd Polynomial\n");
display(start2);
add(start1,start2,start3);
printf("\nAdded Polynomial: \n");
display(start3);
start3=NULL;
sub(start1,start2,start3);
printf("\nSubtracted Polynomial: \n");
display(start3);
getch();
}
void create(struct node *temp)
{
char ch;
do
{
printf("Enter coefficient and power\n ");
scanf("%d%d",&temp->coeff,&temp->pow);
temp->next=(struct node *)malloc(sizeof(struct node));
temp=temp->next;
temp->next=NULL;
printf("Press y to continue\n");
ch=getch(); //To add terms to the polynomial
}while(ch=='y'|| ch=='Y');
}
void display(struct node *start)
{
p=start;
while(p->next!=NULL)
{
printf("%dx^%d",p->coeff,p->pow);
p=p->next;
if(p->next!=NULL)
printf(" + ");
}
printf("\n");
}
void add(struct node *start1,struct node *start2,struct node
*start3)
{
while(start1->next && start2->next)
{
if(start1->pow==start2->pow)
{
start3->coeff=start1->coeff+start2->coeff;
start3->pow=start1->pow;
start1=start1->next;
start2=start2->next;
}
else if(start1->pow > start2->pow)
{
start3->coeff=start1->coeff;
start3->pow=start1->pow;
start1=start1->next;
}
else
{
start3->coeff=start2->coeff;
start3->pow=start2->pow;
start2=start2->next;
}
start3->next=(struct node *)malloc(sizeof (struct node));
start3=start3->next;
start3->next=NULL;
}
while(start1->next || start2->next)
{
if(start1->next)
{
start3->pow=start1->pow;
start3->coeff=start1->coeff;
start1=start1->next;
}
if(start2->next)
{
start3->coeff=start2->coeff;
start3->pow=start2->pow;
start2=start2->next;
}
start3->next=(struct node *)malloc(sizeof(struct node));
start3=start3->next;
start3->next=NULL;
}
}
void sub(struct node *start1,struct node *start2,struct node
*start3)
{
while(start1->next && start2->next)
{
if(start1->pow==start2->pow)
{
start3->coeff=start1->coeff-start2->coeff;
start3->pow=start1->pow;
start1=start1->next;
start2=start2->next;
}
else if(start1->pow > start2->pow)
{
start3->coeff=start1->coeff;
start3->pow=start1->pow;
start1=start1->next;
}
else
{
start3->coeff=-start2->coeff;
start3->pow=start2->pow;
start2=start2->next;
}
start3->next=(struct node *)malloc(sizeof (struct node));
start3=start3->next;
start3->next=NULL;
}
while(start1->next || start2->next)
{
if(start1->next)
{
start3->pow=start1->pow;
start3->coeff=start1->coeff;
start1=start1->next;
}
if(start2->next)
{
start3->coeff=start2->coeff;
start3->pow=start2->pow;
start2=start2->next;
}
start3->next=(struct node *)malloc(sizeof(struct node));
start3=start3->next;
start3->next=NULL;
}
}
Output