DMI College of Engineering - dmice.ac.indmice.ac.in/wp-content/uploads/2017/05/PDSI.pdf · DMI...

66
DMI College of Engineering (Affiliated to Anna University, Chennai-600 025) Palanchur, Chennai-602 123. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING CS6212 PROGRAMMING AND DATA STRUCTURES I LABORATORY II SEMESTER/ I YEAR VERSION 4

Transcript of DMI College of Engineering - dmice.ac.indmice.ac.in/wp-content/uploads/2017/05/PDSI.pdf · DMI...

DMI College of Engineering

(Affiliated to Anna University, Chennai-600 025)

Palanchur, Chennai-602 123.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CS6212 PROGRAMMING AND DATA STRUCTURES I

LABORATORY

II SEMESTER/ I YEAR

VERSION 4

General Instructions

Leave your footwear outside the lab.

The students should get their observations signed from the Lab in charge in the

first 15 minutes. The details in observation

AIM ALGORITHM SOURCE CODE

OUTPUT RESULT

The student should proceed with the program execution thereafter.

The student should attend the Viva Voce simultaneously.

The student should complete the program within their lab session. The Record

work of the program should be submitted in the next week.

The student should maintain strict silence within the lab.

CS6212- PROGRAMMING AND DATA STRUCTURES

LAB I SYLLABUS

OBJECTIVES:

To introduce the concepts of structured Programming language.

To introduce the concepts of pointers and files

To introduce the concepts of primitive Data Structures.

1. C Programs using Conditional and Control Statements

2. C Programs using Arrays, Strings and Pointers and Functions

3. Representation of records using Structures in C – Creation of Linked List –

Manipulation of records in a Linked List

4. File Handling in C – Sequential access – Random Access

5. Operations on a Stack and Queue – infix to postfix – simple expression evaluation

Using Stacks - Linked Stack Implementation – Linked Queue Implementation

6. Implementation of sorting algorithms

7. Implementation of Linear search and Binary Search.

CS6212- PROGRAMMING AND DATA STRUCTURES- I

LIST OF EXPERIMENTS

S.NO TOPICS

1 GREATEST OF THREE NUMBERS

2 FIBBONACCI SERIES

3 SUM AND AVERAGE OF THE GIVEN ARRAY

4 TO CHECK WHETHER THE GIVEN STRING IS PALINDROME

5 TO CALCULATE THE SUM OF THE ARRAY ELEMENT USING POINTERS AND FUNTIONS

6 IMPLEMENTATION OF SEQUENTIAL ACCESS FILE

7 IMPLEMENTATION OF RANDOM ACCESS FILE

8 IMPLEMENTATION OF SINGLY LINKED LIST

9 INFIX TO POSTFIX EXPRESSION CONVERSION

10 EVALUATION OF POSTFIX EXPRESSION

11 IMPLEMENTATION OF STACK USING LINKED LIST

12 IMPLEMENTATION OF QUEUE USING LINKED LIST

13 IMPLEMENTATION OF INSERTION SORT

14 IMPLEMENTATION OF SELECTION SORT

15 IMPLEMENTATION OF BUBBLE SORT

16 IMPLEMENTATION OF QUICK SORT

17 IMPLEMENTATION OF MERGE SORT

18 IMPLEMENTATION OF SHELL SORT

19 IMPLEMENTATION OF RADIX SORT

20 IMPLEMENTATION OF LINEAR SEARCH

21 IMPLEMENTATION OF BINARY SEARCH

CONTENT BEYOND SYLLABUS

22 IMPLEMENTATION OF DOUBLY LINKED LIST

23 ADDITION OF POLYNOMIALS USING LINKED LIST

GREATEST OF THREE NUMBERS

EX.NO:1

AIM

To write a C program to implement the greatest of three numbers. ALGORITHM

Step-1: Start the program

Step-2: Read the three numbers a,b,c.

Step-3: check for the 1st two numbers.

Step-4: If a is greater than b then proceed by checking with c

Step-5: If b is greater than a then proceed by checking with c

Step-6: Display the result

Step-7: End the program

PROGRAM

void main() { int a,b; printf(“\n Enter a and b values:”); scanf(“%d %d ”,&a,&b); if((a>b) && (a>c)) { Printf(“\n a is greater”); } else if(b>c) { printf(“\n b is greater ”) } else { printf(“ \n c is greater”); } } OUTPUT

Enter a,b,c values: 30 20 50 C is greater RESULT: Thus a c program to implement the greatest of three numbers is executed.

FIBBONACCI SERIES

EX.NO:2

AIM

To write a C program to generate the Fibonacci series ALGORITHM

Step-1: Start the program

Step-2: Enter the number

Step-3: Check the number whether the number is zero or not. If zero print Zero value. If

not zero go further.

Step-4: Set a loop up to the given number.

Step-5: fib=fib+a;

a=b;

b=c;

Step-6: Every increment in the loop prints the value of fib.

Step-7: After the execution of the loop stops the program

PROGRAM

#include<stdio.h> main() { int num,fib=0,a=0,b=1,i; printf(“Enter the number”); scanf(“%d”,&num); printf(“\n FIBBONACI SERIES\n”); if(num==0) printf(“0”); else { for(i=0;i<num;i++) { fib=fib+a; a=b; b=fib; printf(“%d\t”,fib); }}} OUTPUT

Enter the number 5 FIBONACCI SERIES 0 1 1 2 3 RESULT: Thus a c program to implement Fibonacci series is executed.

SUM AND AVERAGE OF THE GIVEN ARRAY

EX.NO:3

AIM To write a C program to find the sum and average of the given array ALGORITHM

Step-1: Start the program

Step-2: Get the number of elements to be used in the array

Step-3: Enter the element.

Step-4: Find the summation of all the elements using for loop

Step-5: Display the result

Step-6: End the program

PROGRAM

#include<stdio.h> main() { int a[100],i,no,sum=0; float avg=0; printf(“\nEnter the number of elements”); scanf(“%d”,&no); printf(“Enter the numbers”); for(i=0;i<no;i++) { scanf(“%d”,&a[i]); sum=sum+a[i]; } avg=(float)sum/no; printf(“sum=%d\naverage=%f”,sum,avg); } OUTPUT

Enter the number of elements5 Enter the numbers 1 2 3 4 5 Sum=15 Average=3.000000 RESULT: Thus a c program to find the sum and average from the given array is executed.

