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

C LAB without front page

The document contains a series of C programming experiments, each with an aim, algorithm, program code, output, and result. It covers various topics such as calculating the area of a circle, average of numbers, even or odd determination, Fibonacci series generation, sum of digits, factorial calculation using recursion, array element summation, maximum number using pointers, student result calculation using structures, and file handling. Each experiment is successfully executed and demonstrates fundamental programming concepts.

Uploaded by

Subramanian R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C LAB without front page

The document contains a series of C programming experiments, each with an aim, algorithm, program code, output, and result. It covers various topics such as calculating the area of a circle, average of numbers, even or odd determination, Fibonacci series generation, sum of digits, factorial calculation using recursion, array element summation, maximum number using pointers, student result calculation using structures, and file handling. Each experiment is successfully executed and demonstrates fundamental programming concepts.

Uploaded by

Subramanian R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

CONTENTS

S.No Date Title of the Experiment Page No. Remarks

3
S.No Date Title of the Experiment Page No. Remarks

4
Ex. No. 1a
C PROGRAMMING USING STATEMENTS
Date:

AIM:

To write a C program using statements to find the Area of the circle.


ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables

Step 3: Get the radius from the user.


Step 4: Calculate area=3.14*radius*radius

Step 5: Print the area of the circle.


Step 6: Stop

PROGRAM :
Area of Circle
#include<stdio.h>
#include<conio.h>
void main()
{
float r, area;clrscr();
printf("Enter the Radius of the circle:\n");printf("\n Radius=");
scanf("%f",&r);
area=3.14*r*r;
printf("\n Area=%f",area);getch();
}

OUTPUT :
Enter the Radius of the circle:
Radius=5
Area=78.500000

RESULT:
Thus the C program has been written to find the area of the circle and executed
successfully.

5
Ex. No. 1b
C PROGRAMMING USING EXPRESSIONS
Date:

AIM:
To write a C program using expressions to calculate the average of given five numbers.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare five variables to get the marks.

Step 3: Read the input of five marks.

Step 4: Calculate the sum of five numbers.


Step 5: Divide the sum by 5
Step 6: Display the average.
Step 7: Stop.
PROGRAM :
#include<stdio.h>
#include<conio.h>

void main()

