- Home
- Archives for2020
Algorithm
InsertNodeAtBegining()
If (newNode == NULL) then
print ('Unable to allocate memory')
End if
Else then
read item
newNode.item ← item
newNode.next ← head
head ← newNode
End else
InsertNodeAtEnding()
If (newNode == NULL) then
print ('Unable to allocate memory')
End if
Else then
read item
newNode.item ← item
newNode.next ← NULL
temp ← End
While (temp.next != NULL) do
temp ← temp.next
End while
temp.next ← newNode
End else
InsertNodeAtMiddle()
If (newNode == NULL) then
write ('Unable to allocate memory.')
End if
Else then
read item
newNode.item ← item
temp ← head
For i ← 2 to n-1
temp ← temp.next
If (temp == NULL) then
break
End if
End for
If (temp != NULL) then
newNode.next ← temp.next
temp.next ← newNode
End if
End else
deleteFirstNode()
If (head==NULL)
print “List Empty”
end if
else
temp = head
head = head.next
delete temp
End
deleteLastNode()
If (head == NULL) then
print ('List is already empty')
End if
Else then
temp ← head
previousNode ← head
While (temp.next != NULL) do
previousNode ← temp
temp ← temp.next
End while
If (temp == head) then
head ← NULL
End if
Else then
previousNode.next ← NULL
End else
free (temp)
End
deleteMiddleNode()
If (head == NULL) then
print('List empty')
End if
Else then
temp ← head
previousNode ← head
For i←2 to n do
previousNode ← temp
temp ← temp.next
If (temp == NULL) then
break
End if
End for
If (temp != NULL and temp == head) then
head ← head.next
End if
Else
previousNode.next ← temp.next
temp.next ← NULL
free (temp)
End else
End
Source Code of Single Link List operation
/*
Single Link List using C program
*/
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node
{
int item;
struct node *next;
} * head;
void createList(int n);
void displayList();
void insertNodeAtBeginning(int item);
void insertNodeAtEnd(int item);
void insertNodeAtMiddle(int item, int position);
void deleteFirstNode();
void deleteLastNode();
void deleteMiddleNode(int position);
int main()
{
int n, item, position;
int choice;
for (;;)
{
printf("\n===========::Single Link List Menu::=================\n\n ");
printf("\n1. Press 1 for Creating Lists\n");
printf("\n2. Press 2 for display Lists\n");
printf("\n3. Press 3 for insert item into fisrt Position of Lists\n");
printf("\n4. Press 4 for insert item into last Position of Lists\n");
printf("\n5. Press 5 for insert item into specified middle Position of Lists\n");
printf("\n6. Press 6 for delete item from fisrt Position of Lists\n");
printf("\n7. Press 7 for delete item from last Position of Lists\n");
printf("\n8. Press 8 for delete item from specified middle Position of Lists\n");
printf("\n===========::Single Link List Menu::=================\n ");
printf("\n\tEnter your choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
break;
case 2:
printf("\nitem in the list \n");
displayList();
break;
case 3:
printf("\nEnter item to insert at beginning of the list: ");
scanf("%d", &item);
insertNodeAtBeginning(item);
break;
case 4:
printf("\nEnter item to insert at end of the list: ");
scanf("%d", &item);
insertNodeAtEnd(item);
break;
case 5:
printf("nEnter item to insert at middle of the list: ");
scanf("%d", &item);
printf("Enter the position to insert new node: ");
scanf("%d", &position);
insertNodeAtMiddle(item, position);
break;
case 6:
deleteFirstNode();
break;
case 7:
deleteLastNode();
break;
case 8:
printf("\nEnter the node position you want to delete: ");
scanf("%d", &position);
deleteMiddleNode(position);
break;
case 9:
exit(1);
break;
default:
printf("Wrong selction");
}
}
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int item, i;
head = (struct node *)malloc(sizeof(struct node));
if (head == NULL)
{
printf("Unable to allocate memory.");
exit(0);
}
printf("Enter the item of node 1: ");
scanf("%d", &item);
head->item = item;
head->next = NULL;
temp = head;
for (i = 2; i <= n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
printf("Enter the item of node %d: ", i);
scanf("%d", &item);
newNode->item = item;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
void displayList()
{
struct node *temp;
if (head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while (temp != NULL)
{
printf("item = %d\n", temp->item);
temp = temp->next;
}
}
}
void insertNodeAtBeginning(int item)
{
struct node *newNode;
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->item = item;
newNode->next = head;
head = newNode;
printf("item INSERTED SUCCESSFULLY\n");
}
}
void insertNodeAtEnd(int item)
{
struct node *newNode, *temp;
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->item = item;
newNode->next = NULL;
temp = head;
while (temp != NULL && temp->next != NULL)
temp = temp->next;
temp->next = newNode;
printf("item INSERTED SUCCESSFULLY\n");
}
}
void insertNodeAtMiddle(int item, int position)
{
int i;
struct node *newNode, *temp;
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->item = item;
newNode->next = NULL;
temp = head;
for (i = 2; i <= position - 1; i++)
{
temp = temp->next;
if (temp == NULL)
break;
}
if (temp != NULL)
{
newNode->next = temp->next;
temp->next = newNode;
printf("item INSERTED SUCCESSFULLY\n");
}
else
{
printf("UNABLE TO INSERT item AT THE GIVEN POSITION\n");
}
}
}
void deleteFirstNode()
{
struct node *toDelete;
if (head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
head = head->next;
printf("\nData deleted = %d\n", toDelete->item);
free(toDelete);
printf("Successfully Delete First Node\n");
}
}
void deleteLastNode()
{
struct node *toDelete, *secondLastNode;
if (head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
secondLastNode = head;
while (toDelete->next != NULL)
{
secondLastNode = toDelete;
toDelete = toDelete->next;
}
if (toDelete == head)
{
head = NULL;
}
else
{
secondLastNode->next = NULL;
}
free(toDelete);
printf("Successfully Deleted Last Node\n");
}
}
void deleteMiddleNode(int position)
{
int i;
struct node *toDelete, *prevNode;
if (head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
prevNode = head;
for (i = 2; i <= position; i++)
{
prevNode = toDelete;
toDelete = toDelete->next;
if (toDelete == NULL)
break;
}
if (toDelete != NULL)
{
if (toDelete == head)
head = head->next;
prevNode->next = toDelete->next;
toDelete->next = NULL;
free(toDelete);
printf("Successfully\n");
}
else
{
printf("Invalid position unable to delete.");
}
}
}
Stack implementation using C | || Queue implementation using C |
---|
Navigation Bar -1
In this post, I have created a Navigation Bar of a website using simple HTML and internal CSS. Source Code are following
Source Code:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Website</title>
<style>
.nav{
background-color: black;
}
.item {
list-style-type: none;
display: inline;
}
.items {
background-color: black;
margin: 0;
height: 34px;
padding: 34px ;
width: 50%;
margin-left: auto;
}
a {
margin: 0 10px;
text-decoration: none;
text-transform: capitalize;
letter-spacing: 2px;
font-size: 1.5vw;
color:white;
}
a:hover {
background-color:green;
font-size: 1.5em;
}
a:active {
font-size: 1.5em;
}
</style>
</head>
<body>
<div class="header">
<div class="nav">
<ul class="items">
<li class="item"><a href="">Home</a></li>
<li class="item"><a href="">About Us</a></li>
<li class="item"><a href="">Service</a></li>
<li class="item"><a href="">Gallery</a></li>
<li class="item"><a href="">Contact Us</a></li>
</ul>
</div>
</div>
</body>
</html>
Navigation Bar -2
Another Navigation Bar with the image using Simple HTML and Internal CSS. Source codes are the following.
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {font-family: Arial, Helvetica, sans-serif;}
.navbar {
width: 100%;
background-color: #555;
overflow: auto;
}
.navbar a {
float: left;
padding: 12px;
color: white;
text-decoration: none;
font-size: 17px;
}
.navbar a:hover {
background-color: #000;
}
.active {
background-color: #4CAF50;
}
@media screen and (max-width: 500px) {
.navbar a {
float: none;
display: block;
}
}
</style>
<body>
<div class="navbar">
<a class="active" href="#"><i class="fa fa-fw fa-home"></i> Home</a>
<a href="#"><i class="fa fa-fw fa-search"></i> Search</a>
<a href="#"><i class="fa fa-fw fa-envelope"></i> Contact</a>
<a href="#"><i class="fa fa-fw fa-user"></i> Login</a>
</div>
</body>
</html>
Queue Algorithm
enqueue/insert element to the queue
procedure enqueue(data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
dequeue/delete element from the queue
procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
Source Code of Queue implementation in C using Array
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
void enqueue();
void dequeue();
void display();
int queue_array[MAX];
int rear = -1;
int front = -1;
int main()
{
int choice;
while (1)
{
printf("\n1.Enqueue/Insert element to queue \n");
printf("\n2.Dequeue/Delete element from queue \n");
printf("\n3.Display all elements of queue \n");
printf("\n4.Quit \n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}
}
}
void enqueue()
{
int data;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == -1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &data);
rear = rear + 1;
queue_array[rear] = data;
}
}
void dequeue()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow \n");
return;
}
else
{
printf("Element dequeue from queue is : %d\n", queue_array[front]);
front = front + 1;
}
}
void display()
{
int i;
if (front == -1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
Source Code of Queue implementation in C using Link List
#include<stdio.h>
#include<stdlib.h>
struct Queue //structure is declared
{
struct Queue *next; //self referensing structure
int item; //initailizing item in the structure
};
struct Queue *front; //a pointer front is declared in structure
struct Queue *rear; //a pointer rear is declared in structure
void addq(int); //addq function is declared
int delq(); //delq function id declared
void main()
{
int ch, item, val; //variables are declared
while(1)
{
printf("\nEnter 1 to add element in the Queue");
printf("\nEnter 2 to delete element from the Queue");
printf("\nEnter 3 to exit from program");
printf("\nEnter your choice::");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the item in the list::");
scanf("%d", &item);
addq(item);
break;
case 2: val=delq() ;
printf("\nThe deleted element is %d=",val);
break ;
case 3: exit(1);
default: printf("\nWrong Input") ;
}
}
}
void addq(int item)
{
struct Queue *temp; //a pointer temp is declared within the structure
temp=(struct Queue*)malloc(sizeof(struct Queue));
if(temp==NULL)
{
printf("\nQueue Overflowed");
return;
}
temp->item=item;//the value is entered in the List
temp->next=NULL;
if(rear==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->next=temp;
rear=rear->next; //rear is incremented
}
}
int delq()
{
struct Queue *temp;
int item=0;
if(front==NULL) //checking unerflowed condition
{
printf("\nQueue Underflowed");
return 0;
}
item=front->item;
temp=front;
if(front==NULL) //checking whether front is null or not, if true
{
front=NULL; //front is assign with NULL
rear=NULL;
}
else
{
front=front->next;
free(temp);
}
return item;
}
Single Link List implementation using C |
---|