Showing posts with label Data Structures Programs. Show all posts
Showing posts with label Data Structures Programs. Show all posts

Friday, 19 January 2018

C program to implement queue ADT using singly linked list

C program for implementing Queue ADT using singly linked list explained, How to implement Queue ADT using singly linked list? How to implement Queue data structure using linear linked list in C?

C Program for implementation of Queue ADT using linked list


#include<stdio.h>
#include <stdlib.h>

struct QueueNode //Node definition
{
int data; //To store the data element
struct QueueNode *next; //To store the pointer to the next node
};
typedef struct QueueNode node;
/* Next two statements declare front and rear pointers to point the first and the last elements respectively. They are set to NULL initially. */
node *front = NULL;
node *rear = NULL; //rear

node* createNode() //Function createNode to create a new node
{
node *temp;
temp = (node *) malloc(sizeof(node)) ; //Dynamic memory allocation
printf("\n Enter data to enqueue : ");
scanf("%d", &temp -> data); //Reads the data for new element enqueued
temp -> next = NULL; //sets the next of new node with NULL
return temp;
}
void enQueue() //To insert an element into the queue
{
node *newnode;
newnode= createNode(); //Calls the createNode function
if(front == NULL) //if front is NULL, the queue is empty
{
/* For the first time insertion, we initialize the front and rear pointers to point to the newly created node by assigning the address of first element of newnode to front and rear in the following two statements */
front = newnode;
rear = newnode;
}
else
{
/* If the queue has nodes in it, the current rear node’s next pointer, ie rear->next is assigned with the address stored in new node. And, the new node becomes the new rear. */
rear -> next = newnode; //here newnode has the starting address of first element in newnode
rear=newnode;
}
printf("\n\n Data added to the Queue..");
}
void deQueue()
{
node *temp;
if(front == NULL)
{
printf("\n\n\t Empty Queue..");
return;
}
/* To dequeue a node, the front pointer has to be set to point to the next node in the queue. (In array implementation, we have incremented front with front + 1). This can be done by assigning the current front node’s next element, ie., front->next to new front. */
temp = front; //current front is stored in a temporary variable
front = front -> next; //what front points to is stored in front
printf("\n\n\t Deleted element from queue is %d ", temp -> data);
free(temp); //free (de-allocate) the memory used for temp
}
void displayQ()
{
node *temp;
if(front == NULL)
{
printf("\n\n\t\t Empty Queue ");
}
else
{
/* From front node, displays the data, ie temp->data and move the pointer to the next node and so on */
temp = front;
printf("\n\n\n\t\t Elements in the Queue are: ");
while(temp != NULL)
{
printf("%5d ", temp -> data); //displays the data in current node
temp = temp -> next; //Assign the current node with the address store in the next pointer
}
}
}

int main()
{
int n;
while(1)
{
printf("\n\n Linked list implementation of Queue operations ");
printf("\n ----------------------------------------------\n");
printf("\n 1. Enqueue ");
printf("\n 2. Dequeue ");
printf("\n 3. Display");
printf("\n 4. Quit ");
printf("\n Enter your choice: ");
scanf("%d", &n);
switch(n)
{
case 1 :
enQueue();
break;
case 2 :
deQueue();
break;
case 3 :
displayQ();
break;
case 4:
return;
}
}
}




***************











Wednesday, 17 January 2018

C program to implement stack ADT using singly linked list

C program for implementing Stack ADT using singly linked list explained, How to implement stack ADT using singly linked list? How to implement stack data structure using linear linked list in C?


Implementation of Stack using singly linked list


// Program to implement stack using singly linked list
# include <stdio.h>
# include <stdlib.h>

struct stack //structure stack has one data element and one pointer to the next stack node
{
int data;
struct stack *next;
};
/*structure is like our own data type. We can declare variables using the syntax struct structure_name. typedef in the following line helps in giving a simple name so that we can use that name wherever we need to use struct structure_name. For example, in the following sentence, we define the word node in place of struct stack.*/
typedef struct stack node;
void pop();
void display();

node *start=NULL; //initialize the start node with NULL value
/*whenever we perform push operation, we are creating a new node and set that node as the top node. The following function node* pushNode() creates a new node and pushes it in the stack */
node* pushNode()
{
node *temp;
temp=(node *) malloc( sizeof(node)) ; //dynamically allocates memory for new node
printf("\n Enter data ");
scanf("%d", &temp -> data); // reads the value for data element of new node

/* if start node points to NULL, the element pushed into the stack is the first element (new element). Hence, we set the temp->next to NULL to mark that the current new node is the last node also */
if (start==NULL)
{
                temp -> next = NULL;
}
Else // if already elements are in the stack
{
    temp -> next = start; // set the new nodes’ next with the address of current top node in the stack
}
start = temp; // set the new node as the first or top element. The address stored in temp will be copied to start.
}

void pop() // to remove the top element from stack
{
node *temp;
if(start==NULL) //if start == NULL, no elements are there in the stack. Hence stack underflow
{
        printf("Stack underflow...\n");
        return;
}
temp = start; // assigns the address stored in start to temp

/* Following statement resets the start with the address in temp->next. It means the top element is removed. Here we reset the pointer of start to the second element in the stack from top */

start = temp->next;
printf("The popped element is %d\n", temp->data);
free(temp); //the memory for the deleted node is freed so that the memory can be used by others
}

void display()
{
node *temp;
if(start == NULL)
{
printf("\n\n\t\t Stack is empty ");
}
else
{
temp = start;
printf("\n\n\n\t\t Elements in the stack: \n");
while(temp!=NULL) //until we reach NULL pointer, we display temp->data and move temp to temp->next in each iteration
{
printf("%5d ", temp -> data); //displays the data in the stack
temp = temp -> next; //resets the pointer to point to the next node
}
}
}

int main()
{
int ch;

while(1)
{
printf("\n \tStack operations using pointers.. ");
printf("\n -----------**********-------------\n");
printf("\n 1. Push ");
printf("\n 2. Pop ");
printf("\n 3. Display");
printf("\n 4. Quit ");
printf("\n Enter your choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1 : pushNode();
       break;
case 2 : pop();
       break;
case 3 : display();
       break;
case 4: exit(0);
}
}
}


***************





















SQL exercises for beginners one

SQL Exercises for Beginners / Simple SQL Exercises with Answers / Solved SQL exercises / SQL solved exercises with simple conditions / So...