TO CHECK WHETHER THE GIVEN STRING IS PALINDROME

EX.NO:4

AIM To write a C program to find whether the string is palindrome or not. ALGORITHM:

Step-1: Start the program Step-2: Enter the string

Step-3: Find the string length using the strlen() function

Step-4: Print the string length

Step-5: Set a loop up to the half of the string length

Step-6: Compare every character above the middle character with the below character

of the middle character

Step-7: If any character equal prints the given string is palindrome

Step-8: If the character is not equal then print the given string is not a palindrome

Step-9: Stop

PROGRAM

#include<stdio.h> #include<stdlib.h> #include<conio.h> void main() { int len=0,i,j, count=0; char name[25]; printf(“Enter the string...”); scanf(“%s”,name); while(name[len]!=’\0') len++; printf(“\n%d”,len); for(i=0,j=len-1;i<len/2;i++,j--) { if(name[i]!=name[j]) { count++ ; if(count==0) { printf(“\nThe given string is a palindrome”); else printf(“\nThe given string is not a palindrome”); } getch( ); exit(0); } }}

OUTPUT

Enter the string...engineering 11 The given string is not a palindrome RESULT: Thus a c program to check the given string for palindrome is executed.

TO CALCULATE THE SUM OF THE ARRAY ELEMENT USING

POINTERS AND FUNTIONS EX.NO:5

AIM

To write a C program to illustrate parameter passed to the function. ALGORITHM

Step-1: Start the program

Step-2: Enter the size of the array

Step-3: Enter the elements of the array

Step-4: Print the array elements

Step-5: Call the function with base address of the array passed to it

Step-6: In the calling function gets the base address in the pointer variable

Step-7: Add the array elements

Step-8: Return the value from the calling function to the variable in the called function

Step-9: Print the sum in the called function

Step-10: End the program in the main function

PROGRAM

#include<stdio.h> main() { int a[10],i,no,sum=0; printf(“Enter the size of array...”); scanf(“%d”,&no); printf(“Enter the elements of the array...”); for(i=0;i<no;i++) scanf(“%d”,&a[i]); for(i=0;i<no;i++) printf(“\n%d”,a[i]); sum=add(&a[0],no); printf(“\nThe sum of %d numbers is...%d”,no,sum); } add(int *pt,int n) { int i,a=0; for(i=0;i<n;i++) { a=a+*pt; pt++; } return(a); }

OUTPUT Enter the size of array... 5 Enter the elements of the array... 1 2 3 4 5 1 2 3 4 5 The sum of 5 numbers is... 15

RESULT: Thus a c program to find the substring from a given string is implemented.

IMPLEMENTATION OF SEQUENTIAL ACCESS FILE

EX.NO:6

AIM

To write a C program to implement sequential access of file ALGORITHM

Step-1: Start the program

Step-2: Open the student file in writing mode

Step-3: Get the user id, name and marks of the students

Step-4: Close the file and open the file in read mode

Step-5: For the search option, user id is given. If the record is found the details of the

Student of the student is displayed else failure will be displayed.

Step-6: The display option gives the entire details of the student

Step-7: End the program in the main function

PROGRAM

#include <stdio.h> typedef struct { int usn; char name[25]; int m1,m2,m3; }STD; STD s; void display(FILE *); int search(FILE *,int); void main() { int i,n,usn_key,opn; FILE *fp; printf(" How many Records ? "); scanf("%d",&n); fp=fopen("stud.dat","w"); for(i=0;i<n;i++) { printf("Read the Info for Student: %d (uid,name,m1,m2,m3) \n",i+1); scanf("%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3); fwrite(&s,sizeof(s),1,fp); } fclose(fp); fp=fopen("stud.dat","r");

do { printf("Press 1- Display\t 2- Search\t 3- Exit\t Your Option?"); scanf("%d",&opn); switch(opn) { case 1: printf("\n Student Records in the File \n"); display(fp); break; case 2: printf(" Read the uid of the student to be searched ?"); scanf("%d",&usn_key); if(search(fp,usn_key)) { printf("Success ! Record found in the file\n"); printf("%d\t%s\t%d\t%d\t%d\n",s.usn,s.name,s.m1,s.m2,s.m3); } else printf(" Failure!! Record with USN %d not found\n",usn_key); break; case 3: printf(" Exit!! Press a key . . ."); getch(); break; default: printf(" Invalid Option!!! Try again !!!\n"); break; } }while(opn != 3); fclose(fp); } /* End of main() */ void display(FILE *fp) { rewind(fp); while(fread(&s,sizeof(s),1,fp)) printf("%d\t%s\t%d\t%d\t%d\n",s.usn,s.name,s.m1,s.m2,s.m3); } int search(FILE *fp, int usn_key) { rewind(fp); while(fread(&s,sizeof(s),1,fp)) if( s.usn == usn_key) return 1; return 0; }

OUTPUT

How many Records ?4 Read the Info for Student: 1 (uid,name,m1,m2,m3) 5 allen 68 59 40 Read the Info for Student: 2 (uid,name,m1,m2,m3) 2 briane 89 58 49 Read the Info for Student: 3 (uid,name,m1,m2,m3) 44 kernighan 44 69 58 Read the Info for Student: 4 (uid,name,m1,m2,m3) 8 weiss 95 48 69 Press 1- Display 2- Search 3- Exit Your Option?1 Student Records in the File 5 allen 68 59 40 2 briane 89 58 49 44 kernighan 44 69 58 8 weiss 95 48 69 Press 1- Display 2- Search 3- Exit Your Option?2 Read the uid of the student to be searched ?44 Success ! Record found in the file 44 kernighan 44 69 58 Press 1- Display 2- Search 3- Exit Your Option?3 Exit!! Press a key . . .

RESULT: Thus a c program for sequential access file is implemented successfully.

IMPLEMENTATION OF RANDOM ACCESS FILE

EX.NO:7

AIM

To write a C program to implement random access of files. ALGORITHM

Step-1: Start the program

Step-2: Open the file RANDOM in write mode and get the characters.

Step-3: If it is the End of File then print the number of characters entered.

Step-4: Close the file and open it in read mode.

Step-5: Find the position of each character using fseek and print it.

Step-6: Stop the program

PROGRAM

