Вы находитесь на странице: 1из 18

1

DATA 5TRUCTURE5 WITH C



10C535

LAB MANUAL

2






DA1A S1kUC1UkLS WI1n C]C++ LA8CkA1Ck
(Common to CSL ISL)
Sub[ect Code 10CSL37 IA Marks 2S
nours]Week 03 Lxam nours 03
1ota| nours 42 Lxam Marks S0

1 uslng clrcular represenLaLlon for a polynomlal deslgn develop and execuLe a program ln C Lo accepL
Lwo polynomlals add Lhem and Lhen prlnL Lhe resulLlng polynomlal
2 ueslgn develop and execuLe a program ln C Lo converL a glven valld parenLheslzed lnflx arlLhmeLlc
expresslon Lo posLflx expresslon and Lhen Lo prlnL boLh Lhe expresslons 1he expresslon conslsLs of 13
slngle characLer operands and Lhe blnary operaLors + (plus) (mlnus) * (mulLlply) and / (dlvlde)
3 ueslgn develop and execuLe a program ln C Lo evaluaLe a valld posLflx expresslon uslng sLack
Assume LhaL Lhe posLflx expresslon ls read as a slngle llne conslsLlng of nonnegaLlve slngle dlglL
operands and blnary arlLhmeLlc operaLors 1he arlLhmeLlc operaLors are + (add) (subLracL) *
(mulLlply) and / (dlvlde)
4 ueslgn develop and execuLe a program ln C Lo slmulaLe Lhe worklng of a queue of lnLegers uslng an
array rovlde Lhe followlng operaLlons
a lnserL b ueleLe c ulsplay

3 ueslgn develop and execuLe a program ln C Lo read a sparse maLrlx of lnLeger values and Lo search
Lhe sparse maLrlx for an elemenL speclfled by Lhe user rlnL Lhe resulL of Lhe search approprlaLely
use Lhe Lrlple row column value Lo represenL an elemenL ln Lhe sparse maLrlx

6 ueslgn develop and execuLe a program ln C Lo creaLe a max heap of lnLegers by accepLlng one
elemenL aL a Llme and by lnserLlng lL lmmedlaLely ln Lo Lhe heap use Lhe array represenLaLlon for Lhe
heap ulsplay Lhe array aL Lhe end of lnserLlon phase

7 ueslgn develop and execuLe a program ln C Lo lmplemenL a doubly llnked llsL where each node
conslsLs of lnLegers 1he program should supporL Lhe followlng operaLlons
l CreaLe a doubly llnked llsL by addlng each node aL Lhe fronL
ll lnserL a new node Lo Lhe lefL of Lhe node whose key value ls read as an lnpuL
lll ueleLe Lhe node of a glven daLa lf lL ls found oLherwlse dlsplay approprlaLe message
lv ulsplay Lhe conLenLs of Lhe llsL
(noLe Cnly elLher (ab and d) or (a c and d) may be asked ln Lhe
examlnaLlon)

-ote In the exam|nat|on each student p|cks one quest|on from
a |ot of fthe 14 quest|ons


1. Using circuIar representation for a poIynomiaI, design, deveIop, and execute a program in
C to accept two poIynomiaIs, add them, and then print the resuIting poIynomiaI.
3



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

/* declaration of the nodes in the linked list */
typedef struct
{
int coeff;
int expo;
struct node *link;
}node;

node *avail=NULL,*ha,*hb,*hc,*temp,*prev=NULL;

node* getnode() /* create new node or get a used node*/
{
node *head;
if(avail==NULL)
{
head=(node*) malloc(sizeof(node));
if(head==NULL)
{
fprintf(stderr,"No Memory space !!!");
exit(1);
}
}
else
{
head=avail;
avail=avail->link;
}
return head;
}

node* attach(node *head) /* insert a term into poly */
{
int c,e;
temp=getnode();
printf("\nEnter the term: ");
scanf("%d%d",&c,&e);
temp->coeff=c;
temp->expo=e;
temp->link=head;
if(head->link==NULL)
head->link=temp;

else
prev->link=temp;
prev=temp;
return head;
4


}