{
int m1,m2,m3,m4,m5;
clrscr();
float avg, tot;
printf("Enter the 5 marks\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
tot=m1+m2+m3+m4+m5;
avg=tot/5;
printf("\n The average is %.2f\n",avg);
getch();
}

6
OUTPUT:
Enter the 5 marks
99
35
65
78
35
The average is 62.40

RESULT:
Thus the average of five numbers program is written using C and has been executed
successfully.

7
Ex. No. 1c
C PROGRAMMING USING DECISION MAKING
Date:

AIM:
To write a C program to find the given number is even or odd. .
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variable of respective data type.n->integer
Step 3: Get the value for n
Step 4: If (n%2= 0)
print n is Even Else print n is odd
Step 5: Stop.
PROGRAM :
#include<stdio.h>
#include<conio.h>

void main()
{
int n;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
if(n%2==0)
printf("\n The number %d is Even..!",n);
else
printf("\n The number %d is Odd..!",n);
getch();
}
OUTPUT:
Enter the number: 776
The number 776 is Even..!

RESULT:
Thus the program has been written to find whether the given number is even or odd
using C and executed successfully.

8
Ex. No. 1d
C PROGRAMMING USING ITERATIVE STATEMENTS
Date:
AIM:
To write a C program to generate fibonacci series.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare the variables of respective data type.n,a,b,c->integer
Step 3: Get the value for n
Step 4: Set i=0
Step 5: if(i<n) then go to step 6Else goto step 10
Step 6: Calculate c=a+b
Step 7: Print c
Step 8: Set a=b, b=c, i=i+1;
Step 9: Goto step 5
Step 10: Stop.

PROGRAM :
#include<stdio.h>
#include<conio.h>

void main()
{
int n, i, c;
int a=-1,b=1;

clrscr();

printf("Enter the number:\n"); scanf("%d",&n); printf("\nFibonacci Series is :");

for(i=0; i<n; i++)


{
c=a+b;a=b; b=c;
printf("\n%d",c);
getch();
}
}

9
OUTPUT:
Enter the number: 6
Fibonacci Series is:
0
1
1
2
3
5

RESULT:
Thus the program is written to generate Fibonacci series using C and has been executed
successfully.

10
Ex. No. 1e
C PROGRAMMING USING ITERATIVE STATEMENTS
Date:

AIM:
To write a C program to find the sum of digits.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables of respective data type.n, r, sum->integer.

Step 3: Set sum=0

Step 4: If n>0 then


Step 5: Calculate r=n%10Sum=sum+r n=n/10
Step 6: Print sum
Step 7: Stop

PROGRAM :
/*Sum of Digits*/
#include<stdio.h>
#include<conio.h>void
main()
{
int n, r, sum=0;clrscr();
printf("Enter the number:\n");scanf("%d",&n);
while(n >0)
{
r=n%10;
sum=sum+r;n=n/10;
}
printf("\nSum of digits =%d", sum);getch();
}

11
OUTPUT:
Enter the number:
76
Sum of digits=13

RESULT:
Thus the sum of digits program is written using C and has been executed successfully.

12
Ex. No. 2a
C PROGRAMMING USING FUNCTIONS
Date:

Aim:
To write a C program to find the factorial of a number using recursion.

Algorithm:
Step1: Start
Step 2 : Enter the number

Step 3 : Call the factorial recursive function by passing the number


Step 4 : Print the result
Step 5: Stop.
Recursive Function:
Step 1 : Start the function
Step 2 : Assign fact=1
Step 3 : If num equals 1 then factorial is 1
Step 4 : Calculate fact = x* fact(x-1)
Step 5 : Return fact value

Program:
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n;
clrscr();
printf(“Enter the number :\n”);
scanf(“%d”,&n);
printf(“factorial of %d = %d”,n,fact(n));
getch();
}
int fact(int x)
{
int i;
if(x == 1)
return(1);
else
i = x*fact(x-1);
return(i);
}

Output:
Enter the number : 5
factorial of 5 = 120
13
Result:
Thus above C Program was executed and the output was obtained.

Ex. No. 2b
C PROGRAMMING USING ARRAY
Date:

AIM:
To write a C program to find the sum of Array elements.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare an array with necessary size.
Step 3: Get the value for total number of elements.
Step 4: Read the inputs.
Step 5: Initialize the index value and sum value.
Step 6: Increment the index value by 1
Step 7: Repeat the step 4 and 5 until index value less than total number of elements.
Step 8: Print all the elements of array.
Step 9: Print the Sum.
Step 10: Stop the Program.
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, a[10], sum=0;
clrscr();
printf("Enter the total no. of elements\n");
scanf("%d", &n);
printf("Enter Array elements one by one\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
sum=sum+a[i];
printf("The sum of Array Elements is %d \n", sum);
getch();

14
}

OUTPUT:
Enter the total no. of elements 6
Enter Array elements one by one
34
5

6
7

The sum of Array Elements is 69

RESULT:
Thus the sum of array elements is written using C and has been executed successfully.

15
Ex. No. 3a
C PROGRAMMING USING POINTER
Date:

AIM:
To write a C program to find the maximum number between two numbers using
pointers.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables fno, sno,*ptr1,*ptr2.
STEP 3: Get the values for first number and second number.
STEP 4: Compare two pointer values.
STEP 5: Print the maximum number values.
STEP 6: Stop

PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int fno, sno,*ptr1=&fno,*ptr2=&sno;
clrscr();
printf("\n\n Pointers: Find the maximum number between two numbers :\n");
printf(" Input the first number : ");
scanf("%d", ptr1);
printf(" Input the second number : ");
scanf("%d", ptr2);
if (*ptr1>*ptr2)
{
printf("\n\n %d is the maximum number.\n\n",*ptr1);
}
Else

16
{
printf("\n\n %d is the maximum number.\n\n",*ptr2);
}
getch();
}

