DataStructuresUsingC
DataStructuresUsingC
void display() {
struct node *dptr=front;
while(dptr!=NULL) {
printf("\n Id= %d height=%f weight=%f",dptr->id,dptr->height,dptr->weight);
dptr=dptr->next;
}
}
float seekFront() {
printf ("\n Front Element is Id= %d height=%f weight=%f",front->id,front->height,
front->weight);
return 0.0;
}
float seekRear() {
printf ("\n Rear Element is Id= %d height=%f weight=%f",rear->id,rear->height,
rear->weight);
return 0.0;
}
5. //Circular Linked Queue and operations on QUEUE are INSERT-DELETE-SEEK FRONT AND
REAR ELEMENT-isEMPTY
/* In Circular Queue*/
#include<stdlib.h>
struct node {
int id;
float height;
float weight;
struct node *next;
};
struct node *front=NULL;
struct node *rear=NULL;
void insert(int,float,float);
void display();
float seekFront();
float seekRear();
void main() {
clrscr();
insert(10,5.6,50.36);
insert(11,5.7,60.66);
insert(12,5.5,66.66);
insert(13,6.0,70.80);
display();
seekFront();
seekRear();
getch();
}//end of main ()
void insert(int id,float h,float w) {
struct node *ptr=(struct node *)malloc(sizeof(struct node));
if(ptr==NULL){
printf("Memory Not Available");
}
else {
if(front==NULL) {
ptr->id=id;
ptr->height=h;
ptr->weight=w;
front=ptr;
rear=ptr;
front->next=ptr;
}
else {
ptr->id=id;
ptr->height=h;
ptr->weight=w;
ptr->next=front;
rear->next=ptr;
rear=ptr;
//front->next=ptr;
}//end of nested if
}//end of outer if
}// end of insert()
void display() {
struct node *dptr=front;
if(front==NULL && rear==NULL )
{
printf("queue is empty");
}
else {
while(dptr!=NULL) {
printf("\n Id= %d height=%f weight=%f",dptr->id,dptr->height,dptr->weight);
dptr=dptr->next;
}
}
}
float seekFront() {
printf ("\n Front Element is Id= %d height=%f weight=%f",front->id,front-
>height,front->weight);
return 0.0;
}
float seekRear() {
printf ("\n Rear Element is Id= %d height=%f weight=%f",rear->id,rear-
>height,rear->weight);
return 0.0;
}
6. //DoubleEnded Circular Linked Queue and operations on QUEUE are INSERT-DELETE-
SEEK FRONT AND REAR ELEMENT-isEMPTY
/* In Circular Queue front node address is stored in the rear node */
#include<stdlib.h>
struct node {
int id;
float height;
float weight;
struct node *next;
struct node *prev;
};
int count=0;
struct node *front=NULL;
struct node *rear=NULL;
void createNode(int,float,float);
void insertNode(int,int,float,float);
void display();
float seekFront();
float seekRear();
void insertNode(int at,int id,float h,float w) {
struct node *fptr=front;
while(fptr!=NULL) {
if(id==fptr->id) {
// printf("\n Element %d Found",fptr->id);
// count++;
// break;
struct node *iptr=(struct node *)malloc(sizeof(struct node));
}
fptr=fptr->next;
}
// if(count>0){
// printf("\n --------------Element Found---------------");
// }
}
void main() {
clrscr();
createNode(10,5.6,50.36);
createNode(11,5.7,60.66);
createNode(12,5.5,66.66);
createNode(13,6.0,70.80);
display();
seekFront();
seekRear();
insertNode(4,12,25.36,45.25);
getch();
}//end of main ()
void createNode(int id,float h,float w) {
struct node *ptr=(struct node *)malloc(sizeof(struct node));
if(ptr==NULL){
printf("Memory Not Available");
}
else {
if(front==NULL) {
ptr->id=id;
ptr->height=h;
ptr->weight=w;
front=ptr;
rear=ptr;
front->next=NULL;
front->prev=NULL;
}
else {
ptr->id=id;
ptr->height=h;
ptr->weight=w;
ptr->next=NULL;
ptr->prev=rear;
rear->next=ptr;
rear=ptr;
}//end of nested if
}//end of outer if
}// end of insert()
void display() {
struct node *dptr=rear; // to display from back to front
//struct node *dptr=front //to display from front to back
if(front==NULL && rear==NULL )
{
printf("queue is empty");
}
else {
while(dptr!=NULL) {
printf("\n Id= %d height=%f weight=%f",dptr->id,dptr->height,dptr->weight);
dptr=dptr->prev;//to display from back to front
//dptr=dptr->next;//to display from front to back
}
}
}
float seekFront() {
printf ("\n Front Element is Id= %d height=%f weight=%f",front->id,
front->height,front->weight);
return 0.0;
}
float seekRear() {
printf ("\n Rear Element is Id= %d height=%f weight=%f",rear->id,rear->height,
rear->weight);
return 0.0;
}
7. //Double Linked List
#include<stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head=NULL;
struct node *tail=NULL;
struct node *start=NULL;
void insertNode(int);
void displayNodes();
void main(){
clrscr();
insertNode(5);
insertNode(10);
insertNode(12);
insertNode(45);
displayNodes();
}
void displayNodes() {
struct node *dptr;
dptr=head;
while(dptr!=NULL) {
printf("\n data=%d",dptr->data);
dptr=dptr->prev;
}
}
void insertNode(int n){
struct node *ptr=(struct node *)malloc(sizeof(struct node));
if(ptr==NULL) {
printf("Not enough memory available");
}
else {
if(head==NULL) {
ptr->data=n;
head=ptr;
tail=ptr;
start=ptr;
head->next=NULL;
head->prev=NULL;
}
else {
ptr->data=n;
ptr->prev=head;
head->next=ptr;
head=ptr;
head->next=NULL;
/* for circular doubly linked list
start->prev=ptr;
head->next=start;
*/
}
}
}