Exercise-3
1. Implement a linked list to represent polynomials and perform addition?
#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;
}
Output:
Enter number of terms: 4
Enter each term with coeff and exp:
46
24
34
45
4x6 + 2x4 + 3x4 + 4x5
Enter value of x: 4
21760
2. C Program to detect and remove duplicates from a Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
} *first = NULL;
void Create(int A[], int n)
{
int i;
struct Node *t, *last;
first = (struct Node *) malloc (sizeof (struct Node));
first->data = A[0];
first->next = NULL;
last = first;
for (i = 1; i < n; i++)
{
t = (struct Node *) malloc (sizeof (struct Node));
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node *p)
{
while (p != NULL)
{
printf ("%d ", p->data);
p = p->next;
}
}
void RemoveDuplicate(struct Node *p)
{
struct Node *q = p->next;
while (q != NULL)
{
if (p->data != q->data)
{
p = q;
q = q->next;
}
else
{
p->next = q->next;
free(q);
q = p->next;
}
}
}
int main()
{
int A[] = { 4, 4, 7, 7, 7 };
Create (A, 5);
printf ("Linked List with Duplicates: \n");
Display (first);
RemoveDuplicate (first);
printf ("\nLinked List without Duplicates: \n");
Display (first);
return 0;
}
Exercise-4
1. Implement double linked list and perform various operations to understand
properties and its applications?
/* C program for the all operations in the Doubly Linked List*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *prev, *next;
};
struct node* start = NULL;
// Function to traverse the linked list
void traverse()
{
// List is empty
if (start == NULL)
{
printf("\nList is empty\n");
return;
}
// Else print the Data
struct node* temp;
temp = start;
while (temp != NULL)
{
printf("Data = %d\n", temp->info);
temp = temp->next;
}
}
void insertAtFront()
{
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->prev = NULL;
temp->next = start;
start = temp;
}
void insertAtEnd()
{
int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->next = NULL;
trav = start;
// If start is NULL
if (start == NULL)
{
start = temp;
}
// Changes Links
else {
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}
void insertAtPosition()
{
int data, pos, i = 1;
struct node *temp, *newnode;
newnode = malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;
// Enter the position and data
printf("\nEnter position : ");
scanf("%d", &pos);
// If start==NULL,
if (start == NULL) {
start = newnode;
newnode->prev = NULL;
newnode->next = NULL;
}
// If position==1,
else if (pos == 1) {
/* newnode->next = start;
newnode->next->prev = newnode;
newnode->prev = NULL;
start = newnode; */
insertAtFront();
}
// Change links
else {
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
newnode->info = data;
temp = start;
while (i < pos - 1)
{
temp = temp->next;
i++;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
temp->next->prev = newnode;
}
}
void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
start = start->next;
if (start != NULL)
start->prev = NULL;
free(temp);
}
}
void deleteEnd()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
temp = start;
while (temp->next != NULL)
temp = temp->next;
if (start->next == NULL)
start = NULL;
else {
temp->prev->next = NULL;
free(temp);
}
}
void deletePosition()
{
int pos, i = 1;
struct node *temp, *position;
temp = start;
// If DLL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else
{
// Position to be deleted
printf("\nEnter position : ");
scanf("%d", &pos);
// If the position is the first node
if (pos == 1)
{
deleteFirst();
if (start != NULL)
{
start->prev = NULL;
}
free(position);
return;
}
// Traverse till position
while (i < pos - 1)
{
temp = temp->next;
i++;
}
// Change Links
position = temp->next;
if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;
// Free memory
free(position);
}
}
int main()
{
int choice;
while (1) {
printf("\n\t1 To see list\n");
printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
exit(1);
break;
default:
printf("Incorrect Choice. Try Again \n");
continue;
}
}
return 0;
}
2. Implement Circular linked list and perform various operations of circular linked list?
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
struct node *next;
};
struct node *head;
void beginsert ();
void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 7)
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from
last\n5.Search for an element\n6.Show\n7.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
printf("Please enter valid choice..");
void beginsert()
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
printf("\nOVERFLOW");
}
else
printf("\nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
head = ptr;
ptr -> next = head;
else
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
printf("\nnode inserted\n");
}
}
void lastinsert()
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
printf("\nOVERFLOW\n");
else
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
head = ptr;
ptr -> next = head;
else
temp = head;
while(temp -> next != head)
temp = temp -> next;
temp -> next = ptr;
ptr -> next = head;
printf("\nnode inserted\n");
void begin_delete()
struct node *ptr;
if(head == NULL)
printf("\nUNDERFLOW");
else if(head->next == head)
head = NULL;
free(head);
printf("\nnode deleted\n");
else
{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
void last_delete()
struct node *ptr, *preptr;
if(head==NULL)
printf("\nUNDERFLOW");
else if (head ->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
else
ptr = head;
while(ptr ->next != head)
preptr=ptr;
ptr = ptr->next;
preptr->next = ptr -> next;
free(ptr);
printf("\nnode deleted\n");
void search()
struct node *ptr;
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
if(head ->data == item)
printf("item found at location %d",i+1);
flag=0;
else
while (ptr->next != head)
if(ptr->data == item)
printf("item found at location %d ",i+1);
flag=0;
break;
else
flag=1;
i++;
ptr = ptr -> next;
if(flag != 0)
printf("Item not found\n");
void display()
struct node *ptr;
ptr=head;
if(head == NULL)
printf("\nnothing to print");
}
else
printf("\n printing values ... \n");
while(ptr -> next != head)
printf("%d\n", ptr -> data);
ptr = ptr -> next;
printf("%d\n", ptr -> data);