OUTPUT:
Pointers: Find the maximum number between two numbers:
Input the first number: 5
Input the second number: 6
6 is the maximum number.

RESULT:
Thus the maximum number is found using the pointers in C program and has been
executed successfully.

17
Ex. No. 3b
C PROGRAMMING USING STRUCTURES
Date:

AIM:
To write a C program to calculate the student’s results using structures.

ALGORITHM:
Step 1: Start the program.
Step 2: Create a structure named stud and initialize the variables.
Step 3: Obtain the student’s name and his marks.
Step 4: Create the conditions for changing it to grade.
Step 5: Obtain the result.
Step 6: Stop

PROGRAM :
#include<stdio.h>
#include<conio.h>

struct student
{
int rollno;
char name[10];

int m1,m2,m3;
};
void main()
{
struct student s[10];
int total,i,n;
float avg;
clrscr();
printf("Enter the number of students:\n");
scanf("%d",&n);
for(i=0;i<n;i++)

18
{
printf("Enter the Student detail:\n");printf("\nRoll no:\t");
scanf("%d",&s[i].rollno);
printf("\nName:\t");
scanf("%s",s[i].name);
printf("\nFirst subject mark:\t");
scanf("%d",&s[i].m1);
printf("\nSecond subject mark:\t");
scanf("%d",&s[i].m2);
printf("\nThird subject mark:\t");
scanf("%d",&s[i].m3);
total=s[i].m1+s[i].m2+s[i].m3;
avg=(s[i].m1+s[i].m2+s[i].m3)/3;
printf("\nTOTAL=%d\nAVERAGE=%f",total,avg);
if((s[i].m1>=50)&&(s[i].m2>=50)&&(s[i].m3>=50))
{
if(avg>=75)
{
printf("\n DISTINCTION");
}
else if((avg>=60)&&(avg<75))
{
printf("\n FIRST CLASS");
}
else if((avg>=50)&&(avg<60))
{
printf("\n SECOND CLASS");

}
}
else

}
getch();
}

19
printf(“FAIL\n”);

OUTPUT:
Enter the number of students:
1
Enter the Student details:
Roll no:1416103
Name: RAVI

First subject mark: 92

Second subject mark: 93


Third subject mark: 94
Total=279
Average=93.000000
DISTINCTION

RESULT:
Thus the students result calculation program is written using structures in C has been
executed successfully.

20
Ex. No. 4a
IMPLEMENT C PROGRAMS USING FILES
Date:

Aim:
To write a C program to read and write a file.

Algorithm:
Step1 : Start
Step 2 : Create a file pointer
Step 3 : Read the file name to be opened.
Step 4 :Open the file with write mode.
Step 5 :Write the data
Step 6 : Open the file with read mode

Step 7 : Print the data


Step 8 : Stop.

Program:
#include<stdio.h>
#define SIZE 50
int main()
{
FILE *fptr;
char s1[SIZE], s2[SIZE];
printf(“\nEnter any string: “);
gets(s1);
fptr = fopen(“abc.txt”, “w”);
fputs(s1, fptr);
fclose(fptr);
fptr = fopen(“abc.txt”, “r”);
fgets(s2, SIZE, fptr);
printf(“\nYou entered:”);
puts(s2);
}

Output:
Enter any string:
C ProgrammingYou entered

Result:
Thus above C Program was executed and the output was obtained.

21
Ex. No. 4b
IMPLEMENT C PROGRAM USING FILES
Date:

Aim:
To write a C program to count number of characters in a file using file pointer.

Algorithm:
Step1 : Start
Step2 : Create file pointer
Step3 : Open the file with read mode
Step4 : Till the end of file reached read one character at a time
Step5 : Print the number of characters present in file.
Step6 : Close the file

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int count=0;
FILE *fptr;
clrscr();
fptr=fopen("text.txt","r");
printf("\nContents of the File is:");
while((ch=fgetc(fptr))!=EOF)
{
count++;
printf("%c",ch);
}
fclose(fptr);
printf("\nThe number of characters present in file is: %d",count);
getch();
}

Output:
Contents of the File is: Hello
The number of characters present in file is: 5