#inlcude<stdio.h> #include<conio.h> void main() { FILE *fp; long n; char c; fp=fopen(“RANDOM”,”w”),; while((c=getchar())!=EOF)

putc(c,fp); printf(“no. of characters entered = %ld \n”,ftell(fp)); fclose(fp); fp=fopen(“RANDOM”,”r”); n=OL; while(feof(fp)==0) { fseek(fp,n,0); /* position to ()n+1)th character */ printf(“Position of %c is %ld \n”, getc(fp),ftell(fp)); n=n+5L; } putchar(‘\n’); fseek(fp,-1L,2); do { putchar(getc(fp)); } while(!fseek(fp,-2L,1)); fclose(fp); }

OUTPUT

ABCDEFGHIJKLMNOPQRSTUVWXYZ No. of characters entered =26 Position of A is 0 Position of F is 5 Position of K is 10 Position of P is 15 Position of U is 20 Position of Z is 25 RESULT: Thus a C program is written to implement random access of files is executed.

IMPLEMENTATION OF SINGLY LINKED LIST EX.NO:8

AIM:

To write a C program to implement singly linked list. ALGORITHM:

Step 1: Define a list as a node of a structure with one data and one pointer

pointing to next element in the structure.

Step 2: Declare the function prototypes creation (), insertion (), deletion () and

display () to perform the list function

Step 3: Declare the necessary pointer variables as structure node data type.

Step 4: Get a choice from the user.

Step 5: If the choice is create get first data from the user by calling the function

create () and display contents of the list.

Step 6: If the choice is insert, get data and its position by calling function

insert ()and assign the next field of the given data node according to the

position. Display the contents of the list.

Step 7: If the choice is delete, get the position of data which is going to be

removed from the list and assign next field of previous node to address.

Step 8: Repeat the steps 4, 5&6 until the choice is exit.

Step 9: Terminate the execution.

PROGRAM

#include<stdio.h> #include<conio.h> #include<stdio.h> typedef struct SLL { int data; struct SLL *next; }node; node *create(); void main() { int choice,val; char ans; node *head; void display(node*); node *search(node*,int); void insert(node*); void Delete(node**); head=NULL;

do { clrscr(); printf("Program to perform various operations:"); printf("\n1.Create\n2.Display\n3.Search\n4.Insert\n5.Delete\n6.Exit"); printf("\nEnter your choice "); scanf("%d",&choice); switch(choice) { case 1: head=create();break; case 2: display(head);break; case 3: printf("Enter the element to be searched"); scanf("%d",&val); search(head,val);break; case 4: insert(head);break; case 5: Delete(&head);break; case 6: exit(0); default : clrscr(); printf("Invalid choice"); getch(); } }while(choice!=6); } node *create() { node *temp,*New,*head; int val,flag; char ans='y'; node *get_node(); temp=NULL; flag=1; do { printf("\nEnter your element:"); scanf("%d",&val); New=get_node(); if(New==NULL) printf("\nMemory not allocated");

New->data=val; if(flag) { head=New; temp=head; flag=0; } else { temp->next=New; temp=New; } printf("\nDo you want to enter more elements?(y/n)"); ans=getch(); }while(ans=='y'); printf("\nSLL is created"); getch(); clrscr(); return head; } node *get_node() { node *temp; temp=(node*)malloc(sizeof(node)); temp->next=NULL; return temp; } void display(node *head) { node *temp; temp=head; if(temp==NULL) { printf("\nList empty"); getch(); clrscr(); return; } while(temp!=NULL) { printf("%d->",temp->data); temp=temp->next; } printf("NULL"); getch();

clrscr(); } node *search(node *head,int key) { node *temp; int found; temp=head; if(temp==NULL) { printf("Linked list empty\n"); getch(); clrscr(); return NULL; }found=0; while(temp!=NULL&&!found) { if(temp->data!=key) temp=temp->next; else found=1; } if(found) { printf("Elements are present in list\n"); getch(); return temp; } else { printf("\nElements are not present"); getch(); return NULL; } } void insert(node *head) { node *temp,*New; int val; temp=head; if(temp==NULL) { printf("\nInsertion impossible\n"); getch(); return; } clrscr();

printf("\nEnter element after whch you want to insert"); scanf("%d",&val); temp=search(head,val); if(temp!=NULL) { printf("\nEnter the element to be inserted"); scanf("%d",&val); New=(node*)malloc(sizeof(node)); if(New==NULL) printf("memory not allocaed\n"); New->data=val; New->next=NULL; New->next=temp->next; temp->next=New; printf("\nThe element is inserted\n"); getch(); clrscr(); } } node *get_prev(node *head,int val) { node *temp,*prev; int flag; temp=head; if(temp==NULL) return 0; flag=0; prev=NULL; while(temp!=NULL&&!flag) { if(temp->data!=val) { prev=temp; temp=temp->next; } else flag=1; } if(flag) return prev; else return NULL; } void Delete(node **head) { node *temp,*prev;

int key; temp=*head; if(temp==NULL){ printf("\nThe list is empty \n"); getch(); clrscr(); return; } clrscr(); printf("\nEnter the elements to be deleted"); scanf("%d",&key); temp=search(*head,key); if(temp!=NULL) { prev=get_prev(*head,key); if(prev!=NULL) { prev->next=temp->next; free(temp); } else { *head=temp->next; free(temp);} printf("\nThe element is deleted\n"); getch(); clrscr(); } }

OUTPUT

Program to perform various operations: 1.Create 2.Display 3.Search 4.Insert 5.Delete 6.Exit Enter your choice 1 Enter your element:10 Do you want to enter more elements?(y/n) Enter your element:20 Do you want to enter more elements?(y/n) Enter your element:30 Do you want to enter more elements?(y/n) SLL is created Enter your choice 2 10->20->30->NULL Enter your choice 3 Enter the element to be searched20 Elements are present in list Enter your choice 4 Enter element after which you want to insert20 Elements are present in list Enter the element to be inserted25 The element is inserted Enter your choice 5 Enter the elements to be deleted25 Elements are present in list The element is deleted Enter your choice 6

RESULT: Thus a C program is written to implement singly linked list and executed successfully

Infix to Postfix Expression Conversion EX.NO:9

AIM:

To write a C program to convert the infix expression into postfix expression using stack. ALGORITHM:

