Saturday, August 13, 2011
Data Structures (Queue operations using arrays)
#include<stdio.h>
#include<conio.h>
int queue[10];
int front = -1;
int rear = -1;
int n = 9;
void queue_insert(int data)
{
     if(front==n)
    {
         printf("Queue rull");
    }
    else if(front==-1)
    {
         queue[0] = data;
         front = rear = 0;
     }
     else
     {
         queue[++rear] = data;
    }
}
void queue_delete()
{
     if(front==-1 && rear==-1)
     {
         printf("Queue empty");
         exit(0);
     }
     else if(front==rear)
     {
          printf("\n%d deleted from the queue.\n",queue[front]);
         front=rear=-1;
     }
     else
     {
     printf("%d deleted from the queue.\n",queue[front]);
     front++;
     }
}
int main()
{
    queue_insert(10);
    queue_insert(20);
    queue_delete();
    getch();
}
Thursday, August 11, 2011
Data Structures (Stack operations using arrays)
#include<stdio.h>
#include<conio.h>
int top = -1;
int stack[10];
void push(int data)
{
stack[++top] = data;
printf("Data pushed\n");
}
void pop()
{
if(top!=-1)
{
printf("\n%d is popped out of the stack",stack[top]);
top--;
}
else
printf("Stack empty");
}
int main()
{
push(10);
push(20);
pop();
getch();
}
#include<conio.h>
int top = -1;
int stack[10];
void push(int data)
{
stack[++top] = data;
printf("Data pushed\n");
}
void pop()
{
if(top!=-1)
{
printf("\n%d is popped out of the stack",stack[top]);
top--;
}
else
printf("Stack empty");
}
int main()
{
push(10);
push(20);
pop();
getch();
}
Data Structures (Stack operations using linked lists)
#include<stdio.h>
#include<conio.h>
/*
Define the structure of the node with the data it holds and a link to another node.
*/
struct node
{
int item;
struct node *link;
};
/*
Declare a pointer top to point the TOP of the stack. Initially the pointer top is pointing to NULL
*/
struct node *top;
/*
The push function.
*/
void push(int data)
{
//create a temporary node, add data to it, link it with the stack and update the top
struct node temp;
if(&temp!=NULL) // if node is not created, then there is no memory for new node
{
temp.item = data;
temp.link = top;
top = &temp;
printf("Data Inserted\n");
}
else
printf("Stack is full");
}
/*
The pop function
*/
void pop()
{
struct node *temp;
if(top!=NULL)//top NULL implies that stack is empty
{
temp = top;
printf("popped data is:%d",temp->item);
top = top->link;
}
else
printf("Stack empty");
}
int main()
{
push(10);
push(20);
pop();
getch();
return 0;
}
#include<conio.h>
/*
Define the structure of the node with the data it holds and a link to another node.
*/
struct node
{
int item;
struct node *link;
};
/*
Declare a pointer top to point the TOP of the stack. Initially the pointer top is pointing to NULL
*/
struct node *top;
/*
The push function.
*/
void push(int data)
{
//create a temporary node, add data to it, link it with the stack and update the top
struct node temp;
if(&temp!=NULL) // if node is not created, then there is no memory for new node
{
temp.item = data;
temp.link = top;
top = &temp;
printf("Data Inserted\n");
}
else
printf("Stack is full");
}
/*
The pop function
*/
void pop()
{
struct node *temp;
if(top!=NULL)//top NULL implies that stack is empty
{
temp = top;
printf("popped data is:%d",temp->item);
top = top->link;
}
else
printf("Stack empty");
}
int main()
{
push(10);
push(20);
pop();
getch();
return 0;
}
Subscribe to:
Posts (Atom)