Result:
Thus above C Program was executed and the output was obtained.

22
Ex. No. 5
DEVELOPMENT OF REAL TIME C APPLICATIONS
Date:

Aim:
To develop a c program for a real time calculator application

Algorithm:
Step1 : Start
Step 2 : Display the menu
Step 3 : Read the operator
Step 4 : If the operator is +, then Read two numbers. Add two numbers, and print the
result.
Step 5 : If the operator is -, then read two numbers, subtract a-b and print the result.
Step 6 : If the operator is * then read two numbers, multiply two numbers and print the
result.
Step 7 : If the operator is / then read two numbers, divide a/b and print the result.
Step 8 : Stop the program

Program:
#include <stdio.h>
#include<conio.h>
double n1, n2, result;
void readOperands()
{
n1 = n2 = result = 0;
printf("Enter two operands: ");
scanf(" %lf %lf", & n1, & n2);
}

void main()
{
int tmp;
char oper, nxt;
clrscr();
printf("……..Basic Calculator . \n");
do
{
tmp =0;
printf("\nEnter an operator (+, -, *, /): ");
scanf(" %c", & oper);
switch (oper)
{
case '+':
readOperands();
result = n1 + n2;
break;
case '-':
readOperands();
result = n1 - n2;
break;
case '*':

23
readOperands();
result = n1 * n2;
break;
case '/':
readOperands();
result = n1 / n2;
break;
default:
tmp = 1;
printf("\nEntered operation is not available\n\n");
}
if (tmp == 0)
printf("\n%5.2f %c %5.2f= %5.2f\n\n", n1, oper, n2, result);
printf("Do you want to continue? (y/n) ");
scanf(" %c", & nxt);
}
while (nxt == 'y'||nxt == 'Y');
}

Output:

Result:
Thus above C Program was executed and the output was obtained.

24
Ex. No. 6
ARRAY IMPLEMENTATION OF LIST ADT
Date:

Aim
To perform various operations on List ADT using array implementation.

Algorithm
Step 1: Start
Step 2: Create a list of n elements
Step 3: Display list operations as a menu
Step 4: Accept user choice
Step 5: If choice = 1 then
Step 6: Get position of element to be deleted
Step 7: Move elements one position upwards thereon. Decrement length of the list
Step 8: Else if choice = 2
Step 9: Get position of element to be inserted. Increment length of the list
Step 10: Move elements one position downwards thereon Store the new element in
corresponding position
Step 11: Else if choice = 3
Step 12: Traverse the list and inspect each element Report position if it exists.
Step 13: Stop

Program
/* List operation using Arrays */
#include <stdio.h>
#include <conio.h>
void create();
void insert();
void search();
void deletion();
void display();
int i, e, n, pos;
static int b[50];
main()
{
int ch;
char g = 'y';
create();
do
{
printf("\n List Operations");
25
printf("\n 1.Deletion\n 2.Insert\n 3.Search\n 4.Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
deletion();
break;
case 2:
insert();
break;
case 3:
search();
break;
case 4:
exit(0);
default:
printf("\n Enter the correct choice:");
}
printf("Do you want to continue: ");
fflush(stdin);
scanf("\n %c",&g);
}
while(g=='y' || g=='Y');
getch();
}
void create()
{
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter list elements: ");
for(i=0; i<n; i++)
scanf("%d", &b[i]);
}
void deletion()
{
printf("\n enter the position you want to delete: ");
scanf("%d", &pos);
if(pos >= n)
printf("\n Invalid location");
else
{
for(i=pos+1; i<n; i++)
b[i-1] = b[i];
n--;
printf("List elements after deletion");
display();
}
}
void search()
{
int flag = 0;

26
printf("\n Enter the element to be searched: ");
scanf("%d", &e);
for(i=0; i<n; i++)
{
if(b[i] == e)
{
flag = 1;
printf("Element is in the %d position", i);
break;
}
}
if(flag == 0)
printf("Value %d is not in the list", e);
}
void insert()
{
printf("\n Enter the position you need to insert: ");
scanf("%d", &pos);
if(pos >= n)
printf("\n Invalid location");
else
{
++n;
for(i=n; i>pos; i--)
b[i] = b[i-1];
printf("\n Enter the element to insert: ");
scanf("%d", &e);
b[pos] = e;
}
printf("\n List after insertion:");
display();
}
void display()
{
for(i=0; i<n; i++)
printf("\n %d", b[i]);
}

