ACP Journal

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

ADVANCED C PROGRAMING ASSIGNMENTS

PART- A

/*A1.Write C program to accept and display 1D array


Also write functions.
i)to insert an element at the specified position
ii)to delete element based on the position
iii)to delete based on the value
function should take care of invalid data and accordingly display
appropriate error messages.
*/

1
#include<stdio.h>
int main()
{
int a[20],n,pos,key,ele,choice;
int read_array(int*);
void display_array(int*,int);
int insert_at_pos(int*,int*,int,int);
int delete_key_element(int*,int*,int,int*);
int delete_at_pos(int*,int*,int,int*);
n=read_array(a);
printf("Elements are\n");
display_array(a,n);
while(1)
{
printf("1-display\n2-insert\n3-delete based on position\n4-delete key
element\ndefault stop\n");
printf("input choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Elements are\n");
display_array(a,n);
break;
case 2:printf("input element & position of insertion starting from 1
upto %d\n",n);
scanf("%d %d",&ele,&pos);
if(!insert_at_pos(a,&n,pos,ele))
printf("sorry invalid position\n");
else
printf("successfull insertion\n");
break;
case 3:printf("input position of deletion starting from 1 upto %d\n",n);
scanf("%d",&pos);
if(!delete_at_pos(a,&n,pos,&ele))
printf("sorry invalid position\n");
else
printf("successfully deleted %d element at %d position\n",ele,pos);
break;
case 4:printf("input key element to be deleted\n");
scanf("%d",&key);
if(!delete_key_element(a,&n,key,&pos))

2
printf("sorry %d do not exist\n",key);
else
printf("successfully deleted %d element at position %d\n",key,pos);
break;
default:return(0);
}
}
return 0;
}
/*function to load array & return the size*/
int read_array(int *pa)
{
int n;
printf("input size?\n");
scanf("%d",&n);
printf("input %d no of elements\n",n);
for(int i=0;i<n;i++)
scanf("%d",(pa+i));
return n;
}
/*function to display array takes array & size as parameter*/
void display_array(int*pa,int n)
{
printf("cell no \t address \t value\n");
for(int i=0;i<n;i++)
printf("a[%d]\t\t %p\t\t %d\n",i,(pa+i),*(pa+i));
}
/*
task:to insert an element at the specified position
pa: beginning address of array
pn:points to location(n) containing no of elements
pos:position of insertion
ele:element to be inserted
return value: 1 successful insertion 0 failure
*/
int insert_at_pos(int*pa,int*pn,int pos,int ele)
{
if(pos>(*pn)+1|| pos<=0)
return 0;
else
{

3
for ( int j=*(pn)-1;j>=pos-1;j--)
{printf("***%d",j);
pa[j+1]=pa[j];
}
pa[pos-1]=ele;
(*pn)++;
printf("***%d",*pn);
return 1;
}
}
/*
task:to delete an element at the specified position
pa: beginning address of array
pn:points to location(n) containing no of elements
pos:position of deletion
pe:deleted element
return value: 1 successful insertion 0 failure
*/
int delete_at_pos(int*pa,int*pn,int pos,int*pe)
{
if(pos>*pn|| pos<=0)
return 0;
else
{
*pe=pa[pos-1];
for ( int j=pos-1;j<*pn-1;j++)
pa[j]=pa[j+1];
(*pn)--;
return 1;
}
}
/*
task:to delete key element
pa:beginning address of array
pn :points to location(n) containing no of elements
key:key element to be deleted
ppos:pointer to location(pos)where position(index) of deleted element is
placed
return value: 1 successful insertion 0 failure
*/
int delete_key_element(int*pa,int*pn,int key ,int*ppos)

4
{ *ppos=-1;
for(int i=0;i<*pn;i++)
{
if(*(pa+i)==key)
{
*ppos=i+1;
break;
}
}
if(*ppos==-1)
return 0;
for ( int j=(*ppos)-1;j<(*pn)-1;j++)
pa[j]=pa[j+1];
(*pn)--;
return 1;
}

5
OUTPUT A1:

input size?
5
input 5 no of elements
10
20
30
40
50
Elements are
cell no address value
a[0] 000000d4b8bff960 10
a[1] 000000d4b8bff964 20
a[2] 000000d4b8bff968 30
a[3] 000000d4b8bff96c 40
a[4] 000000d4b8bff970 50
1-display
2-insert
3-delete based on position
4-delete key element
default stop
input choice
2
input element & position of insertion starting from 1 upto 5
35
3
***4***3***2***6successfull insertion
1-display
2-insert
3-delete based on position
4-delete key element
default stop
input choice
1
Elements are
cell no address value
a[0] 000000d4b8bff960 10
a[1] 000000d4b8bff964 20
a[2] 000000d4b8bff968 35
a[3] 000000d4b8bff96c 30
a[4] 000000d4b8bff970 40
a[5] 000000d4b8bff974 50
1-display
2-insert
3-delete based on position
4-delete key element
6
default stop
input choice
3
input position of deletion starting from 1 upto 6
4
successfully deleted 30 element at 4 position
1-display
2-insert
3-delete based on position
4-delete key element
default stop
input choice
1
Elements are
cell no address value
a[0] 000000d4b8bff960 10
a[1] 000000d4b8bff964 20
a[2] 000000d4b8bff968 35
a[3] 000000d4b8bff96c 40
a[4] 000000d4b8bff970 50
1-display
2-insert
3-delete based on position
4-delete key element
default stop
input choice
4
input key element to be deleted
1
sorry 1 do not exist
1-display
2-insert
3-delete based on position
4-delete key element
default stop
input choice
4
input key element to be deleted
10
successfully deleted 10 element at position 1
1-display
2-insert
3-delete based on position
4-delete key element
default stop
input choice
1

7
Elements are
cell no address value
a[0] 000000d4b8bff960 20
a[1] 000000d4b8bff964 35
a[2] 000000d4b8bff968 40
a[3] 000000d4b8bff96c 50
1-display
2-insert
3-delete based on position
4-delete key element
default stop
input choice

8
/* A2.Write C program to accept and display 2d array of user specified size.Also
write functions to perform the following on the 2d array
I. Function row_sum that takes row number as parameter and returns the sum
of the row
II. Function col_sum that takes column number as parameter and returns the
sum of the column
III. Function secondary _diagonal_sum that returns the sum of secondary
diagonal elements if possible else should return -1
IV. Function primary_diagonal_sum that returns the sum of primary diagonal
elements if possible else should return -1
*/

9
#include<stdio.h>
int main()
{
void read_array(int[][5],int,int);
void display_array(int[][5],int,int);
int row_sum(int [][5],int,int);
int col_sum(int [][5],int,int);
int sec_diagonal_sum(int [][5],int,int);
int pri_diagonal_sum(int [][5],int,int);
int nr,nc,ch,kr,kc,res;
int a[5][5];
printf("input no of rows & columns < =5\n");
scanf("%d %d",&nr,&nc);
read_array(a,nr,nc);
display_array(a,nr,nc);
while(1)
{
printf("1-rowsum\n2-colsum\n3-principal diagonal sum\n4-secondary
diagonal sum\n 5-display\nanyother terminate\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("input row number of the row to be summed valid range
1 to %d\n",nr);
scanf("%d",&kr);
if(kr>nr || kr<0)
printf("invalid row no");
else
{
res=row_sum(a,kr-1,nc);
printf("sum of elements of %d row is=%d\n",kr,res);
}
break;
case 2:printf("input column number of the row to be summed valid
range 1 to %d\n",nc);
scanf("%d",&kc);
if(kc<nc || kc<0)
printf("invalid row no");
else
{
res=col_sum(a,kc-1,nr);

10
printf("sum of elements of %d col is=%d\n",kc,res);
}
break;
case 3: res=pri_diagonal_sum(a,nr,nc);
if(res==-1)
printf("not square matrix\n");
else
printf("sum of principal diagonal elements is=%d\n",res);
break;
case 4: res=sec_diagonal_sum(a,nr,nc);
if(res==-1)
printf("not square matrix\n");
else
printf("sum of secondary diagonal elements is=%d\n",res);
break;
case 5:display_array(a,nr,nc);
break;
default:return(0);
}
}
}
/* return row sum of specified row , if square matrix else returns -1
a1:2d array
kr1:row number to be summed
nr1:number of rows
nc1:number of columns
returns row sum or -1
*/
void read_array(int a1[][5],int nr1,int nc1)
{
for(int row=0;row<nr1;row++)
{
printf("input %d no of elements of %d row\n",nc1,row);
for(int col=0;col<nc1;col++)
scanf("%d",&a1[row][col]);
printf("\n");
}

}
/* returns row sum of specified row , if square matrix else returns -1
a1:2d array

11
kr1:row number to be summed
nr1:number of rows
nc1:number of columns
returns row sum of -1
*/
void display_array(int a1[][5],int nr1,int nc1)
{
printf("contents are \n");
for(int row=0;row<nr1;row++)
{
for(int col=0;col<nc1;col++)
printf("%d\t",a1[row][col]);
printf("\n");
}
}
/* returns row of specified row , if square matrix else returns -1
a1:2d array
kr1:row number to be summed
nc1:number of columns
returns row sum or -1
*/
int row_sum(int a1[][5],int kr1,int nc1)
{
int rsum=0;
for(int col=0;col<nc1;col++)
rsum+=a1[kr1][col];
return rsum;
}
/* returns column sum of specified column, if square matrix else returns-
1
a1:2d array
kc1:column number to be summed
nr1:number of rows
returns column sum or -1
*/
int col_sum(int a1[][5],int kc1,int nr1)
{
int csum=0;
for(int row=0;row<nr1;row++)
csum+=a1[row][kc1];
return csum;

12
}
/* returns secondary diagonal sum,if square matrix else returns -1
a1:2d array
nr1:number of rows
nc1:number of columns
returns secondary diagonal sum or -1
*/
int sec_diagonal_sum(int a1[][5],int nr1, int nc1)
{
int row,col,ssum=0;
if(nr1==nc1)
{
for(row=0,col=nc1-1;(row<nr1 && col>=0);row++,col--)
ssum+=a1[row][col];
return ssum;
}
else
return -1;
}
/* returns principal diagonal sum,if square matrix else returns -1
a1:2d array
nr1:number of rows
nc1:number of columns
returns principal diagonal sum or -1
*/
int pri_diagonal_sum(int a1[][5],int nr1,int nc1)
{
int psum=0;
if(nr1==nc1)
{
for(int row=0;row<nr1;row++)
psum+=a1[row][row];
return psum;
}
else
return -1;
}

13
OUTPUT A2:

input no of rows and columns<=5


43
input 3 no of elements of 0 row
236
input 3 no of elements of 1 row
587
input 3 no of elements of 2 row
789
input 3 no of elements of 3 row
358
contents are
2 3 6
5 8 7
7 8 9
3 5 8
1-rowsum
2-colsum
3-principal diagonal sum
4-secondary diagonal sum
5-display
any other terminate
1
input row number of the row to be summed valid range 1 to 4
2
sum of elements of 2 row is=20
1-rowsum
2-colsum
3-principal diagonal sum
4-secondary diagonal sum
5-display
any other terminate
2
input column number of the row to be summed valid range 1 to 3
3
sum of elements of 3 col is=30
1-rowsum
2-colsum
3-principal diagonal sum
4-secondary diagonal sum
5-display
any other terminate
3
not square matrix
1-rowsum
2-colsum

14
3-principal diagonal sum
4-secondary diagonal sum
5-display
any other terminate
4
not square matrix
1-rowsum
2-colsum
3-principal diagonal sum
4-secondary diagonal sum
5-display
any other terminate

15
/*A3.Swap of two integers*/

16
#include<stdio.h>
int main()
{
int a,b;
void swap(int*,int*);
printf("input two integers\n");
scanf("%d %d",&a,&b);
printf("before swap values of a &b are\n");
printf("a=%d\tb=%d\n",a,b);
swap(&a,&b);
printf("after swap values of a &b are\n");
printf("a=%d\tb=%d\n",a,b);
return 0;
}

void swap(int *pa,int *pb)


{
printf("within function before exchange\n");
printf("a=%d\tb=%d\n",*pa,*pb);
int t;
t=*pa;
*pa=*pb;
*pb=t;
printf("within function after exchange\n");
printf("a=%d\tb=%d\n",*pa,*pb);
}

17
OUTPUT A3:

input two integers


50
60
before swap values of a &b are
a=50 b=60
within function before exchange
a=50 b=60
within function after exchange
a=60 b=50
after swap values of a &b are
a=60 b=50

18
/*A4.Write program to accept and display(both forward & reverse) and sum
array elements using pointer arithmetic
* Write C program to accept and display 1d array.Use external pointer to
process the array.Use separate functions to
• Accept the array elements
• Display the array elements in forward direction
• Display the array elements in reverse diretion
• To compute the average of the elements in the array*/

19
#include<stdlib.h>
#include<stdio.h>
int main()
{
int a[10],n;
int accept_array(int*);
void display_forward(int*,int);
void display_reverse(int*,int*);
float average(int*,int);
int *pb=a,*pe;
n=accept_array(pb);
display_forward(pb,n);
pe=a+n-1;
display_reverse(pe,pb);
printf("average=%f\n",average(pb,n));
return 0;
}
int accept_array(int*pb)
{
int n;
printf("size<10?\n");
scanf("%d",&n);

/*accepting values*/
printf("input %d integers\n",n);
for (int i=0;i<n;i++)
scanf("%d",(pb+i));
return n;
}
void display_forward(int*pb,int n)
{
int*pe=pb+n-1,*pw;
printf("array elements in forward direction\n");
/*print forward direction using external pointer*/
for(pw=pb;pw<=pe;pw++)
printf("address= %p\tvalue= %d\n",pw,*pw);
}
void display_reverse(int*pe,int*pb)
{
/*reverse printing*/
printf("array elements in reverse order\n");

20
for(int*pw=pe;pw>pb;pw--)
printf("%d\n",*pw);
}
float average(int*pb,int n)
{
int sum=0;
/*summing array elements*/
for (int i=0;i<n;i++)
sum+=*(pb+i);
return(sum/n);
}

21
OUTPUT A4:

Size<10?
2
Input 2 integers
6 8
Array elements in forward direction
Address=0061FEEC value=6
Address=0061FEF0 value=8
Array elements in reverse order
8 6
Average=7.00000000

22
/*A5. Write C program to store
information(name,employee_id,designation,date of birth,stay details) about set
of employees in a company. Here
designation is string that can take one of these values {md, manager,clerk,peon}
date_ of_ birth is a structure for holding birth date with fields day,month,year
stay_detail is a structure that contains street number and sector number and
house number details.
Write separate functions to accept & display the employees*/

23
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
typedef struct
{
int year,month,day;
}date;

typedef struct
{
int street_no,sector_no,house_no;
}address;

typedef struct
{
int emp_id;
char name[15];
char desig[15];
address con_add;
date dob;
}employee;
int main()
{
employee emp[20];
int n;
int accept_details(employee[]);
void display_details(employee[],int);

n=accept_details(emp);
display_details(emp,n);
return 0;
}
int accept_details(employee e[])
{
int i,n,d;
printf("input total number of employees\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("input employees id & name\n");
scanf("%d %s",&(e[i].emp_id),e[i].name);

24
printf("input 1 for md\n2-for manager\n3-for clerk\n4-peon\n");
scanf("%d",&d);
switch(d)
{
case 1:strcpy(e[i].desig,"md");
break;
case 2:strcpy(e[i].desig,"manager");
break;
case 3:strcpy(e[i].desig,"clerk");
break;
case 4:strcpy(e[i].desig,"peon");
}
printf("contact address details\n");
printf("street_no\tsector_no\thouse_no\n");
scanf("%d %d
%d",&(e[i].con_add.street_no),&(e[i].con_add.sector_no),&(e[i].con_add
.house_no));
printf("birth date details \n");
printf("day\tmonth\tyears\n");
scanf("%d %d
%d",&(e[i].dob.day),&(e[i].dob.month),&(e[i].dob.year));
}
return n;
}
void display_details(employee e[10],int n)
{
int i;
printf("employees details\n");
for(i=0;i<n;i++)
{
printf("_\n");
printf("employees id=%d\t name=%s\n",(e[i].emp_id),e[i].name);
printf("designation =%s\n",(e[i].desig));
printf("contact address details\nstreet_no,house_no\n");
printf("street_no=%d\tsector_no=
%d\t,house_no=%d\n",(e[i].con_add.street_no),(e[i].con_add.sector_no
),(e[i].con_add.house_no));
printf("birth date details day month year\n");

printf("day=%d\tmonth=%d\tyear=%d\n",(e[i].dob.day),(e[i].dob.month
),(e[i].dob.year));

25
}
}

26
OUTPUT A5:

input total number of employees


2
input employees id & name
24
Arun
input 1 for md
2-for manager
3-for clerk
4-peon
1
contact address details
street_no sector_no house_no
9
11
36
birth date details
day month years
26
09
2001
input employees id & name
30
Anand
input 1 for md
2-for manager
3-for clerk
4-peon
2
contact address details
street_no sector_no house_no
6
8
40
birth date details
day month years
1
8
2000
employees details

27
_
employees id=24 name=Arun
designation =md
contact address details
street_no,house_no
street_no=9 sector_no= 11 ,house_no=36
birth date details day month year
day=26 month=9 year=2001
_
employees id=30 name=Anand
designation =manager
contact address details
street_no,house_no
street_no=6 sector_no= 8 ,house_no=40
birth date details day month year
day=1 month=8 year=2000

28
PART- B

/*B1. Write C program to implement stack of integers using array.*/

29
#include<stdio.h>
#include<stdlib.h>

#define MAX 10

int STACK[MAX],TOP;
/* display stack element*/
void display(int [ ]);

/* push (insert) item into stack*/


void PUSH(int [ ],int);

/* pop (remove item from stack*/


void POP (int [ ]);

int main( )
{
int ITEM=0;
int choice=0;
TOP=-1;

while(1)
{
printf("Enter choice (1: display, 2: insert (PUSH), 3: remove(POP),
4: Exit..:");
scanf("%d",&choice);

switch(choice)
{
case 1:display(STACK);
break;
case 2:printf("Enter Item to be insert :");
scanf("%d",&ITEM);
PUSH(STACK,ITEM);
break;
case 3:POP(STACK);
break;
case 4:exit(0);
default:
printf("\nInvalid choice.");
break;

30
}

}// end of while(1)

}
/* function : display(),to display stack elements.*/
void display(int stack[])
{
int i=0;
if(TOP==-1)
{
printf("Stack is Empty .\n");
return;
}

printf("%d <-- TOP ",stack[TOP]);


for(i=TOP-1;i >=0;i--)
{
printf("\n%d",stack[i]);
}
printf("\n\n");
}
/* function : PUSH(),to push an item into stack.*/
void PUSH(int stack[],int item)
{
if(TOP==MAX-1)
{
printf("\nSTACK is FULL CAN'T ADD ITEM\n");
return;
}
TOP++;
stack[TOP]=item;
}
/* function : POP(),to pop an item from stack.*/
void POP(int stack[])
{
int deletedItem;
if(TOP==-1)
{
printf("STACK is EMPTY.\n");
return;

31
}
deletedItem=stack[TOP];
TOP--;
printf("%d deleted successfully\n",deletedItem);
return;
}

32
OUTPUT B1:

Enter choice (1: display, 2: insert (PUSH), 3: remove(POP), 4: Exit..:


2
Enter Item to be insert :60
Enter choice (1: display, 2: insert (PUSH), 3: remove(POP), 4: Exit..:3
60 deleted successfully
Enter choice (1: display, 2: insert (PUSH), 3: remove(POP), 4: Exit..:1
Stack is Empty .
Enter choice (1: display, 2: insert (PUSH), 3: remove(POP), 4: Exit..:4

33
/* B2. Write C program to implement linear queue of integers using
array */

34
#include<stdio.h>
#include<stdlib.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");

35
}
break;
case 4:exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}

36
OUTPUT B2:

Queue using Array


1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1

Enter no 1:12

Enter the Choice:3

Queue Elements are:


12

Enter the Choice:2

Deleted Element is 12
Enter the Choice:1

Enter no 2:25

Enter the Choice:3

Queue Elements are:


25

Enter the Choice:2

Deleted Element is 25
Enter the Choice:3

Queue Elements are:

Queue is Empty
Enter the Choice:4

37
B3. Write C program to create & display singly linked list of integers

38
#include<stdio.h>
#include<stdlib.h>

struct node
{
int num; //Data of the node
struct node *nextptr; //Address of the next node
}*stnode;

void createNodeList(int n); // function to create the list


void displayList(); // function to display the list

int main()
{
int n;
printf("\n\n Linked List : To create and display singly Linked List
:\n");
printf("\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the List : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num,i;
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode == NULL) //check whether the fnnode is NULL and if so no


memory allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard

printf(" Input data for node 1 : ");

39
scanf("%d",&num);
stnode->num = num;
stnode->nextptr = NULL; // links the address field to NULL
tmp = stnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);

fnNode->num = num; // links the field of fnNode


with num
fnNode->nextptr = NULL;// links the address field
of fnNode with NULL
tmp->nextptr = fnNode; // links previous node
i.e. tmp to the fnNode
tmp = tmp->nextptr;
}
}
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{

40
printf(" Data = %d\n", tmp->num); // prints the
data of current node
tmp = tmp->nextptr; // advances the
position of current node
}
}
}

41
OUTPUT B3:

Linked List : To create and display singly Linked List :

Input the number of nodes : 2


Input data for node 1 : 4
Input data for node 2 : 8

Data entered in the List :


Data = 4
Data = 8

42
/* B4. Write C program to implement stack using linked list */

43
#include<stdio.h>
#include<stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;

int main ()
{
int choice=0;
printf("\n*stack operations using linked list*\n");
printf("\n-\n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\n1.push\n2.pop\n3.show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:

44
{
printf("Exiting....");
break;
}
default:
{
printf("please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the values");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr-> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}

void pop()
{

45
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped %d",item);
}
}
void display()
{
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("stack is empty\n");
}
else
{
printf("printing stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}

46
OUTPUT B4:

*stack operations using linked list*

Chose one from the below options...

1.push
2.pop
3.show
4.Exit
Enter your choice
1
Enter the values20
Item pushed

Chose one from the below options...

1.push
2.pop
3.show
4.Exit
Enter your choice
3
printing stack elements
20

Chose one from the below options...

1.push
2.pop
3.show
4.Exit
Enter your choice
2
Item popped 20

Chose one from the below options...

47
1.push
2.pop
3.show
4.Exit
Enter your choice
3
stack is empty

Chose one from the below options...

1.push
2.pop
3.show
4.Exit
Enter your choice
4
Exiting....

48
/* B5. Write C program to implement queue using linked list */

49
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
int main ()
{
int choice;
while(choice !=4)
{
printf("\n*MainMenu*\n");
printf("\n======\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the
queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}

50
}
void insert()
{
struct node *ptr;
int item;

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


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else

51
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{
printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}

52
OUTPUT B5:

*MainMenu*

======

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter value?
58

*MainMenu*

======

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

printing values .....

58

*MainMenu*

======

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?2


53
*MainMenu*

======

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

Empty queue

*MainMenu*

======

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?4

54

You might also like