void add() /* to add the two polynomials*/
{
node *a=ha->link,*b=hb->link;
temp=(node*)malloc(sizeof(node));
while(1)
{
if(hc->link==NULL)
hc->link=temp;
else
prev->link=temp;
temp->link=hc;
if(a->expo==b->expo)
{
if(a==ha)
break; /* a points to the header node */
else
{
temp->coeff=a->coeff+b->coeff;
temp->expo=a->expo;
a=a->link;
b=b->link;
}
}
else if(a->expo<b->expo)
{
temp->expo=b->expo;
temp->coeff=b->coeff;
b=b->link;
}
else
{
temp->expo=a->expo;
temp->coeff=a->coeff;
a=a->link;
}
prev=temp;
temp=getnode();
}
prev->link=hc;
/* display the result- c polynomial */
printf("\n The sum of the two polynomials : \n\n\t");
for(temp=hc->link;temp->link!=hc;temp=temp->link)

printf("%dx^%d + ",temp->coeff,temp->expo);

printf("%dx^%d",temp->coeff,temp->expo);
}

3


void main()
{
int i,n;
clrscr();

/* creation of the header nodes for a,b,c poly*/
ha=getnode();
hb=getnode();
hc=getnode();

ha->expo = hb->expo = hc->expo = -1;
ha->link = hb->link = hc->link = NULL;

/* input the terms of a,b poly*/
printf("\nEnter the number of terms in the 1st polynomial: ");
scanf("%d",&n);
for(i=0;i<n;i++)
ha=attach(ha);

printf("\nEnter the number of terms in the 2nd polynomial: ");
scanf("%d",&n);
for(i=0;i<n;i++)
hb=attach(hb);

add(ha,hb);
getch();
}























6


. Design, deveIop, and execute a program in C to convert a given vaIid parenthesized infix
arithmetic expression to postfix expression and then to print both the expressions. The
expression consists of singIe character operands and the binary operators + (pIus), -
(minus), * (muItipIy) and / (divide).

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX_SZE 20

char stack[MAX_SZE]; /* creation of an empty stack */
int top=-1;

void push(char c) /* insert an element in stack */
{
if(top > MAX_SZE) /* no of elements in stack is 20*/
{
fprintf(stderr,"Stack is full");
exit(1);
}
else stack[++top]=c;
}

char pop() /* remove the topmost element from stack */
{
if(top<0) /* no element in stack */
{
fprintf(stderr,"Stack is empty");
exit(1);
}
else
return stack[top--];
}

int prec(char c)
{
if( (c=='+') || (c=='-') ) return 1;
else if (c=='*' || c=='/') return 2;
else if (c=='(') return 0;
else if (c=='#') return -1;
}

void main()
{
int tr,i=0,len,nx=-1;
char token,c,infx[20],pf[10];
clrscr();
push('#');
printf("\n Enter the valid infix notation: ");

gets(infx);
7


len=strlen(infx);
while(i<len)
{
token=infx[i];
switch(token)
{
case '(': push(token);
break;
case ')': c=pop();
while(c != '(') {
pf[++nx]=c;
c=pop();
}
break;
case '+': /* check for precedence */
case '-': /* if precedence is more remove the top value*/
case '*':
case '/': while(prec(stack[top]) >= prec(token))
pf[++nx]=pop();
push(token);
break;
default: pf[++nx]=token; /* operand */
break;
}
i++;
}
while(top>0)
pf[++nx]=pop();
pf[++nx]=0;
printf("\n Postfix expression : ");
puts(pf);
getch();
}
















. Design, deveIop, and execute a program in C to evaIuate a vaIid postfix expression using
stack. Assume that the postfix expression is read as a singIe Iine consisting of non-



negative singIe digit operands and binary arithmetic operators. The arithmetic operators
are + (add), - (subtract), * (muItipIy) and / (divide).

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX_SZE 20

int stack[MAX_SZE];
int top=-1;

int isOperator(char s)
{
if(s=='+'||s=='-'||s=='*'||s=='/')
return 1;
else
return 0;
}