27
Output
Enter the number of elements:5
Enter list elements: 12 23 34 45 56
List Operations
1. Deletion
2. Insert
3. Search
4. Exit
Enter your choice:2
Enter the position you need to insert: 1
Enter the element to insert: 99
List after insertion:
12
99
23
34
45
56
Do you want to continue: n

Result
Thus various operations was successfully executed on list using array implementation.

28
Ex. No. 7a
ARRAY IMPLEMENTATION OF STACK ADT
Date:

AIM:
To write a C Program to implement stack using arrays.
ALGORITHM:
Step 1: Start
Step 2: Define an array stack of size max = 5
Step 3: Initialize top = -1
Step 4: Display a menu listing stack operations
Step 5: Read the choice from user
Step 6: If choice = 1 then
Step 6.1: If top < max -1
Step 6.1.1: Increment top
Step 6.1.2: Store element at current position of top
Step 6.2: Else Print Stack overflow

Step 7: Else If choice = 2 then


Step 7.1: If top < 0 then
Step 7.1.1: Print Stack underflow
Step 7.2: Else Display current top element
Step 7.3: Decrement top
Step 8: Else If choice = 3 then
Step 8.1: Display stack elements starting from top
Step 9: Stop

PROGRAM :

/* Stack Operation using Arrays */


#include <stdio.h>
#include <conio.h>
#define max 5
static int stack[max];
int top = -1;
void push(int x)
{
stack[++top] = x;
29
}

int pop()
{
return (stack[top--]);
}
void view()
{
int i;
if (top < 0)
printf("\n Stack Empty \n");
else
{
printf("\n Top-->");
for(i=top; i>=0; i--)
{
printf("%4d", stack[i]);
}
printf("\n");
}
}
void main()
{
int ch=0, val;clrscr();
while(ch != 4)
{
printf("\n STACK OPERATIONS \n");
printf("1.PUSH ");
printf("2.POP ");
printf("3.VIEW ");
printf(“4. QUIT \n”);
printf(“Enter Choice:”);
scanf("%d", &ch);
switch(ch)
{
case 1:
if(top < max-1)
{
printf("\nEnter Stack element : ");
scanf("%d", &val);
push(val);
}
else
printf("\n Stack Overflow \n");
break;
case 2:
if(top < 0)
printf("\n Stack Underflow \n");
else
{
val = pop();
30
printf("\n Popped element is %d\n", val);
}
break;
case 3:
view();
break;
case 4:
exit(0);
default:
printf("\n Invalid Choice \n");
}
}
getch();
}

OUTPUT:

STACK OPERATIONS
1.PUSH
2.POP
3.VIEW
4.QUIT
Enter Choice : 1
Enter Stack element : 12

STACK OPERATIONS
1. PUSH
2.POP
3.VIEW
4.QUIT
Enter Choice : 1
Enter Stack element : 23

STACK OPERATIONS
1.PUSH
2.POP
3.VIEW
4.QUIT
Enter Choice : 1
Enter Stack element : 34

STACK OPERATIONS
1.PUSH
2.POP
3.VIEW
4.QUIT
Enter Choice : 1
Enter Stack element : 45

31
STACK OPERATIONS
1.PUSH
2.POP
3.VIEW
4.QUIT
Enter Choice : 3
Top--> 45 34 23 12

STACK OPERATIONS
1.PUSH
2.POP
3.VIEW
4.QUIT
Enter Choice : 2
Popped element is 45

STACK OPERATIONS
1.PUSH
2.POP
3.VIEW
4.QUIT
Enter Choice : 3
Top--> 34 23 12

RESULT:
Thus the stack is implemented using arrays in C and has been executed
successfully.

