0% found this document useful (0 votes)
68 views27 pages

Data Structure: Submited To

The document contains program code submissions from Savera Khurshid, Fatima Faroqui, and Sana Yonus for various data structure programs. The programs cover topics like traversing an array, solving a quadratic equation, inserting an element into an array, bubble sort, linear search, binary search, postfix notation, stack operations, linked lists, substrings, trees, tower of Hanoi, and queues. The document provides the source code for each program along with sample inputs and outputs.

Uploaded by

anum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views27 pages

Data Structure: Submited To

The document contains program code submissions from Savera Khurshid, Fatima Faroqui, and Sana Yonus for various data structure programs. The programs cover topics like traversing an array, solving a quadratic equation, inserting an element into an array, bubble sort, linear search, binary search, postfix notation, stack operations, linked lists, substrings, trees, tower of Hanoi, and queues. The document provides the source code for each program along with sample inputs and outputs.

Uploaded by

anum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 27

********DATA STRUCTURE******

**SUBMITED BY:**
*SAVERA KHURSHID
*FATIMA FAROQUI
*SANA YONUS

SUBMITED TO:
MEM NARMEEN
PROGRAMES OF DATA STRUCTURE

PROGRAME
TRAVERSING AN ARRAY

#include<stdio.h>
#include<conio.h>
main()
{
int la[]={0,1,2,3,4,5};
int k ,ub=5,lb=0;
clrscr();
for(k=lb;k<=ub;k++)
{
printf("%d",la[k]);
}
}
OUTPUT:
012345
PROGRAMME
QUADRATIC EQUATION

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int a,b,c,d,e;
clrscr();
printf("enter three numbers");
scanf("%d %d %d",&a,&b,&c);
d=sqrt(b-(4*a*c));
if(d==0)
e=((sqrt(b)+sqrt(d))/2*a);
printf("the result is %d",e);
}