void push(int v)
{
if(top>MAX_SZE)
{
fprintf(stderr,"Stack is full");
exit(0);
}
else
stack[++top]=v;
}

int pop()
{
if(top<0)
{
fprintf(stderr,"Stack is empty");
exit(0);
}
else
return stack[top--];
}

void main()
{
int i,res,len,op1,op2;
char pf[20];
clrscr();
printf("Enter the valid postfix expression: ");

gets(pf);
len=strlen(pf);
for(i=0;i<len;i++)



{
if(!isOperator(pf[i])) /* if token is an operand */
push(pf[i]-'0'); /* converts char to int and pushes */
else
{ /* if token is an operator */
op1=pop();
op2=pop();
switch(pf[i])
{
case '+' : res = op2 + op1;
break;
case '-' : res = op2 - op1;
break;
case '*' : res = op2 * op1;
break;
case '/' : res = op2 / op1;
break;
}
push(res);
}
}
printf("\n The result of %s = %d",pf,stack[top]);
getch();
}

























. Design, deveIop, and execute a program in C to simuIate the working of a queue of
integers using an array. Provide the foIIowing operations:
10


a. Insert b. DeIete c. DispIay

#include<stdio.h>
#include<conio.h>
#define MAX_QSZE 20 /* max no. of elements in queue */

int q[MAX_QSZE],rear=-1,front=-1; /*global declarations */

void insert(int x)
{
if(rear==MAX_QSZE-1)
{
fprintf(stderr,"Queue is full\n");
exit(1);
}
q[++rear]=x;
}

int del()
{
if(front==rear)
{
fprintf(stderr,"\n Queue is empty");
exit(1);
}
return q[++front];
}

void display()
{
int i;
printf("\nThe contents of queue: \n");
for(i=front+1;i<=rear;i++)
printf("\t %d",q[i]);
}

void main()
{
int op,no,res;
clrscr();
printf("\nOptions:\n1.nsert\n2.Delete");
printf("\n3.Display\n4.Exit");

while(1)
{
printf("\nSelect an option: ");
scanf("%d",&op);

switch(op)
{
case 1: printf("\nEnter the number: ");
11


scanf("%d",&no);
insert(no);
break;
case 2: res=del();
printf("\n Deleted element is %d",res);
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nEnter a valid choice");
}
}
}
12



. Design, deveIop, and execute a program in C to read a sparse matrix of integer vaIues and
to search the sparse matrix for an eIement specified by the user. Print the resuIt of the
search appropriateIy. Use the tripIe <row, coIumn, vaIue> to represent an eIement in the
sparse matrix.

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

/* declaration of the triple for sparse matrix */
struct
{
int row,col,val;
}sp[10];

void main()
{
int r,c,org[3][3],cnt=0,i,j,k,sprow;
int key,kr,kc,found=0;
clrscr();
printf("\nEnter the no of rows,cols: ");
scanf("%d%d",&r,&c);
printf("\nEnter the elements of the matrix: \n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
scanf("%d",&org[i][j]);
if(org[i][j]!=0) cnt++;
}
sp[0].row=r;
sp[0].col=c;
sp[0].val=cnt;
sprow=1; /* sprow- from 1 till the no.of non-zero elements*/
while(sprow<=cnt)
{
for(j=0;j<r;j++)
for(k=0;k<c;k++)
if(org[j][k]!=0)
{
sp[sprow].row=j;
sp[sprow].col=k;
sp[sprow].val=org[j][k];
sprow++;
}
}
for(j=0;j<sprow;j++)
printf("\n%d %d %d ",sp[j].row,sp[j].col,sp[j].val);
printf("\nEnter the element to be searched: ");
scanf("%d",&key);
for(j=1;j<sprow;j++)
if(sp[j].val==key) /* key found in the sparse matrix */
13


{
kr=sp[j].row;
kc=sp[j].col;
found=1;
}
if(found)
printf("\nElement found in sparse matrix\n\t <%d,%d %d> <row,col,val>",kr,kc,key);
else
printf("\nElement-%d not found in sparse matrix",key);
getch();
}






