Step 1: Start the program.

Step 2: Get the infix expression as input.

Step 3: Read the input from left to right.

Step 4: If the input is operand then place it in the postfix expression.

Step 5: Else if the input symbol is an operator then check for the operator type and

also the precedence, pop entries from the stack and place them in the

postfix expression until the lowest priority operator is encountered.

Step 6: ‘(‘symbol will be popped from stack only when we get a ‘)’ symbol.

Step 7: When the input is completely read then pop the elements in stack until it

becomes empty.

Step 8: Display the postfix expression.

Step 9: Stop the program.

PROGRAM

#include<stdio.h> #include<conio.h> #include<alloc.h> int top=0,st[20]; char inf[40],post[40]; void postfix(); void push(int); char pop(); void main() { clrscr(); printf("Enter the infix expression:"); scanf("%s",inf); postfix(); getch(); } void postfix() {int i,j=0; for(i=0;inf[i]!=0;i++) {switch(inf[i])

{ case '+':while(st[top]>=1) post[j++]=pop(); push(1); break; case '-':while(st[top]>=1) post[j++]=pop(); push(2); break; case '*':while(st[top]>=3) post[j++]=pop(); push(3); break; case '/':while(st[top]>=4) post[j++]=pop(); push(4); break; case '^': post[j++]=pop(); push(5); break; case '(':push(0); break; case ')':while(st[top]!=0) post[j++]=pop(); top--; break; default: post[j++]=inf[i]; }} while(top>0) post[j++]=pop(); printf("\nPostfix expression is =>\n\t\t%s",post); }void push(int ele) { top++; st[top]=ele; }char pop() {int el; char e; el=st[top]; top--; switch(el) {case 1: e='+'; break;

case 2: e='-'; break; case 3: e='*'; break; case 4: e='/'; break; case 5: e='^'; break; }return(e); }

OUTPUT

Enter the infix expression:((a+b)*(c+d)*(e/f)^g) Postfix expression is => ab+cd+*ef/*g^

RESULT: Thus a C program is written to convert infix expression into a postfix expression and executed successfully.

Evaluation of Postfix Expression EX.NO:10

AIM:

To write a C program to evaluate the postfix expression ALGORITHM:

Step 1: Start the program.

Step 2: Read the postfix expression from left to right

Step 3: If the symbol read is an operand then push it onto the stack

Step 4: If the operator is read POP two operands and perform arithmetic

operations if operator is

+ then result=operand 1 + operand 2

- then result=operand 1 - operand 2

* then result=operand 1 * operand 2

/ then result=operand 1 / operand 2

Step 5: Push the result onto the stack

Step 6: Repeat steps 2-5 till the postfix expression is not over

Step 7: Stop the program.

PROGRAM

#include <stdio.h> #include <string.h> int top = -1; int stack[100]; /* push the given data into the stack */ void push (int data) { stack[++top] = data; } /* Pop the top element from the stack */ int pop () { int data; if (top == -1) return -1; data = stack[top]; stack[top] = 0; top--; return (data); } int main() { char str[100]; int i, data = -1, operand1, operand2, result; /* Get the postfix expression from the user */

printf("Enter ur postfix expression:"); fgets(str, 100, stdin); for (i = 0; i < strlen(str); i++) { if (isdigit(str[i])) { /* * if the i/p char is digit, parse * character by character to get * complete operand */ data = (data == -1) ? 0 : data; data = (data * 10) + (str[i] - 48); continue; } if (data != -1) { /* if the i/p is operand, push it into the stack */ push(data); } if (str[i] == '+' || str[i] == '-'|| str[i] == '*' || str[i] == '/') { /* * if the i/p is an operator, pop 2 elements * from the stack and apply the operator */ operand2 = pop(); operand1 = pop(); if (operand1 == -1 || operand2 == -1) break; switch (str[i]) { case '+': result = operand1 + operand2; /* push the result into the stack */ push(result); break; case '-': result = operand1 - operand2; push(result); break; case '*': result = operand1 * operand2; push(result); break; case '/':

result = operand1 / operand2; push(result); break; } } data = -1; } if (top == 0) printf("The answer is:%d\n", stack[top]); else printf("u have given wrong postfix expression\n"); return 0; }

OUTPUT

Enter you postfix expression: 10 20 * 30 40 10 /-+ The answer is: 226

Result: Thus a C program is written to evaluate postfix expression and executed successfully

Implementation of Stack Using Linked List EX.NO:11

AIM:

To write a c program to implement stack using linked list ALGORITHM:

Step 1: Start the program.

Step 2: For Push operation, check for stack overflow

Step 3: If Top>=N then print stack overflow else increment Top and insert the

element.

Step 4: For Pop operation, check for underflow of the stack.

Step 5: If Top=0 then print stack underflow else decrement Top and delete the

element

Step 6: Stop the program.

PROGRAM

#include <stdio.h> #include <stdlib.h> struct Node { int Data; struct Node *next; }*top; void popStack() { struct Node *temp, *var=top; if(var==top) { top = top->next; free(var); } else printf("\nStack Empty"); } void push(int value) { struct Node *temp; temp=(struct Node *)malloc(sizeof(struct Node)); temp->Data=value; if (top == NULL) { top=temp; top->next=NULL; }

else { temp->next=top; top=temp; } } void display() { struct Node *var=top; if(var!=NULL) { printf("\nElements are as:\n"); while(var!=NULL) { printf("\t%d\n",var->Data); var=var->next; } printf("\n"); } else printf("\nStack is Empty"); } int main() { int i=0; top=NULL; clrscr(); printf(" \n1. Push to stack"); printf(" \n2. Pop from Stack"); printf(" \n3. Display data of Stack"); printf(" \n4. Exit\n"); while(1) { printf(" \nChoose Option: "); scanf("%d",&i); switch(i) { case 1: { int value; printf("\nEnter a value to push into Stack: "); scanf("%d",&value); push(value); break; } case 2:

{ popStack(); printf("\n The last element is popped"); break; } case 3: { display(); break; } case 4: { struct Node *temp; while(top!=NULL) { temp = top->next; free(top); top=temp; } exit(0); } default: { printf("\nwrong choice for operation"); }}}}

OUTPUT