OUTPUT:
ENTER THREE NUMBERS:1
2
4
THE RESULT IS 2
PROGRAMME
INSERTING ELEMENT IN ARRAY
#include<stdio.h>
#include<conio.h>
main()
{
int la[20]={0,1,2,3,4,5};
int a,k,j=6,b;
printf("please enter number");
scanf("%d",&a);
printf("plz enter position");
scanf("%d"&k);
while(j>=k)
{
la[j+1]=la[j];
j=j-1;
}
la[k]=a;
for(b=0;b<=7;b++)
{
printf("%d",la[b]);
}

OUTPUT:
PROGRAMME
BUBBLE SORT

#include<stdio.h>
#include<conio.h>
main(){
int data[]={7,3,8,1,90};
int n=4,k,temp;
int ptr=0;
clrscr();
for(k=1;k<=5;k++)
{
ptr=0;
while(ptr<6-k)
{
if(data[ptr]>data[ptr+1])
{
temp=data[ptr+1];
data[ptr+1]=data[ptr];
data[ptr]=temp;
}
ptr=ptr+1;
}
}
for(k=0;k<6;k++)
{
printf("%d",data[k]);
}
}
PROGRAMME
LINEAR SEARCH

#include<stdio.h>
#include<conio.h>
main()
{
int data[]={1,5,30,20,60};
int loc=0,k=0,a,c=0;
printf("enter number");
scanf("%d",&a);

while(loc=0&&k<-4)
{
if(a==data[k];loc=k;
k=k+1;
}
if(loc==0)
{
printf("item is not in array");
}
else
printf("the location of a %d",loc);
}
PROGRAMME
BINARY SEARCH

#include<stdio.h>
#include<conio.h>
main()
{
int data[]={5,10,15,20,25};
int beg=5;
int end=25;
int mid;int a;
printf("enter number");
scanf("%d",&a);
mid=beg+end/2;
while(beg<=25&&data[mid]!=a)
{
if(a<data[mid])
end=mid-1;
else
beg=mid+1;
mid=int((beg+end)/2);}
if(data[mid]==a)
printf("location is %d",mid);
else
printf("number not exist");
}
PROGRAMME
POSTFIX NOTATION

POSTFIX KA PAGE ATCHMENT MAIN HAI WOO COPY KR LOO OR IS PAGE PR PRINT
OUT NIKAL LO
PROGRAMME OF STACK:
PUSH N POP:

#include<stdio.h>
#include<conio.h>
void push(void);
void pop(void);
int top=-1;
int stack[5];
main()
{
char ch;
char op;
clrscr();
while (ch!='n')
{
printf("\n \n to continue this programme press key 'y'\n to
terminat eprograme prees key 'n'");
ch=getche();
if(ch=='y')
{
printf("\n \n choose 'I' for push,'D' for pop");
op=getche();
switch(op)
{
case 'I':
{
push();
}
break;
case 'D':
{
pop();
}
break;
}
}
}}
void push (void)
{
if(top==4)
{
printf("overflow");
}
else{
printf("push on stack:%d",stack[++top]);
}
}
void pop(void)
{
if(top==-1)
{
printf("underflow");
}
else
{
printf("pop on stack :%d",stack[top--]);
}
}
2.BUBBLE SORT
#include<stdio.h>
#include<conio.h>
main()
{
int DATA[]={10,39,56,88,30,20};
int PTR,j,k=0;
clrscr();
for(k=0;k<6;k++)
{
PTR=0;
while(PTR<=6-k)
{
if(DATA[PTR]>DATA[PTR+1])
{
j=DATA[PTR+1];
DATA[PTR+1]=DATA[PTR];
DATA[PTR]=j;
}
PTR++;

}
}
for(k=0;k<6;k++)
{
printf("%d ",DATA[k]);
}
}
3.BINARY SEARCH
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int data[15]={5,10,15,20,25};
int beg=0;
int end=4;
int mid,item;
clrscr();
printf("enter no");
scanf("%d",&item);
mid=beg+end/2;
{
while(beg<=end && data[mid]!=item)
{
if(item<data[mid])
end=mid-1;
else
beg=mid+1;
mid=int((beg+end)/2); }
if(data[mid]==item)
printf("local %d",mid);
else
printf("number not exist");
}
}
PROGRAMME:

LINKED LIST:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<alloc.h>
struct list{
char item[10];
struct list*next;

{
typedef struct list node;
void create(node*pt);
node*insert(node*pt);
node*remove(node*pt);
void display(node*pt);

main()
{
node*start;
int choice;
clrscr();
do
{
printf("\nenter the choice\n");
printf("1-create the link list");
printf("2-add a component");
printf("3-delete a componene");
printf("4-end\n");
printf("%d",&choice);
switch(choice)
{
case1:
start=(node*)malloc(sizeof(node));
create(start);
printf("\n");
display(start);
continue;
case2:
start=insert(start);
printf("\n");
display(start);
continue;
case3:
start=remove(start);
printf("\n");
display(start);
continue;
default;
printf("end of computation");
}
}
while(choice!=4)
}
}
void create(node*record)
{
printf("dataitem(type end when finished):");
scanf("%s",record->item);
if(strcmp(record->item,"end")==0)
record->next=NULL;
else
{
record->nex=(node*)malloc(sizeof(node));
create(record->next);
}
return;
}
void display(node*record)
{
if(record->next!=NULL)
{
printf("%s",record->item);
display(record->next);
}
return;
}
node*insert(node*first);
{
node*locate(node*,char[]);
node*newrecord;
node*tag;
char newitem[40];
char target[40];
printf("enter new data item");
scanf(%ds",new item);
printf("plce before(type\'end\;'iflast):");
scanf("%s",target);
if(strcmp(first->item,target)==0)
{
newrecord=(node*)malloc(signof(node));
strcpy(newrecord->item,newitem);
newrecord->next=first;
first=newrecord;
}
else
{
tag=locate(first,target);
if(tag==NULL)
printf("\nmatch not found-please try again\n");
else
{
newrecord=(node*)malloc(signof(node));
strcpy(newrecord->item,newitem);
newrecord->next=tag->next;
tag->next=newrecord;
}
}
return(first);
}
node*locate(node*record,char target[]);
{
if strcmp(record->next->item,target)==0)
return(record);
if(record->next->next==NULL)
return(NULL);
else
locate(record->next,target);
}
node*remove(node*first)
{
node*locate(node*nchar[]);
node*tag;
node*temp;
char target[10];
printf("data item to be deleted");
scanf("%s",target);
if(strcmp(first->item,target)==0)
{
temp=first->next;
free(first);
first=temp;
}
else
{
tag=locate(first,target);
if(tag==NULL)
printf("\nmatch not found\n");
else
{
temp=tag->next;
free(tag->next);
tag->next=temp;
}
}
return(first);
}

PROGRAMME OF SUBSTRING

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char string[20];
int b,position,a,lenght;
clrscr();
printf("enter string");
gets(string);
printf("enter position");
scanf("%d",&position);
printf("enter lenght");
scanf("%d",&lenght);
b=position+lenght;
for(a=position;a<b-1;a++)
{
printf("%c",string[a]);
}
PROGRAMME OF TREES

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

struct treenode

{
struct treenode *leftchild;
int data;
struct treenode *rightchild;
};

inorder(struct treenode *);


preorder(struct treenode *);
postorder(struct treenode *);
isert(struct treenode **,int);

void main()

{
struct treenode *bt;
int req,i=1,num;
bt=NULL;
clrscr();

printf("specify the number");


scanf("%d",&req);

while (i++<=req)
{
printf("enter data = ");
scanf("%d",&num);
insert(&bt,num);
}

printf("\ninorder");
inorder(bt);
printf("\npreorder");
preorder(bt);
printf("\npostorder");
postorder(bt);
}

insert(struct treenode **sr,int num)

{
if(*sr==NULL)
{
*sr=malloc(sizeof(struct treenode));

(*sr)->leftchild=NULL;
(*sr)->data=num;
(*sr)->rightchild=NULL;
return;
}
else
{
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num);
else
insert(&((*sr)->rightchild),num);
}
return;
}

inorder(struct treenode *sr)

{
if(sr!=NULL)
{
inorder(sr->leftchild);
printf("\t%d",sr->data);
inorder(sr->rightchild);
}
else
return;
}

preorder(struct treenode *sr)

{
if(sr!=NULL)
{
printf("\t%d",sr->data);
preorder(sr->leftchild);
preorder(sr->rightchild);
}
else
return ;
}

postorder(struct treenode *sr)


{
if(sr!=NULL)
{
postorder(sr->leftchild);
postorder(sr->rightchild);
printf("\t%d",sr->data);
}
else
return;
}
PROGRAMME OF SUBSTRING DELETE

#include<stdio.h>
#include<conio.h>
#include<string.h>
void del(char str[],int n,int l);
main()
{
char string[80];
int position,lenght,l;
clrscr();
printf("enter string");

gets(string);
printf("enter position n lenght");
scanf("%d %d",&position,&lenght);
del(string,position,lenght);
puts(string);

}
void del(char str[],int n,int l)
{
strcpy(&str[n-1],&str[n+l-1]);
}
PROGRAMME
TOWER OF HANOI
#include<stdio.h>
#include<conio.h>
void tower(int n,int a,int b,int c);
main()
{
int disk;
char x,y,z;
clrscr();
printf("enter no of disk to move");
scanf("%d",&disk);
tower(disk,1,2,3);
getch();
}
void tower(int n,int a,int b,int c);
{
if(n>0)
{
tower(n-1,a,c,b);
printf("step%d-->%d",a,b);
tower(n-1,c,b,a);
}
}
PROGRAMME:
QUES
#include<stdio.h>
#include<conio.h>
void ins(void);
void del(void);

int front=-1,rear=-1,queue[5];
main()
{

char ch;
char op;
clrscr();
while(ch=='N')
{
printf("\n\nto continue program press 'y'&terminate press 'N'");
ch=getche();
if(ch=='y')
{
printf("\nchoose 'i' for insertion and choose 'd' for deletion");
op=getche();
switch(op)
{
case 'i':
{
ins();
}
break;
case 'd':
{
del();
}
break;
}
}
}
}
void ins(void)
{
if(rear==4)
{
printf("overflow");
}
else
{
printf("after insertion an queue %d",queue[rear++]);
printf("\nfront=%d,rear=%d",front,rear);
}
}
void del(void)
{
if(front==rear)
{
printf("underflow");
}
else
{
printf("after deletion from que %d",queue[front++]);
printf("\nfront=%d,rear=%d,front,rear");
}
}
PROGRAMME OF PATTERN MATCHING

You might also like