Professional Documents
Culture Documents
org
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct node
int data;
};
void main()
int num,loc;
char choice;
do
clrscr();
printf("—————————————\n");
printf("\n 7. Exit");
oper:
gotoxy(1,15);printf(" ");
choice=getch();
switch(choice)
char ans;
case ’1′:
do
{
printf("Enter any number : ");
scanf("%d",&num);
append(&p,num);
fflush(stdin);
ans=getchar();
}while(ans!=’n');
break;
case ’2′:
scanf("%d",&num);
add_at_begin(&p,num);
break;
case ’3′:
scanf("%d",&loc);
scanf("%d",&num);
in_middle(&p,loc,num);
break;
case ’4′:
scanf("%d",&num);
del(&p,num);
break;
case ’5′:
getch();
break;
case ’6′:
display(p);
getch();
break;
case ’7′:
printf("\n Quiting…….");
getch();
exit(0);
break;
default:
getch();
goto oper;
}while(choice!=7);
temp=*q;
if(*q==NULL)
temp->data=num;
temp->link=NULL;
*q=temp;
else
temp=*q;
while(temp->link!=NULL)
temp=temp->link;
r->data=num;
r->link=NULL;
temp->link=r;
}
void display(struct node *q)
if(q==NULL)
getch();
goto last;
while(q!=NULL)
printf("\n%d",q->data);
q=q->link;
last:
int c=0;
if(q==NULL)
getch();
goto last;
}
while(q!=NULL)
c++;
q=q->link;
last:
return c;
if(*q==NULL)
getch();
goto last;
else
temp->data=num;
temp->link=*q;
*q=temp;
last:
getch();
int c=1,flag=0;
temp=*q;
if(*q==NULL)
getch();
goto last;
else
while(temp!=NULL)
if(c==loc)
n->data=num;
n->link=temp->link;
temp->link=n;
flag=1;
c++;
temp=temp->link;
if(flag==0)
getch();
else
printf("Data Inserted");
getch();
last:
getch();
if(*q==NULL)
{
getch();
goto last;
else
int flag=0;
temp=*q;
while(temp!=NULL)
if(temp->data==num)
if(temp==*q)
*q=temp->link;
else
old->link=temp->link;
free(temp);
flag=1;
else
{
old=temp;
temp=temp->link;
if(flag==0)
else
getch();
last:
getch();
Sample output:
—————————————
4. Deleting a node
7. Exit
Enter your choice : Enter any number : 12
12
23
34
Data Inserted
12
23
0
34
#include<stdio.h>
#include<conio.h>
void create(void);
void display(void);
void queue_in(void);
void queue_out(void);
void front_rear(void);
int front,rear;
int q[25];
void create()
int n;
scanf("%d",&n);
for(rear=0;rear<n;rear++)
scanf("%d",&q[rear]);
}
front=0;
void display()
if(rear!=0)
for(front=0;front<rear;front++)
printf("%d",q[front]);
front=0;
else if(rear==0)
else if(rear==25)
void front_rear()
if(rear!=0)
else
void queue_in()
if(rear<25)
scanf("%d",&q[rear]);
++rear;
else
void queue_out()
if(rear!=0)
for(front=0;front<rear-1;++front)
q[front]=q[front+1];
rear–;
front=0;
int main()
int ch=0;
clrscr();
printf("creation \n");
create();
display();
getch();
do
clrscr();
printf("—————- \n");
scanf("%d\n",&ch);
switch(ch)
case 1 :queue_in();
display();
break;
case 2 :queue_out();
display();
break;
case 3 :front_rear();
break;
case 4 :display();
break;
break;
getch();
while(ch!=5);
return (0);
}
Sample output:
QUEUE MANIPULATION
Creation
1234
Queue operations
—————-
5. Exit
12345
12345
2345
END
#include<stdio.h>
{
int data;
struct node*link;
}queue;
queue*getnode();
void releasenode(queue*p);
int isfull();
int isempty(queue*front);
void dequeue(queue**frontptr,queue**rearptr,int*value);
void peek(queue*front,int*value);
void view(queue*front);
int size(queue*front);
void displaymenu(void);
void main()
queue*front=NULL,*rear=NULL;
int choice,item;
displaymenu();
while(1)
printf("\n?");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(isfull())
else
fflush(stdin);
scanf("%d",&item);
enqueue(&front,&rear,item);
break;
case 2:
if(isempty(front))
else
dequeue(&front,&rear,&item);
break;
case 3:
if(!isempty(front))
{
peek(front,&item);
else
printf("\nQueue is empty");
break;
case 4:
break;
case 5:
view(front);
break;
default:
exit(0);
void displaymenu()
printf("\n\t1.enqueue");
printf("\n\t2.dequeue");
printf("\n\t3.peek");
printf("\n\t4.size");
printf("\n\t5.view");
printf("\n\t6.exit");
void releasenode(queue*p)
free(p);
queue*getnode()
int size;
queue*newnode;
size=sizeof(queue);
newnode=(queue*)malloc(size);
return(newnode);
int isempty(queue*front)
if(front==NULL)
return 1;
else
return 0;
int isfull()
queue*newnode;
newnode=getnode();
if(newnode==NULL)
return 1;
releasenode(newnode);
return 0;
queue*newnode;
if(isfull())
return;
newnode=getnode();
newnode->data=value;
newnode->link=NULL;
if(*frontptr==NULL)
*frontptr=newnode;
else
(*rearptr)->link=newnode;
*rearptr=newnode;
void dequeue(queue**frontptr,queue**rearptr,int*value)
queue*tempnode;
if(isempty(*frontptr))
return;
tempnode=*frontptr;
*frontptr=(*frontptr)->link;
if(*frontptr==NULL)
*rearptr=NULL;
*value=tempnode->data;
releasenode(tempnode);
void peek(queue*front,int*value)
if(isempty(front))
return;
}
*value=front->data;
void view(queue*front)
if(isempty(front))
return;
printf("\nQueue contains…front->");
while(front!=NULL)
printf("%d–>",front->data);
front=front->link;
printf("rear/n");
int size(queue*front)
int count=0;
if(front==NULL)
return count;
for(;front!=NULL;)
count++;
front=front->link;
return count;
Sample output:
1. enqueue
2. dequeue
3. peek
4. size
5. view
6. exit
?2
?3
Queue is empty
?4
?5
?1
?1
?5
Queue contains…front->11–>22–>33–>rear/n
?4
?2
?6
#include<stdio.h>
#include<conio.h>
void create(void);
void push(0void);
void pop(void);
void display(void);
void topelement(void);
int a[25];
int top;
void create()
int i;
scanf("%d",&top);
for(i=0;i<top;++i)
scanf("%d",&a[i]);
return;
void display()
int i;
for(i=top-1;i>=0;–i)
printf("%d",a[i]);
getch();
}
void push()
if(top==25)
else
scanf("%d",&aTop);
top++;
return;
void pop()
if(top==0)
else
return;
void topelement()
int t;
if(top==0)
else
t=top-1;
return;
void main()
int ch;
clrscr();
create();
display();
do{
clrscr();
printf("—————-\n");
scanf("%d\n",&ch);
switch(ch)
case 1: push();
display();
break;
case 2: pop();
display();
break;
case 3: topelement();
// display();
break;
case 4: display();
break;
break;
getch();
}while(ch !=5);
Sample output:
4321
Stack operations
—————-
1. PUSH
2. POP
3. TOP ELEMENT
4. Displaying the stack
5. Quit
04321
14321
END
STACK USING LINKED LIST
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int data;
}stack;
int size();
void view();
int isFull();
int isEmpty();
void displayMenu();
stack *getNode();
stack *topstk=NULL;
void main()
{
int data,status,choice;
displayMenu();
while(1)
printf("\n\n ?");
scanf("%d",&choice);
switch(choice)
case 1:
fflush(stdin);
scanf("%d",&data);
status=push(data);
if(status == -1)
break;
case 2:
status=pop(&data);
if(status == -1)
else
break;
case 3:
status=peek(&data);
if(status == -1)
printf("\nstack is empty!!!");
else
break;
case 4:
break;
case 5:
view();
break;
default:
exit(0);
void displayMenu()
printf("\n\t1.push");
printf("\n\t2.pop");
printf("\n\t3.peek");
printf("\n\t4.size");
printf("\n\t5.view");
printf("\n\t6.exit");
stack *getNode()
return((stack *)malloc(sizeof(stack)));
free(newnode);
stack *newptr;
if(isFull())
return -1;
newptr=getNode();
newptr->data=value;
newptr->next=topstk;
topstk=newptr;
return 0;
stack *temp;
if(isEmpty())
return -1;
temp=topstk;
topstk=topstk->next;
*value=temp->data;
releaseNode(temp);
return 0;
stack *temp;
if(isEmpty())
return -1;
temp=topstk;
*value=temp->data;
return 0;
int isEmpty()
if(topstk==NULL)
return 1;
else
return 0;
int isFull()
stack *temp;
temp=getNode();
if(temp==NULL)
return 1;
else
releaseNode(temp);
return 0;
}
int size()
stack *top;
int count=0;
for(top=topstk;top!=NULL;top=top->next)
count++;
return count;
void view()
stack *top;
if(isEmpty())
return;
for(top=topstk;top!=NULL;top=top->next)
printf("–>%d",top->data);
Sample output:
Representation of stack using linked list
1. Push
2. Pop
3. Peek
4. Size
5. View
6. Exit
?2
?3
stack is empty
?4
stack is elements=0
?1
?1
?5
?4
stack elements=2
?2
TRAVERSALS
#include<stdio.h>
#include<conio.h>
int data;
struct node*left;
struct node*right;
}tree;
tree* createtree();
tree* insert(tree*,tree*);
void preorder(tree*);
void inorder(tree*);
void postorder(tree*);
void main()
int ch;
tree*bt;
clrscr();
bt=createtree();
printf("\n \t2.exit\n");
scanf("%d",&ch);
if(bt==NULL)
return;
switch(ch)
case 1:
preorder(bt);
inorder(bt);
postorder(bt);
break;
case 2:
printf("\n exit");
exit(0);
}
getch();
free(bt);
tree* createtree()
char ch;
tree *bt=NULL,*temp;
do
temp=(tree*)malloc(sizeof(struct node));
scanf("%d",&temp->data);
temp->left=NULL;
temp->right=NULL;
bt=insert(bt,temp);
fflush(stdin);
scanf("%c",&ch);
}while((ch==’y')||(ch==’Y'));
return bt;
{
if(bt==NULL)
return temp;
else if(temp->data<bt->data)
bt->left=insert(bt->left,temp);
else if(temp->data>bt->data)
bt->right=insert(bt->right,temp);
else if(temp->data==bt->data)
return bt;
return bt;
if(bt)
inorder(bt->left);
printf("%d",bt->data);
inorder(bt->right);
if(bt)
printf("%d",bt->data);
preorder(bt->left);
preorder(bt->right);
if(bt)
postorder(bt->left);
postorder(bt->right);
printf("%d",bt->data);
SAMPLE OUTPUT:
2
Want to add more data y/n: Y
1.traversals
2.exit
QUICK SORT
#include<stdio.h>
#include<conio.h>
#define MAX 25
void input();
void output();
void sort();
int a[MAX];
void input()
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
void output()
int i;
for(i=0;i<n;i++)
printf("%d \n",a[i]);
void sort()
int low=0;
int up=n-1;
qsort(a,&low,&up);
int i,j,t,mid;
i=*low;
j=*up;
mid=a[(*low+*up)/2];
while(i<=j)
while(a[i]<mid)
i++;
while(mid<a[j])
j–;
if(i<=j)
t=a[i];
a[i]=a[j];
a[j]=t;
i++;
j–;
if(*low<j)
qsort(a,low,&j);
if(i<*up)
qsort(a,&i,up);
void main()
clrscr();
input();
output();
sort();
output();
getch();
SAMPLE OUTPUT:
25
12
09
12
12
25
Abstract data type is a collection of value definitions and the operations on those values.
o Value definitions
o Operator definitions
Define a sequence.
S=<s0,s1,s2…….sn-1>
A basic data structure in C is called as the structure. A Structure is a group of items in which
each item is identified by its own identifier.
Example:
struct nametype
char first[10];
int roll;
}sname, ename;
What is the difference between a structure and union in C.
1. Same memory is used for all the members of union. At any time only one member can be
accessed.
2. Individual memory is used for structure members.
Define a Stack.
A stack is an ordered collection of items into which new items may be inserted and from which
items may be deleted at one end called the top of the stack.
Push(s,i);
i=pop(s);
#define STACKSIZE
struct stack
int top;
int items[STACKSIZE];
};
Examples:
Define a queue.
A queue is a ordered collection of items from which itesm may be deleted at one end (called the
front of the queue) and into which items may be inserted at the other end (called the rear of the
queue). It is also called as First in First out (FIFO).
struct queue
int items[MAXQUEUE];
}q;
The priority queue is a data structure in which the intrinsic ordering of the elements does
determine the results. There are two types
is a collection of items into which items can be inserted arbitrarily and from which only the
smallest item can be removed.
Sometimes it is desirable to keep an extra node at the front of the list. Such a node does not
represent an item in the list and is called the header node or a list header.
struct node
int info;
};
16. How to create a node of a Doubly linked list using dynamic variables?
struct node
int info;
};
A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint
subsets. One subset is the left and one subset of the right and the third subset is the root of the
tree.
If every non leaf node in a binary tree has nonempty left and right subtrees, the tree is named as
the strictly binary tree.
19. Define traversal in tree and what is the different type of traversals in tree?
o preorder traversal
visit the root
Traverse the left subtree in preorder.
Traverse the right subtree in preorder
o inorder traversal
Traverse the left subtree in preorder.
visit the root
Traverse the right subtree in preorder
o postorder traversal
Traverse the left subtree in preorder.
Traverse the right subtree in preorder
visit the root
struct nodetype
int info;
};
The nodes which don’t have any sons are called as leaf nodes.
The leaf nodes are called as external nodes and the non leaf nodes are called as internal nodes.
To capture the concept of one function becoming proportional to another as it grows, a notation
is which is called as O notation.
A graph consists of a set of nodes (or vertices) and set of arcs (or edges).
25. Define weighted graph.
A number is associated with each arc of a graph is called a weighted graph. The number
associated with the arc is called the weight.
Given a connected weighted graph G, it is often desired to create a spanning tree T for G such
that the sum of the weights of the tree edges in T is as small as possible. Such a tree is called
minimum spanning tree.
Visits the successors of a visited node before visiting any of its brothers.
Visits all successors of a visited node before visiting any successors of any of those successors.
Example: Lists
A technique for searching an ordered list in which we first check the middle item and – based on
that comparison – "discard" half the data. The same procedure is then applied to the remaining
half until a match is found or there are no more items left.
· Insertions and deletions are easier than for contiguous (array) lists.
· With large records, moving pointers is easier and faster than moving the items th
emselves.
A binary search tree is a binary tree where each node contains a key such that:
· All keys in the left subtree lesser than the key in the root.
· All keys in the right subtree greater the key in the root.
· The left and right subtrees of the root are again binary search trees.
· A data type is a well-defined collection of data with a well-defined set of operations on it.