1. Push to stack 2. Pop from Stack 3. Display data of Stack 4. Exit\ Choose Option:1 Enter a value to push into Stack 5 Choose Option:1 Enter a value to push into Stack 3 Choose Option:1 Enter a value to push into Stack 2 Choose Option:1 Enter a value to push into Stack 9 Choose Option:3 Elements are as : 5 3 2 9 Choose Option:2 The last element is popped Choose Option:3 Elements are as : 3 2 9

RESULT: Thus a C program is written to implement stack using linked list and executed successfully

Implementation of Queue using Linked List EX.NO:12

AIM:

To write a C program to implement queue using linked list ALGORITHM:

Step 1: Start the program.

Step 2: For queue insertion operation, check for queue overflow

Step 3: If R>=N then print queue overflow else increment rear pointer and insert

the element.

Step 4: For queue deletion operation, check for underflow of the queue.

Step 5: If F=0 then print queue underflow else delete the element and increment

the front pointer

Step 6: Stop the program.

PROGRAM

#include<stdio.h > #include<conio.h > #include<alloc.h > struct queue { int data; struct queue *next; }; struct queue *addq(struct queue *front); struct queue *delq(struct queue *front); void main() { struct queue *front; int reply,option,data; clrscr(); front=NULL; do { printf("\n1.addq"); printf("\n2.delq"); printf("\n3.exit"); printf("\nSelect the option"); scanf("%d",&option); switch(option) { case 1 : //addq front=addq(front); printf("\n The element is added into the queue");

break; case 2 : //delq front=delq(front); break; case 3 : exit(0); } }while(1); } struct queue *addq(struct queue *front) { struct queue *c,*r; //create new node c=(struct queue*)malloc(sizeof(struct queue)); if(c==NULL) { printf("Insufficient memory"); return(front); } //read an insert value from console printf("\nEnter data"); scanf("%d",&c->data); c->next=NULL; if(front==NULL) { front=c; } else { //insert new node after last node r=front; while(r->next!=NULL) { r=r->next; }} return(front); } struct queue *delq(struct queue *front) { struct queue *c; if(front==NULL) { printf("Queue is empty"); return(front); } //print the content of first node printf("Deleted data:%d",front->data);

//delete first node c=front; front=front->next; free(c); return(front); }

OUTPUT

1.addq 2.delq 3.exit Select the option 1 Enter data 8 1.addq 2.delq 3.exit Select the option 1 Enter data 5 1.addq 2.delq 3.exit Select the option 1 Enter data 9 1.addq 2.delq 3.exit Select the option 1 Enter data 1 1.addq 2.delq 3.exit Select the option 2 Deleted data: 8 1.addq 2.delq 3.exit Select the option 3

RESULT: Thus a C program is written to implement queue using linked list and executed successfully

Implementation of Insertion Sort EX.NO:13

AIM:

To write a C program to implement insertion sort ALGORITHM:

Step 1: Start the program.

Step 2: The second element of an array is compared with the elements that

appears before it (only first element in this case). If the second element is

smaller than first element, second element is inserted in the position of

first element. After first step, first two elements of an array will be sorted.

Step 3: The third element of an array is compared with the elements that appears

before it (first and second element). If third element is smaller than first

element, it is inserted in the position of first element. If third element is

larger than first element but, smaller than second element, it is inserted in

the position of second element. If third element is larger than both the

elements, it is kept in the position as it is. After second step, first three

elements of an array will be sorted.

Step 3: Similarly, the fourth element of an array is compared with the elements

that appears before it (first, second and third element) and the same

procedure is applied and that element is inserted in the proper position.

After third step, first four elements of an array will be sorted.

Step 4: If there are n elements to be sorted. Then, this procedure is repeated n-

1 times to get sorted list of array

Step 5: Stop the program

PROGRAM

#include<stdio.h> int main(){ int i,j,s,temp,a[20]; printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=1;i<s;i++) { temp=a[i]; j=i-1; while((temp<a[j])&&(j>=0)) { a[j+1]=a[j];

j=j-1; } a[j+1]=temp; } printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; } OUTPUT

Enter total elements: 5 Enter 5 elements: 3 7 9 0 2 After sorting: 0 2 3 7 9

RESULT: Thus a C program is written to implement insertion sort and executed successfully

Implementation of Selection Sort EX.NO:14

AIM:

To write a C program to implement selection sort ALGORITHM:

Step 1: Start the program.

Step 2: Selection sort algorithm starts by comparing first two elements of an array

and swapping if necessary.

Step 3: If you want to sort the elements of array in ascending order and if the first

element is greater than second then, you need to swap the elements.

Step 4:But, if the first element is smaller than second, leave the elements as it is.

Step 5: Then, again first element and third element are compared and swapped if

necessary.

Step 6: This process goes on until first and last element of an array is compared.

This completes the first step of selection sort.

Step 7:If there are n elements to be sorted then, the process mentioned above

should be repeated n-1 times to get required result.

Step 8: Stop the program

PROGRAM

#include<stdio.h> int main() { int s,i,j,temp,a[20]; printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=0;i<s;i++) { for(j=i+1;j<s;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("After sorting is: ");

for(i=0;i<s;i++) printf(" %d",a[i]); return 0; } OUTPUT

Enter total elements: 5 Enter 5 elements: 4 5 0 21 7 The array after sorting is: 0 4 5 7 21

RESULT: Thus a C program is written to implement selection sort and executed successfully

Implementation of Bubble Sort EX.NO:15

AIM:

To write a C program to implement bubble sort ALGORITHM:

Step 1: Start the program.

Step 2: Bubble sort algorithm starts by comparing the first two elements of an

array and swapping if necessary,

Step 3: if you want to sort the elements of array in ascending order and if the first

element is greater than second then, you need to swap the elements but, if

the first element is smaller than second, you mustn't swap the element.

Step 4: Then, again second and third elements are compared and swapped if it is

necessary and this process go on until last and second last element is

compared and swapped. This completes the first step of bubble sort.

Step 5: If there are n elements to be sorted then, the process mentioned above

should be repeated n-1 times to get required result.

Step 6: Exit

PROGRAM

#include<stdio.h> int main() { int s,temp,i,j,a[20]; printf("Enter total numbers of elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); //Bubble sorting ALGORITHM for(i=s-2;i>=0;i--) { for(j=0;j<=i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } }

printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; } OUTPUT

Enter total numbers of elements: 5 Enter 5 elements: 6 2 0 11 9 After sorting: 0 2 6 9 11