32
Ex. No. 7b
ARRAY IMPLEMENTATION OF QUEUE ADT
Date:

AIM:
To write a C Program to implement queue using arrays.

ALGORITHM:
Step 1: Start
Step 2: Define an array queue of size max = 5
Step 3: Initialize front = rear = –1
Step 4: Display a menu listing queue operations
Step 5: Read the choice from user
Step 6: If choice = 1 then
Step 6.1: If rear < max -1
Step 6.1.1: Increment rear
Step 6.1.2: Store element at current position of rear
Step 6.2: Else Print Queue is Full

Step 7: Else If choice = 2 then


Step 7.1: If front =-1 then
Step 7.1.1: Print Queue Empty
Step 7.2: Else Display current front element

Step 7.3: Increment front


Step 8: Else If choice = 3 then
Step 8.1: Display queue elements from front to rear
Step 9: Stop
PROGRAM:

/* Queue Operation using Arrays */

#include <stdio.h>
#include <conio.h>
#define max 5
static int queue[max];
int front = -1;
33
int rear = -1;
void insert(int x)
{
queue[++rear] =x;
if (front == -1)
front = 0;
}
int remove()
{
int val;
val = queue[front];
if (front==rear&& rear==max-1)
front = rear = -1;
else
front ++;
return(val);
}
void view()
{
int i;
if (front == -1)
printf("\n Queue Empty \n");
else
{
printf("\n Front-->");
for(i=front; i<=rear; i++)
printf("%4d", queue[i]);
printf(" <--Rear\n");
}
}
void main()
{
int ch= 0,val;
clrscr();
while(ch != 4)
{
printf("\n QUEUEOPERATIONS\n");
printf("1.ENQUEUE ");
printf("2.DEQUEUE ");
printf("3.VIEW ");
printf("4.QUIT\n");
printf("Enter Choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(rear < max-1)
{
printf("\n Enter element to be inserted : ");
scanf("%d", &val);
insert(val);
}
34
else
printf("\n Queue Full \n");
break;
case 2:
if(front == -1)
printf("\n Queue Empty \n");
else
{
val = remove();
printf("\n Element deleted : %d \n", val);
}
break;
case 3:
view();
break;
case 4:
exit(0);
default:
printf("\n Invalid Choice \n");
}
}
getch();
}

35
OUTPUT:
QUEUEOPERATIONS
1.ENQUEUE 2.DEQUEUE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 12
QUEUE OPERATIONS
1.ENQUEUE 2.DEQUEUE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 23
QUEUE OPERATIONS
1.ENQUEUE 2.DEQUEUE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 34
QUEUE OPERATIONS
1.ENQUEUE 2.DEQUEUE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 45
QUEUE OPERATIONS
1.ENQUEUE 2.DEQUEUE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 56
QUEUE OPERATIONS
1.ENQUEUE 2.DEQUEUE 3.VIEW 4.QUIT
Enter Choice : 1
Queue Full
QUEUEOPERATIONS
1.ENQUEUE 2.DEQUEUE 3.VIEW 4.QUIT
Enter Choice : 3

Front--> 12 23 34 45 56 <--Rear

