0% found this document useful (0 votes)
3 views11 pages

DataStructuresUsingC

The document contains implementations of various data structures including stacks, queues, linked stacks, linked queues, circular linked queues, double-ended circular linked queues, and double linked lists in C. Each section provides the code for the data structure along with basic operations such as insertion, deletion, and display. The implementations demonstrate the use of arrays and linked nodes to manage data in a structured manner.

Uploaded by

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

DataStructuresUsingC

The document contains implementations of various data structures including stacks, queues, linked stacks, linked queues, circular linked queues, double-ended circular linked queues, and double linked lists in C. Each section provides the code for the data structure along with basic operations such as insertion, deletion, and display. The implementations demonstrate the use of arrays and linked nodes to manage data in a structured manner.

Uploaded by

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

1.

//implementation of stack using Arrays-


//stack is a linear data structure in which elements are pushed and poped in First-In-Last-
Out //manner
#define size 6
void push(int []);//array as parameter
int pop(int []);
int top=-1;//top=-1 stack is empty
#include<stdio.h>
void main() {
int stack[size],pval;
push(stack);
pop(stack);
getch();
}
void push(int stk[]){
// int i=0;
while(top!=size){
printf("\n enter values in stack ");
scanf("%d",&stk[++top]);
}
}
int pop(int stk[]){
int rv;
while(top!=-1){
rv=stk[top--];
printf("\n Stack values are %d",rv);
}
return 0;
}
2. //Implementation of Queue using Arrays
/* Queue is a linear data structure in which elements are inserted and deleted in First-
In-First-Out manner */
void insert(int []);
void display(int []);
int front,rear;
#define size 5
void main(){
int queue[size],front,rear;
//front=rear=-1;
insert(queue);
display(queue);
getch();
}
void insert(int queue[])
{
printf("enter %d values in the queue \n",size);
for(rear=0;rear<size;rear++){
scanf("%d",&queue[rear]);
}
}
void display(int queue[]){
printf("Values in the queue are \n");
for(front=0;front<size;front++){
printf("%3d",queue[front]);
}
}
-------------------------------------------------------------------------------------------------------

3. /* Linked Stack and Operations on stack are PUSH- POP- SEEK*/


#include<stdlib.h>
struct node{
int rollno;
struct node *next;
};
struct node *top=NULL;
void push(int);
void pop();
void seek();
void main() {
clrscr();
seek();
push(34);
push(56);
push(12);
push(67);
push(78);
pop();
//seek();
getch();
}
void push(int n) {
struct node *ptr=(struct node *)malloc(sizeof(struct node));
if(ptr==NULL) {
printf(" \n Memory Not allocated to ptr");
}
//end of if
else {
if(top==NULL) {
ptr->rollno=n;
top=ptr;
top->next=NULL;
}
else {
ptr->rollno=n;
ptr->next=top;
top=ptr;
}
//end of nested if
}//end of outer if
}
//--------------- end of push()-----------------------
void pop() {
struct node *dptr;
dptr=top;
while(dptr!=NULL) {
printf("\n Roll numbers = %d",dptr->rollno);
dptr=dptr->next;
}//end of while
}
void seek() {
if(top==NULL) {
printf(" Stack is empty");
}
else {
printf("\n top element address is %u and value=%d",top->next,top->rollno);
}
}
------------------------------------------------------------------------------------------------------------------
4. //Linked Queue and operations on QUEUE are INSERT-DELETE-SEEK FRONT AND REAR
ELEMENT
#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=NULL;
}
else {
ptr->id=id;
ptr->height=h;
ptr->weight=w;
ptr->next=NULL;
//front=ptr;
rear->next=ptr;
rear=ptr;
}//end of nested if
}//end of outer if
}// end of insert()

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;
*/
}
}
}

You might also like