RESULT: Thus a C program is written to implement bubble sort and executed successfully

Implementation of Quick Sort EX.NO:16

AIM:

To write a C program to implement Quick sort ALGORITHM:

Step 1: Start the program.

Step 2: Declare the function quicksort(array)

Step 3: less, equal, greater := three empty arrays

Step 4: if length(array) > 1

Step 5: Then pivot := select any element of array

Step 6: for each x in array

if x < pivot then add x to less

if x = pivot then add x to equal

if x > pivot then add x to greater

quicksort(less)

quicksort(greater)

Step 7: array := concatenate(less, equal, greater)

Step 8: Stop the program.

PROGRAM

#include<stdio.h> void quicksort(int [10],int,int); int main() { int x[20],size,i; printf("Enter size of the array: "); scanf("%d",&size); printf("Enter %d elements: ",size); for(i=0;i<size;i++) scanf("%d",&x[i]); quicksort(x,0,size-1); printf("Sorted elements: "); for(i=0;i<size;i++) printf(" %d",x[i]); return 0; } void quicksort(int x[10],int first,int last) { int pivot,j,temp,i; if(first<last) { pivot=first;

i=first; j=last; while(i<j) { while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot]) j--; if(i<j) { temp=x[i]; x[i]=x[j]; x[j]=temp; } } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); quicksort(x,j+1,last); } } OUTPUT

Enter size of the array: 5 Enter 5 elements: 3 8 0 1 2 Sorted elements: 0 1 2 3 8

RESULT: Thus a C program is written to implement quick sort and executed successfully

Implementation of Merge Sort EX.NO:17

AIM:

To write a C program to implement Merge sort ALGORITHM:

Step 1: Start the program.

Step 2: To sort A[p .. r]: Divide Step: If a given array A has zero or one element,

simply return; it is already sorted. Otherwise, split A[p .. r] into two

sub arrays A[p .. q] and A[q + 1 .. r], each containing about half of the

elements of A[p .. r]. That is, q is the halfway point of A[p .. r].

Step 3: Conquer Step: Conquer by recursively sorting the two sub arrays A[p .. q]

and A[q + 1 .. r].

Step 4: Combine Step : Combine the elements back in A[p .. r] by merging the

two sorted sub arrays A[p .. q] and A[q + 1 .. r] into a sorted sequence. To

accomplish this step, we will define a procedure MERGE (A, p, q, r).

Step 5: Stop the program.

PROGRAM

#include<stdio.h> #define MAX 50 void mergeSort(int arr[],int low,int mid,int high); void partition(int arr[],int low,int high); int main() { int merge[MAX],i,n; printf("Enter the total number of elements: "); scanf("%d",&n); printf("Enter the elements which to be sort: "); for(i=0;i<n;i++) { scanf("%d",&merge[i]); } partition(merge,0,n-1); printf("After merge sorting elements are: "); for(i=0;i<n;i++) { printf("%d ",merge[i]); } return 0; } void partition(int arr[],int low,int high){ int mid;

if(low<high) { mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); mergeSort(arr,low,mid,high); } } void mergeSort(int arr[],int low,int mid,int high) { int i,m,k,l,temp[MAX]; l=low; i=low; m=mid+1; while((l<=mid)&&(m<=high)) { if(arr[l]<=arr[m]) { temp[i]=arr[l]; l++; } else { temp[i]=arr[m]; m++; } i++; } if(l>mid) { for(k=m;k<=high;k++) { temp[i]=arr[k]; i++; } } else { for(k=l;k<=mid;k++){ temp[i]=arr[k]; i++; } } for(k=low;k<=high;k++) { arr[k]=temp[k];

} } OUTPUT

Enter the total number of elements: 5 Enter the elements which to be sort: 2 5 0 9 1 After merge sorting elements are: 0 1 2 5 9

RESULT: Thus a C program is written to implement merge sort and executed successfully

Implementation of Shell Sort EX.NO:18

AIM:

To write a C program to implement shell sort ALGORITHM:

Step 1: Start the program.

Step 2: Set up a inc number. inc number is set according to given elements in the

list.

Step 3: Mark each elements which is comes in inc element.

Step 4: Sort marking elements such as smallest to greater is set as left to right and

not change remain element.

Step 5: Reduce inc number to one

Step 6: Repeat step 2,3 and 4 till all the elements not sorted.

Step 7: Stop the program.

PROGRAM

#include<stdio.h> #include<conio.h> void shell(int a[],int n); int i,j,k,n,temp,array[25]; void main() { clrscr(); printf("\n SHELL SORT"); printf("\n **********"); printf("\n Enter the limit:"); scanf("%d",&n); printf("\n Enter the elements\n\n"); for(i=0;i<n;i++) scanf("%d",&array[i]); shell(array,n); printf("\n Sorted list:"); for(i=0;i<n;i++) printf("\n %d",array[i]); getch(); } void shell(int a[],int n) { for(i=(n+1)/2;i>=1;i/=2) { for(j=i;j<=n-1;j++) { temp=a[j];

k=j-i; while(k>=0&&temp<a[k]) { a[k+i]=a[k]; k=k-i; } a[k+i]=temp; }}}

OUTPUT Enter the limit: 5

Enter the elements

5

4

3

2

1 Sorted list: 1

2

3

4

5

RESULT: Thus a C program is written to implement shell sort and executed successfully

Implementation of Radix Sort EX.NO:19

AIM:

To write a C program to implement radix sort ALGORITHM:

Step 1: Start the program.

Step 2: Each key is first figuratively dropped into one level of buckets

corresponding to the value of the rightmost digit.

Step 3: Each bucket preserves the original order of the keys as the keys are

dropped into the bucket.

Step 4: There is a one-to-one correspondence between the number of buckets and

the number of values that can be represented by a digit.

Step 5: Then, the process repeats with the next neighboring digit until there are no

more digits to process. In other words:

i) Take the least significant digit of each key.

ii) Group the keys based on that digit, but otherwise keep the

original order of keys.

iii) Repeat the grouping process with each more significant

digit.

Step 6: Stop the program.

PROGRAM