RESULT:
Thus the queue is implemented using arrays in C and has been executed
successfully.
36
Ex. No. 8A
LINKED LIST IMPLEMENTATION OF LIST ADT
Date:
AIM:
To write a C Program to implement List using linked list.
ALGORITHM:
Step 1: Start the program
Step 2: Define a linked list node for list
Step 3: Create Head node
Step 4 : Display a menu listing for list operations
Step 5: Read the choice from user
Step 6: Then perform list operations
Step 7: Display list elements starting from head node till end
Step 8: Stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create();
void display();
void insert();
void find();
void delete();
typedef struct node *position;
position L,p,newnode;
struct node
{
int data;
position next;
};
void main()
{
int choice;
clrscr();
do
{
printf("1.create\n2.display\n3.insert\n4.find\n5.delete\n\n\n");
printf("Enter your choice\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert();
37
break;
case 4:
find();
break;
case 5:
delete();
break;
case 6:
exit(0);
}
}
while(choice<7);
getch();
}
void create()
{
int i,n;
L=NULL;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the number of nodes to be inserted\n");
scanf("%d",&n);
printf("\n Enter the data\n");
scanf("%d",&newnode->data);
newnode->next=NULL;
L=newnode;
p=L;
for(i=2;i<=n;i++)
{
newnode=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;p->next=newnode; p=newnode;
}
}
void display()
{
p=L;
while(p!=NULL)
{
printf("%d -> ",p->data);
p=p->next;
}
printf("Null\n");
}
void insert()
{
int ch;
printf("\nEnter ur choice\n"); printf("\n1.first\n2.middle\n3.end\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{ p=L;

newnode=(struct node*)malloc(sizeof(struct node));


printf("\nEnter the data to be inserted\n");
scanf("%d",&newnode->data);newnode->next=L;
38
L=newnode;
display();
break;
}

case 2:
{
int pos,i=1;
p=L;
newnode=(struct node*)malloc(izeof(struct node));
printf("\nEnter the data to be inserted\n");
scanf("%d",&newnode->data);
printf("\nEnter the position to be inserted\n");
scanf("%d",&pos);
newnode->next=NULL;
while(i<pos-1)
{
p=p->next;
i++;
}
newnode->next=p->next;
p->next=newnode;
p=newnode;
display();
break;
}
case 3:
{
p=L;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data to be inserted\n");
scanf("%d",&newnode->data);
while(p->next!=NULL)p=p->next;
newnode->next=NULL;
p->next=newnode;
p=newnode;display();
break;
}
}
}
void find()
{
int search,count=0;
printf("\n Enter the element to be found:\n");
scanf("%d",&search);
p=L;
while(p!=NULL)
{
if(p->data==search)
{
count++;
break;
}
p=p->next;
39
}
if(count==0)
printf("\n Element Not present\n");
else
printf("\n Element present in the list \n\n");
}
void delete()
{
position p,temp;
int x;
p=L;
if(p==NULL)
{
printf("empty list\n");
}
else
{
printf("\nEnter the data to be deleted\n");
scanf("%d",&x);
if(x==p->data)
{
temp=p; L=p->next;
free(temp);
display();
}
else
{
while(p->next!=NULL && p->next->data!=x)
{
p=p->next;
}
temp=p->next;
p->next=p->next->next;free(temp);
display();
}
}
}

40
OUTPUT:
1.create
2.display
3.insert
4.find
5.delete
Enter your choice
1
Enter the number of nodes to be inserted
5
Enter the data

1
2
3
4
5

1.create

2.display

3.insert

4.find

5.delete
Enter your choice2
1 -> 2 -> 3 -> 4 -> 5 ->

1. create

2. display

3.insert
4.find

41
5.delete

Enter your choice3


Enter ur choice
1.first
2.middle
3.end
1
Enter the data to be inserted
7
7 -> 1 -> 2 -> 3 -> 4 -> 5 ->

1. create

2. display
3. insert
4.find
5.delete
Enter your choice

RESULT:
Thus the list is implemented using linked list in C and has been executed successfully.

42
Ex. No. 8B
LINKED LIST IMPLEMENTATION OF STACK ADT
Date:
AIM:
To write a C Program to implement stack using linked list.

ALGORITHM:
Step 1: Start
Step 2: Define a linked list node for stack
Step 3: Create Head node
Step 4: Display a menu listing stack operations
Step 5: Read the choice from user
Step 6:If choice = 1 then
Step 6.1: Allocate memory for a new node with data
Step 6.2: Make new node point to first node
Step 6.3: Make head node point to new node
Step 7: Else If choice = 2 then
Step 7.1: Make temp node point to first node
Step 7.2: Make head node point to next of temp node
Step 7.3: Release memory
Step 8: Else If choice = 3 then
Step 8.1: Display stack elements starting from head node till end
Step 9: Stop