14


. Design, deveIop, and execute a program in C to create a max heap of integers by accepting
one eIement at a time and by inserting it immediateIy in to the heap. Use the array
representation for the heap. DispIay the array at the end of insertion phase.


#include<stdio.h>
#include<conio.h>
#define MAX_ELEMENTS 20
#define HEAP_FULL(n) (n==MAX_ELEMENTS-1)
#define HEAP_EMPTY(n) (!n)
int heap[MAX_ELEMENTS],n=0;

void push(int item)
{
int i;
if(HEAP_FULL(n))
{
fprintf(stderr,"The heap is full!\n");
exit(1);
}
i=++n;
while((i!=1) && (item>heap[i/2]))
{
heap[i]=heap[i/2];
i/=2;
}
heap[i]=item;
}

void display()
{
int j;
printf("\n\nThe heap after insertion :\n");
for(j=1;j<=n;j++)
printf("\t%d",heap[j]);
}

void main()
{
int ch,item,j;
clrscr();
printf("\n1.nsert\t 2.Exit");
while(1)
{
printf("\nEnter the choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the element: ");

scanf("%d",&item);
13


push(item);
display();
break;
case 2: exit(0);
default: printf("\nEnter a valid choice");
}
}
}










































16


. Design, deveIop, and execute a program in C to impIement a doubIy Iinked Iist where each
node consists of integers. The program shouId support the foIIowing operations:
i. Create a doubIy Iinked Iist by adding each node at the front.
ii. Insert a new node to the Ieft of the node whose key vaIue is read as an input.
iii. DeIete the node of a given data if it is found, otherwise dispIay appropriate message.
iv. DispIay the contents of the Iist.
(Note: OnIy either (a,b and d) or (a, c and d) may be asked in the examination)

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

typedef struct
{
int data;
struct NODE *llink,*rlink;
}NODE;

NODE *head=NULL,*temp,*prev,*cur,*next;

void create()
{
int c;
char op;
do
{
printf("\nEnter the element: ");
scanf("%d",&c);
temp=(NODE*)malloc(sizeof(NODE));
temp->data=c;
if(head==NULL)
{
head=temp;
temp->rlink=NULL;
temp->llink=NULL;
}
else
{
temp->rlink=head;
head->llink=temp;
temp->llink=NULL;
head=temp;
}
fflush(stdin);
printf("Continue?(y/n): ");
scanf("%c",&op);
}while(op=='y');
}




17


void insert_dll()
{
int key;
cur=(NODE*)malloc(sizeof(NODE));
printf("\nEnter the value to be inserted: ");
scanf("%d",&cur->data);
printf("\nEnter the key value: ");
scanf("%d",&key);
for(temp=head;temp->data!=key;temp=temp->rlink);
if(temp==NULL)
printf("\nData not found");
else
{
cur->rlink=temp;
cur->llink=temp->llink;
prev=temp->llink;
prev->rlink=cur;
temp->llink=cur;
if(temp==head) head=cur;
printf("\nValue inserted into the list");
}
}

void delete_dll()
{
int val;
printf("Enter the value to be deleted: ");
scanf("%d",&val);
for(temp=head;temp->data!=val;temp=temp->rlink);
if(temp==NULL)
printf("\nData not found");
else if(temp==head)
head=temp->rlink;
else
{
next=temp->rlink;
next->llink=temp->llink;
prev=temp->llink;
prev->rlink=temp->rlink;
}
}

void display()
{
for(temp=head;temp!=NULL;temp=temp->rlink)
printf("\t%d",temp->data);
}




1


void main()
{
int ch;

clrscr();
printf("\n Options:\n1.Create a list \n2.nsert(to the left of key)");
printf("\n3.Delete node with key value \n4.Display the list \n5.Exit");
while(1)
{
printf("\nEnter the option: ");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: insert_dll();
break;
case 3: delete_dll();
break;
case 4: display();
break;
case 5: exit(0);
}
}
}

Вам также может понравиться