#include<stdio.h> #include<conio.h> radix_sort(int array[], int n); void main() { int array[100],n,i; clrscr(); printf("Enter the number of elements to be sorted: "); scanf("%d",&n); printf("\nEnter the elements to be sorted: \n"); for(i = 0 ; i < n ; i++ ) { printf("\tArray[%d] = ",i); scanf("%d",&array[i]); } printf("\nArray Before Radix Sort:"); //Array Before Radix Sort for(i = 0; i < n; i++) { printf("%8d", array[i]); }

printf("\n"); radix_sort(array,n); printf("\nArray After Radix Sort: "); //Array After Radix Sort for(i = 0; i < n; i++) { printf("%8d", array[i]); } printf("\n"); getch(); } radix_sort(int arr[], int n) { int bucket[10][5],buck[10],b[10]; int i,j,k,l,num,div,large,passes; div=1; num=0; large=arr[0]; for(i=0 ; i<n ; i++) { if(arr[i] > large) { large = arr[i]; } while(large > 0) { num++; large = large/10; } for(passes=0 ; passes<num ; passes++) { for(k=0 ; k<10 ; k++) { buck[k] = 0; } for(i=0 ; i<n ;i++) { l = ((arr[i]/div)%10); bucket[l][buck[l]++] = arr[i]; } i=0; for(k=0 ; k<10 ; k++) { for(j=0 ; j<buck[k] ; j++) { arr[i++] = bucket[k][j]; }

} div*=10; }}} OUTPUT

Enter the number of elements to be sorted:7 Enter the elements to be sorted: Array [0] = 777 Array [1] = 269 Array [2] = 158 Array [3] = 341 Array [4] = 265 Array [5] = 989 Array [6] = 506 Array Before Radix Sort: 777 269 158 341 265 989 506 Array After Radix Sort: 158 265 269 341 506 777 989 RESULT: Thus a C program is written to implement radix sort and executed successfully

Implementation of Linear Search EX.NO:20

AIM:

To write a C program to implement Linear Search ALGORITHM:

Step 1: Start the program.

Step 2: Get the total number of elements to be entered n

Step 3: Setup a for loop c=0 ;until c<n; increment i

Step 4: Read the numbers into the array

Step 5: Read which element to be searched in search

Step 6: Setup a for loop c=0 ;until c<n; increment i

Step 7: Evaluate if(search==array[i])

If it is found then print the number and its position

Step 8: Stop the program.

PROGRAM

#include <stdio.h> int main() { int array[100], search, c, n; printf("Enter the number of elements in array\n"); scanf("%d",&n); printf("Enter %d integer(s)\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the number to search\n"); scanf("%d", &search); for (c = 0; c < n; c++) { if (array[c] == search) /* if required element found */ { printf("%d is present at location %d.\n", search, c+1); break; } } if (c == n) printf("%d is not present in array.\n", search); return 0; }

OUTPUT

Enter the number of elements in array 5 Enter the numbers 5 6 4 2 9 Enter the number to search 4 4 is present at location 3

RESULT: Thus a C program is written to implement linear search and executed successfully

Implementation of Binary search EX.NO:21

AIM:

To write a C program to implement Binary search ALGORITHM:

Step 1: Start the program.

Step 2: Read the upper limit of the array as n

Step 3: Setup for loop from i=1 until i<n increment i

Step 4: Read element to array[]

Step 5: Read element to be searched in the array as search

Step 6: Assign first=0,last=n-1

Step 7: Repeat step 8 to 9 until (first<=last)

Step 8: middle=(first+last)/2

Step 9: if(search<a[middle])

True: n=middle=1

False : if(search>a[middle])

True:first=middle+1

False: found the element in middle+1 position

Step 10: Stop the program.

PROGRAM

#include<stdio.h> #include<conio.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter %d integers in ascending order\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter value to find\n"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) {

printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list.\n", search); getch(); return 0; } OUTPUT

Enter the number of elements 5 Enter 6 integers 1 2 3 4 5 Enter the value to find 5 5 in found in the location 5 RESULT: Thus a C program is written to binary search and executed successfully

Implementation of Doubly Linked List EX.NO:22

AIM:

To write a C program to implement doubly linked list. ALGORITHM:

Step 1: Define a list as a node of a structure with one data, one pointer

pointing to next element in the structure and another pointer pointing to

previous element in the structure.

Step 2: Declare the function prototypes creation (), insertion (), deletion () and

display () to perform the list function.

Step 3: Declare the necessary pointer variables as structure node data type.

Step 4: Get a choice from the user.

Step 5: If the choice is create get first data from the user by calling the function

create () and display contents of the list.

Step 6: If the choice is insert, get data and its position by calling function

insert (), assign the next and previous field of the given data node

according to the position. Display the contents of the list.

Step 7: If the choice is delete, get the position of data which is going to be

removed from the list and assign next field of previous node to address.

Step 8: Repeat the steps 4, 5&6 until the choice is exit.

Step 9: Terminate the execution.

PROGRAM

#include<stdio.h> #include<stdlib.h> #include<conio.h> struct node { int data; struct node*next,*prev; }*New,*New1,*temp,*start,*dummy; void add(); struct node *get_node(); void display(); void Delete(); int find(int num); int first=1; void main() { char ans='y'; int choice,num,found,flag=0; start=NULL;