PROGRAM :
/* Stack using Linked List */
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <alloc.h>
struct node
{
int label;
struct node *next;
};
void main()
{
int ch = 0
int k;
struct node *h, *temp, *head;

43
/* Head node construction */
head=(structnode*) malloc(sizeof(struct node));
head->next = NULL;

while(1)
{
printf("\n Stack using Linked List \n");printf("1->Push ");
printf("2->Pop ");
printf("3->View ");
printf("4->Exit \n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
/* Create a new node */
temp=(struct node *)(malloc(sizeof(struct node)));
printf("Enter label for new node : ");
scanf("%d", &temp->label);
h = head;
temp->next=h->next;h->next = temp;
break;
case 2:
/* Delink the first node */
h = head->next;
head->next = h->next;
printf("Node %s deleted\n", h->label);
free(h);
break;

case 3:
printf("\n HEAD -> ");
h = head;
/* Loop till last node */
while(h->next != NULL)
{
h = h->next;
printf("%d->",h->label);
}
printf("NULL \n");
break;
case 4: exit(0);
}
}
getch();
}

44
OUTPUT:
Stack using Linked List
1->Push 2->Pop 3->View 4->Exit
Enter your choice : 1
Enter label for new node : 23New node added

Stack using Linked List


1->Push 2->Pop 3->View 4->Exit
Enter your choice : 1
Enter label for new node : 34

Stack using Linked List


1->Push 2->Pop 3->View 4->Exit
Enter your choice : 3
HEAD -> 34 -> 23 -> NULL

RESULT:
Thus the stack is implemented using linked list in C and has been executed successfully.

45
Ex. No. 8C
LINKED LIST IMPLEMENTATION OF QUEUE ADT
Date:

AIM:
To write a C Program to implement queue using linked list.

ALGORITHM:
Step 1: Start

Step 2: Define a 'Node' structure with two members data and next.
Step 3: Define two Node pointers 'front' and 'rear' and set both to NULL
Step 4: Display a menu listing queue operations
Step 5: Read the choice from user
Step 6:If choice = 1 then
Step 6.1: Create a newNode with given value and set 'newNode → next'to
NULL
Step 6.2: Check whether queue is Empty (rear == NULL)
Step 6.3: If it is Empty then, set front = newNode and rear = newNode.
Step 6.4: If it is Not Empty then set rear→next = newNode and rear
=newNode.
Step 7: Else If choice = 2 then
Step 7.1: Check whether queue is Empty (front == NULL).
Step 7.2: If it is Empty, then display "Queue is Empty!!! Deletion is not
possible!!!" and terminate from the function.
Step 7.3: If it is Not Empty then, define a Node pointer 'temp' and set it
to'front'.
Step 7.4: Then set 'front = front → next' and delete 'temp’ node and free the
memory.
Step 8: Else If choice = 3 then
Step 8.1: Check whether queue is Empty (front == NULL).
Step 8.2: If it is Empty, then display "Queue is Empty!!!” and terminate
from the function.
Step 8.3: If it is Not Empty then, define a Node pointer 'temp' and initialize
with front.
46
Step 8.4: Display 'temp → data --->' and move it to the next node. Repeat
the same until 'temp' reaches to 'rear' (temp → next != NULL).
Step 8.5: Finally! Display 'temp → data ---> NULL’.Step 9: Stop

PROGRAM :
#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
struct Node *next;
}
*front = NULL,*rear = NULL;
void main()
{
int choice, value;
clrscr();
printf("\nQueue Implementation using Linked List\n");
while(1)
{
printf("\n****** MENU ******\n");
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be insert: ");
scanf("%d", &value);
insert(value);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:

47
printf("\nWrong selection!!! Please try again!!!\n");
}
}
void insert(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else
{
rear -> next = newNode;rear = newNode;
}
printf("\nInsertion is Success!!!\n")
}
void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct Node *temp = front;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n",temp->data);
48
}
}
getch();
}

49
OUTPUT:

Queue Implementation using Linked List


****** MENU ******
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 10
Insertion is Success!!!
****** MENU ******
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert:
2010--->20--->NULL

****** MENU ******


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Deleted Element: 10

****** MENU ******


1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your
choice: 310---
>NULL

RESULT:
Thus the queue is implemented using linked list in C and has been
executedsuccessfully.

50

You might also like