- Circular Queue (Array)
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 30
- struct queue {
- int que[MAX];
- int front;
- int rear;
- };
- struct queue items;
- struct queue *q = &items;
- void enque();
- void deque();
- void display();
- int Empty();
- int Full();
- int main(int argc, char *argv[]) {
- int ch = 1;
- do {
- switch(ch) {
- case 1: enque(); break;
- case 2: deque(); break;
- case 3: display(); break;
- }
- } while(ch != 0);
- return 0;
- }
- void enque() {
- int x;
- if(Full()) {
- } else {
- if(Empty()) {
- q->front = q->rear = 0;
- } else {
- q->rear = (q->rear + 1) % MAX;
- }
- q->que[q->rear] = x;
- }
- display();
- }
- void deque() {
- if(Empty()) {
- } else {
- if(q->front == q->rear) {
- q->front = q->rear = -1;
- } else {
- q->front = (q->front + 1) % MAX;
- }
- }
- display();
- }
- int Empty() {
- if(q->rear == -1) {
- return 1;
- } else {
- return 0;
- }
- }
- int Full() {
- if((q->rear + 1) % MAX == q->front) {
- return 1;
- } else {
- return 0;
- }
- }
- void display() {
- int i;
- if(Empty()) {
- } else {
- for(i = q->front; i != q->rear; i = (i + 1) % MAX) {
- }
- }
- }
- ----------------------------------------------------------------------------------------------------------------------------------------------
- Linked list (ALL)
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- struct node
- {
- int data;
- struct node *next;
- }*head =NULL;
- struct node* ptr;
- void beginsert()
- {
- struct node *ptr;
- int item;
- if(ptr == NULL)
- {
- }
- else
- {
- ptr->data = item;
- ptr->next = head;
- head = ptr;
- }
- }
- void lastinsert()
- {
- struct node *ptr,*temp;
- int item;
- if(ptr == NULL)
- {
- }
- else
- {
- ptr->data = item;
- if(head == NULL)
- {
- ptr -> next = NULL;
- head = ptr;
- }
- else
- {
- temp = head;
- while (temp -> next != NULL)
- {
- temp = temp -> next;
- }
- temp->next = ptr;
- ptr->next = NULL;
- }
- }
- }
- //Insert Node at inbetween of LL
- void randominsert()
- {
- int i,loc,item;
- struct node *ptr, *temp;
- if(ptr == NULL)
- {
- }
- else
- {
- ptr->data = item;
- temp=head;
- for(i=1;i<loc;i++)
- {
- temp = temp->next;
- if(temp == NULL)
- {
- return;
- }
- }
- ptr ->next = temp ->next;
- temp ->next = ptr;
- }
- }
- void begin_delete()
- {
- struct node *ptr;
- if(head == NULL)
- {
- }
- else
- {
- ptr = head;
- head = ptr->next;
- }
- }
- //Delete Node at Last of LL
- void last_delete()
- {
- struct node *ptr,*ptr1;
- if(head == NULL)
- {
- }
- else if(head -> next == NULL)
- {
- head = NULL;
- }
- else
- {
- ptr = head;
- while(ptr->next != NULL)
- {
- ptr1 = ptr;
- ptr = ptr ->next;
- }
- ptr1->next = NULL;
- }
- }
- //Delete Node at Mid of LL
- void random_delete()
- {
- struct node *ptr,*ptr1;
- int loc,i;
- ptr=head;
- for(i=1;i<=loc;i++)
- {
- ptr1 = ptr;
- ptr = ptr->next;
- if(ptr == NULL)
- {
- return;
- }
- }
- ptr1 ->next = ptr ->next;
- }
- void display()
- {
- struct node *ptr;
- ptr = head;
- if(ptr == NULL)
- {
- }
- else
- {
- while (ptr!=NULL)
- {
- ptr = ptr -> next;
- }
- if (ptr==NULL)
- /* code */
- }
- }}
- void search()
- {
- struct node *ptr;
- int item,i=0,flag;
- ptr = head;
- if(ptr == NULL) {
- else {
- while (ptr!=NULL) {
- if(ptr->data == item) {
- flag=0;
- }
- else
- {
- flag=1;
- }
- i++;
- ptr = ptr -> next;
- }
- if(flag==1)
- {
- }
- }
- }
- void main(){
- int c=0;
- printf("\n=========================================================================================");
- printf("\nEnter your choice : \n1.insert at beginning \n2.insert at end \n3.delete from beginning \n4.Delete from end \n5.Delete from mid \n6.Display \n7.Search \n0. exit");
- printf("\n=========================================================================================");
- while(c!=0){
- switch(c){
- case 1 : beginsert();break;
- case 2 : lastinsert();break;
- case 3 : begin_delete();break;
- case 4 : last_delete();break;
- case 5 : random_delete();break;
- case 6 : display();break;
- case 7 : search();break;
- }
- printf("\n=========================================================================================");
- printf("\nEnter your choice : \n1.insert at beginning \n2.insert at end \n3.delete from beginning \n4.Delete from end \n5.Delete from mid \n6.Display \n7.Search \n0. exit");
- printf("\n=========================================================================================\n");
- }
- }
- ----------------------------------------------------------------------------------------------------------------------------------------------
- Linked stack
- #include<stdio.h>
- #include<stdlib.h>
- struct node{
- int data;
- struct node *next;
- };
- struct node* top;
- int isStackEmpty(){
- if(top==NULL){ return 1;}
- else { return 0;
- }}
- void pop(){
- if(!isStackEmpty()){
- int item;
- item=top->data;
- top=top->next;
- }
- else{
- }
- }
- void push(int element){
- struct node *temp;
- temp->data=element;
- temp->next=NULL;
- if(top==NULL){
- top=temp;
- }
- else{
- temp->next=top;
- top=temp;
- }
- }
- void display(){
- else{
- }
- }
- void main(){
- int n=1;
- int element;
- top=NULL;
- while(n!=0){
- switch(n){
- case 2 : pop();break;
- break;
- case 4 : display();break;
- }
- printf("\n=================================>\nEnter choice \n1.Push 2.Pop 3.empty or not 4.displaytop \n0 exit\n=================================>\n");
- }
- }
- ----------------------------------------------------------------------------------------------------------------------------------------------
- Priority Queue
- //PriorityQueue
- //Lower the value higher the priority
- #include<stdio.h>
- #include<stdlib.h>
- struct Queue{
- int data;
- int pty;
- struct Queue *next;
- };
- struct Queue *head=NULL;
- void display();
- void insert();
- void delete();
- void main(){
- int ch=0;
- while(ch!=0){
- switch(ch){
- case 1 : insert();break;
- case 2 : delete();break;
- case 3 : display();break;
- }
- }
- }
- void display(){
- struct Queue *temp;
- temp=head;
- if(temp==NULL){
- }
- else {
- while(temp!=NULL){
- temp=temp->next;
- }
- }
- }
- void insert(){
- int element;
- int p;
- temp->data=element;
- temp->pty=p;
- temp->next = NULL;
- if(head==NULL){
- head=temp;
- }
- else{
- if(temp->pty < head->pty){
- temp->next=head;
- head=temp;
- }
- else{struct Queue *q=head;
- while(q->next!=NULL && temp->pty >= (q->next)->pty ){
- q=q->next;
- }
- temp->next=q->next;
- q->next=temp;
- }
- }
- display();
- }
- void delete(){
- struct Queue *temp=head;
- if (temp == NULL){
- }
- else{
- head=head->next;
- }
- display();
- }
- ----------------------------------------------------------------------------------------------------------------------------------------------
- Queue Array
- #include<stdio.h>
- #include<stdlib.h>
- #define MAX 30
- int queue[MAX];
- int front;
- int rear;
- front=-1;
- rear=-1;
- void display();
- void enque();
- void deque();
- int isEmpty();
- int isFull();
- int main(){
- int ch=1;
- do{
- switch(ch){
- case 1 : enque(); break;
- case 2 : deque(); break;
- case 3 : display(); break;
- }
- while(!0);
- return 0;
- }
- void enque(){
- int x;
- if(isFull())
- {
- }
- else if(isEmpty())
- {
- rear=front=0;
- queue[front]=x;
- }
- else
- {
- queue[++rear]=x;
- }
- display();
- }
- void deque(){
- if(isEmpty()){
- }
- else if(front==rear){
- front=rear=-1;
- }
- else{
- }
- display();
- }
- void display(){
- int i;
- if(!isEmpty()){
- for(i=front;i<=rear;i++){
- }
- }
- else{
- }
- }
- int isEmpty(){
- if(rear==-1||front==-1){
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int isFull(){
- if(rear==MAX-1){
- return 1;
- }
- else
- {
- return 0;
- }
- }
- ----------------------------------------------------------------------------------------------------------------------------------------------
- Queue Linked list (Same with slight change)
- #include <stdio.h>
- #include <stdlib.h>
- struct node
- {
- int info;
- struct node *ptr;
- }*front,*rear,*temp,*front1;
- int frontelement();
- void enq(int data);
- void deq();
- void empty();
- void display();
- void create();
- void queuesize();
- int count = 0;
- void main()
- {
- int no, ch, e;
- create();
- while (1)
- {
- switch (ch)
- {
- case 1:
- enq(no);
- break;
- case 2:
- deq();
- break;
- case 3:
- e = frontelement();
- if (e != 0)
- else
- break;
- case 4:
- empty();
- break;
- case 5:
- queuesize();
- break;
- case 6:
- display();
- break;
- case 7:
- break;
- default:
- break;
- }
- }
- }
- void create()
- {
- front = rear = NULL;
- }
- void queuesize()
- {
- }
- void enq(int data)
- {
- if (rear == NULL)
- {
- rear->ptr = NULL;
- rear->info = data;
- front = rear;
- }
- else
- {
- rear->ptr = temp;
- temp->info = data;
- temp->ptr = NULL;
- rear = temp;
- }
- count++;
- }
- void display()
- {
- front1 = front;
- if ((front1 == NULL) && (rear == NULL))
- {
- return;
- }
- while (front1 != rear)
- {
- front1 = front1->ptr;
- }
- if (front1 == rear)
- }
- void deq()
- {
- front1 = front;
- if (front1 == NULL)
- {
- return;
- }
- else
- if (front1->ptr != NULL)
- {
- front1 = front1->ptr;
- front = front1;
- }
- else
- {
- front = NULL;
- rear = NULL;
- }
- count--;
- }
- int frontelement()
- {
- if ((front != NULL) && (rear != NULL))
- return(front->info);
- else
- return 0;
- }
- void empty()
- {
- if ((front == NULL) && (rear == NULL))
- else
- }
- ---------------------------------------------------------------------------------------------------------------------------------------------
- Queue using Array structure
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 30
- struct queue{
- int que[MAX];
- int front;
- int rear;
- };
- struct queue items;
- struct queue *q=&items;
- void enque();
- void deque();
- void display();
- int Empty();
- int Full();
- int main(int argc,char *argv[]){
- q->front =-1;
- q->rear=-1;
- int ch=1;
- while(ch!=0){
- switch(ch){
- case 1 : enque(); break;
- case 2 : deque(); break;
- case 3 : display(); break;
- }
- return 0;
- }
- void enque(){int x;
- if(Full()){
- }
- else if (Empty()){
- q->front=q->rear=0;
- q->que[q->front]=x;
- }
- else
- {
- q->que[++q->rear]=x;
- }
- display();
- }
- void deque(){
- if(Empty()){
- }
- else if (q->front==q->rear ){
- q->rear=q->front=-1;
- }
- else{
- }
- display();
- }
- int Empty(){
- if(q->rear==-1){
- return 1;
- }
- else return 0;
- }
- int Full(){
- if (q->rear==MAX-1) return 1;
- else return 0;
- }
- void display(){
- int i;
- else
- for(i=q->front;i<=q->rear;i++){
- }
- }
- ----------------------------------------------------------------------------------------------------------------------------------------------
- Queue using linked list
- #include<stdio.h>
- #include<stdlib.h>
- struct Queue{
- int data;
- struct Queue *next;
- };
- int size=0;
- struct Queue *front=NULL;
- struct Queue *rear=NULL;
- void display(){
- struct Queue *temp=front;
- else
- while(temp!=NULL){
- temp=temp->next;
- }
- }
- void enque(){
- temp->next=NULL;
- if(front==NULL){
- front=rear=temp;
- size++;
- }
- else{
- rear->next=temp;
- rear=temp;
- size++;
- }
- display();
- }
- void deque(){
- if(front==NULL){
- }
- else{
- struct Queue *temp=front;
- front=front->next;
- size--;
- }
- display();
- }
- int main(){
- int ch;
- while(ch!=0){
- switch(ch){
- case 1 : enque();break;
- case 2 : deque();break;
- case 3 : display();break;
- }
- }
- }
- ----------------------------------------------------------------------------------------------------------------------------------------------
- Queue
- #include<stdio.h>
- #include<stdlib.h>
- #define MAX 20
- //declaration
- void enqueue();
- void dequeue();
- void display();
- int queue[MAX];
- int rear=-1;
- int front=-1;
- //insertion
- void enqueue()
- {
- int info,i;
- if(rear==MAX-1)
- else
- {
- if(front==-1)
- front=0;
- rear=rear+1;
- queue[rear]=info;
- }
- display();
- }
- //deleteion
- void dequeue()
- {
- if(front==-1)
- else
- {
- front=front+1;
- }
- display();
- }
- //display
- void display()
- {
- int i;
- if(front==-1)
- else
- {
- for(i=front;i<=rear;i++)
- {
- }
- }
- }
- //driver code
- void main(){
- int choice;
- while (1)
- {
- switch (choice)
- {
- case 1:
- enqueue();
- break;
- case 2:
- dequeue();
- break;
- case 3:
- display();
- break;
- case 4:
- default:
- }
- }
- }
- ----------------------------------------------------------------------------------------------------------------------------------------------
- Stack
- #include <stdio.h>
- #include<stdlib.h>
- int stack[50];
- int maxcapacity=50;
- int top=-1;
- int empty(){
- if(top == -1) return 1;
- else return 0;
- }
- int isfull(){
- if(top == maxcapacity-1) return 1;
- else return 0;
- }
- void pop(){
- }
- void push(int element){
- if(!isfull()){
- stack[++top]=element;}
- else {
- }
- if(!empty()){
- else{
- }
- void main(){
- int n=1;
- int element;
- printf("=================================>\nEnter choice \n1.Push 2.Pop 3.empty or not 4.display \n0 exit \n-->");
- while(n!=0){
- switch(n){
- case 2 : pop();break;
- case 4 : display();break;
- }
- printf("\n=================================>\nEnter choice \n1.Push 2.Pop 3.empty or not 4.displaytop \n0 exit \n--> ");
- }
- }
Raw Paste