do { clrscr(); printf("\nProgram for DLL\n"); printf("\n1.Insert\n2.Delete\n3.Diplay\n4.Search\n5.Exit\n"); printf("Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: add();break; case 2: Delete();break; case 3: display();break; case 4: printf("\nEnter the number to be searched"); scanf("%d",&num); temp=start; while((temp!=NULL)&&(flag==0)) { found=find(num); flag=found; } if(found==1) printf("\nThe number is present\n"); else printf("\nThe number is not present\n"); break; case 5: exit(0);break; default: printf("\nInvalid option");break; } printf("\nDo you want to continue?(y/n)"); ans=getche(); }while(ans=='y'||ans=='Y'); getch(); } void add() { clrscr(); New=get_node(); printf("\nEnter the element"); scanf("%d",&New->data); if(first==1)

{ start=New; first=0; } else { dummy=start; while(dummy->next!=NULL) dummy=dummy->next; dummy->next=New; New->prev=dummy; } } struct node *get_node() { New1=malloc(sizeof(struct node)); New1->next=NULL; New1->prev=NULL; return(New1); } void display() { clrscr(); temp=start; if(temp==NULL) printf("\nThe DLL is empty\n"); else { while(temp!=NULL) { printf("\n\n%d=>",temp->data); temp=temp->next; } printf("NULL"); } getch(); } int find(int num) { if(temp->data==num) return 1; else temp=temp->next; return 0; } void Delete()

{ int num,flag=0; int found; int last=0; clrscr(); temp=start; if(temp==NULL) printf("\nDLL is not created"); else { printf("Enter the number to be deleted"); scanf("%d",&num); while((flag==0)&&(temp!=NULL)) { found=find(num); flag=found; }if(found==0) printf("\nNumber not found"); else {if(temp==start) { start=start->next; temp->next=NULL; start->prev=NULL; free(temp); getch(); printf("\nThe strating node is deleted"); }else {if(temp->next==NULL) last=1; else last=0; (temp->next)->prev=temp->prev; (temp->prev)->next=temp->next; temp->prev=NULL; temp->next=NULL; free(temp); if(last) printf("\nLast node is deleted"); else printf("\nIntermediate node is deleted"); }}}}

OUTPUT Program for DLL 1.Insert 2.Delete 3.Diplay 4.Search 5.Exit Enter your choice:1 Enter the element10 Do you want to continue?(y/n) Enter your choice:1 Enter the element20 Do you want to continue?(y/n) Enter your choice:2 Enter the number to be deleted20 Last node is deleted Do you want to continue?(y/n) Enter your choice:3 10=>NULL Do you want to continue?(y/n) Enter your choice:4 Enter your choice:4 Enter the number to be searched10 The number is present Do you want to continue?(y/n) Enter your choice:5

RESULT: Thus a C program is written to implement doubly linked list and executed successfully.

Addition of Polynomials Using Linked list EX.NO:23

AIM:

To write a C program to perform the addition of two given polynomials using linked list. ALGORITHM:

Step 1: Start the program.

Step 2: Declare the structure with two data fields; one coefficient field, one

exponent field and one pointer to point the next element in the linked list.

Step 3: Get the coefficients and exponents of polynomials as input.

Step 4: For the given polynomials create node for each term using malloc and

link each term with the next term of the same polynomial.

Step 5: Point last term of each polynomial to NULL.

Step 6: Compare the exponent terms of the polynomials one by one.

i) If they are same add the respective coefficients and then create node and

place them in the resultant polynomial

ii) Else if they do not match with any term then place the term as such in

the resultant polynomial.

Step 7: Display the resultant polynomial

Step 8: Stop the program.

PROGRAM

#include<stdio.h> #include<conio.h> #include<process.h> #include<alloc.h> #include<stdlib.h> typedef struct polynode { float coef; int exp; struct polynode *next; }p; void main() { p *p1,*p2,*p3; p *getpoly(); p *add(p*,p*); void display(p*); clrscr(); printf("\n Enter the 1st polynomial:"); p1=getpoly();

clrscr(); printf("\n Enter the 2nd polynomial:"); p2=getpoly(); clrscr(); printf("\n The 1st poly is:"); display(p1); printf("\n The 2nd poly is:"); display(p2); p3=add(p1,p2); printf("\nAddition of polynomial is:"); display(p3); exit(0); } p *getpoly() { p *temp,*New,*last; int Exp,flag; float Coef; p *getnode(); char ans='y'; temp=NULL; flag=1; printf("\n Enter the polynomial in descending order of exponent:"); do { printf("\nEnter the coeff and expo of a term:"); scanf("%f%d",&Coef,&Exp); New=getnode(); if(New==NULL) printf("\n Memory not allocated:"); New->coef=Coef; New->exp=Exp; if(flag==1) { temp=New; last=temp; flag=0; } else { last->next=New; last=New; } printf("\nDo you want to add more terms(y/n)?"); ans=getch(); }while(ans=='y');

return(temp); } p *getnode() { p *temp; temp=(p*)malloc(sizeof(p)); temp->next=NULL; return(temp); } void display(p *head) { p *temp; temp=head; if(temp==NULL) { printf("\n The polynomial is empty"); getch(); return; } printf("\n"); while(temp->next!=NULL) { printf("%0.1fx^%d+",temp->coef,temp->exp); temp=temp->next; } printf("%0.1fx^%d",temp->coef,temp->exp); getch(); } p *add(p* one,p* two) { p *p1,*p2,*temp,*dummy; char ch; float Coef; p *append(int,float,p*); p1=one; p2=two; temp=(p*)malloc(sizeof(p)); if(temp==NULL) printf("\nMemory not allocated"); dummy=temp; while(p1!=NULL&&p2!=NULL) { if(p1->exp==p2->exp) { Coef=p1->coef+p2->coef; temp=append(p1->exp,Coef,temp);

p1=p1->next; p2=p2->next; } else if(p1->exp<p2->exp) { Coef=p2->coef; temp=append(p2->exp,Coef,temp); p2=p2->next; } else if(p1->exp>p2->exp) { Coef=p1->coef; temp=append(p1->exp,Coef,temp); p1=p1->next; }} while(p1!=NULL) { temp=append(p1->exp,p1->coef,temp); p1=p1->next; } while(p2!=NULL) { temp=append(p2->exp,p2->coef,temp); p2=p2->next; } temp->next=NULL; temp=dummy->next; free(dummy); return(temp); } p *append(int Exp,float Coef,p *temp) { p *New,*dummy; New=(p*)malloc(sizeof(p)); if(New==NULL) printf("\nMemory not alloated\n"); New->exp=Exp; New->coef=Coef; New->next=NULL; dummy=temp; dummy->next=New; dummy=New; return(dummy); }

OUTPUT

Enter the 1st polynomial: Enter the polynomial in descending order of exponent: Enter the coeff and expo of a term:3 1 Do you want to add more terms(y/n)? Enter the coeff and expo of a term:4 0 Do you want to add more terms(y/n)? Enter the 2nd polynomial: Enter the polynomial in descending order of exponent: Enter the coeff and expo of a term:4 1 Do you want to add more terms(y/n)? Enter the coeff and expo of a term:6 0 Do you want to add more terms(y/n)? The 1st poly is: 3.0x^1+4.0x^0 The 2nd poly is: 4.0x^1+6.0x^0 Addition of polynomial is: 7.0x^1+10.0x^0

RESULT: Thus a C program is written to implement polynomial addition using linked list and executed successfully