Skip to content

Added some Algorithms of Linked Lists in C and C++ #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
248 changes: 248 additions & 0 deletions Algorithms/CircularSLL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
#include <iostream>
using namespace std;

template <class T>
struct Node
{
T data;
Node<T> *next;
};

template <class T>
class LinkedList
{
public:
Node<T> *head;
LinkedList()
{
head = NULL;
}
void insertionHead()
{
Node<T> *temp = new Node<T>;
cout << "\nenter the value to be inserted " << endl;
T n;
cin >> n;
temp->data = n;
if (head == NULL)
{
head = temp;
head->next = head;
}
else
{
Node<T> *loop = head;
while (loop->next != head)
loop = loop->next;
temp->next = head;
head = temp;
loop->next = head;
}
}
void insertionTail()
{
Node<T> *temp = new Node<T>;
cout << "\nenter the value to be inserted " << endl;
T n;
cin >> n;
temp->data = n;
if (head == NULL)
{
head = temp;
head->next = head;
}
else
{
Node<T> *p = head;
while (p->next != head)
p = p->next;
temp->next = head;
p->next = temp;
}
}
void deletionHead()
{
if (head != NULL)
{
if (head->next == head)
{
Node<T> *delnode = head;
cout << "element deleted " << head->data;
head = NULL;
delete delnode;
}
else
{
Node<T> *delnode = head;
cout << "element deleted " << head->data;
Node<T> *loop = head;
while (loop->next != head)
loop = loop->next;
head = head->next;
loop->next = head;
delnode->next = NULL;
delete delnode;
}
}
else
{
cout << "List empty";
}
}
void deletionTail()
{
if (head != NULL)
{
if (head->next == head)
{
Node<T> *delnode = head;
cout << "element deleted " << head->data;
head = NULL;
delete delnode;
}
else
{
Node<T> *temp = head;
while (temp->next->next != head)
temp = temp->next;
cout << "element deleted " << temp->next->data;
Node<T> *delnode = temp->next;
delete delnode;
temp->next = head;
}
}
else
{
cout << "List empty";
}
}
void search()
{
if (head == NULL)
cout << "\nList empty";
else
{
cout << "\nenter the value to be searched " << endl;
T x;
cin >> x;
Node<T> *temp = head;
bool found = false;
do
{
if (temp->data == x)
found = true;
temp = temp->next;
} while (temp != head);

if (found)
cout << "\nElement found in linked list";
else
cout << "\nElement not found in linked list";
}
}
void reverse()
{
if (head == NULL)
{
cout << "\nList is empty.";
return;
}
else if (head->next == head)
{
cout << "\nList only has one element";
return;
}

Node<T> *prev = NULL;
Node<T> *temp = head;
Node<T> *next;
do
{
next = temp->next;
temp->next = prev;
prev = temp;
temp = next;
} while (temp != head);
head->next = prev;
head = prev;
cout << "\nThe list has been reversed";
}
void display()
{
Node<T> *temp = head;
while (temp != NULL)
{
cout << temp->data << "->";
if (temp->next == head)
break;
temp = temp->next;
}
cout << "!!!";
}
void alt_display()
{
Node<T> *temp = head;
while (temp != NULL)
{
cout << temp->data << "->";
if (temp->next == head || temp->next->next == head)
break;
temp = temp->next->next;
}
cout << "!!!";
}
};

int main()
{
LinkedList<int> l;
int ch;
char cont;
do
{
cout << "\nEnter your choice among following " << endl;
cout << "1.Insert an element at beginning" << endl;
cout << "2.Insert an element at end" << endl;
cout << "3.Delete an element from beginning" << endl;
cout << "4.Delete an element from end" << endl;
cout << "5.Search the list" << endl;
cout << "6.Reverse the list" << endl;
cout << "7.Display alternate elements of Linked List" << endl;
cout << "8.Display Linked List\n"
<< endl;
cin >> ch;
switch (ch)
{
int val;
case 1:
l.insertionHead();
break;
case 2:
l.insertionTail();
break;
case 3:
l.deletionHead();
break;
case 4:
l.deletionTail();
break;
case 5:
l.search();
break;
case 6:
l.reverse();
break;
case 7:
l.alt_display();
break;
case 8:
l.display();
break;
default:
cout << "\nInvalid choice " << endl;
}
cout << "\n\nContinue?(y/n)" << endl;
cin >> cont;
} while (cont == 'y' || cont == 'Y');
return 0;
}
72 changes: 72 additions & 0 deletions Algorithms/DoublyLinkedList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* CREATING A SIMPLE DOUBLY LINKED LIST */
/* DBLINK.C */

# include <stdio.h>
# include <malloc.h>

struct Double
{
int info;
struct Double *next;
struct Double *previous;
};

int num ;
struct Double start;
void Doubly_link_list (struct Double *);
void display (struct Double *);

/* Function creates a simple doubly linked list */

void Doubly_link_list(struct Double *node)
{
char ch;
start.next = NULL; /* Empty list */
start.previous = NULL;
node = &start; /* Point to the start of the list */
num = 0;
printf("\n Input choice n for break: ");
ch = getchar();

while( ch != 'n')
{
node->next = (struct Double *) malloc(sizeof(struct Double));
node->next->previous = node;
node = node->next;
printf("\n Input the values of the node : %d: ", (num+1));
scanf("%d", &node->info);
node->next = NULL;
fflush(stdin);
printf("\n Input choice n for break: ");
ch = getchar();
num ++;
}
printf("\n Total nodes = %d", num);
}

/* Display the list */

void display (struct Double *node)
{
node = start.next;

do {
printf("\n 0x%x", node);
printf(" %d", node->info);
node = node->next;
} while (node->next); /* Show value of last node only one time */

do {
printf("\n 0x%x", node );
printf(" %d", node->info);
node = node->previous;
} while (node->previous);
}

int main()
{
struct Double *node = (struct Double *) malloc(sizeof(struct Double));
Doubly_link_list(node);
printf("\n Created doubly linked list is as follows\n");
display(node);
}
Loading