Skip to content
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

Added algorithm for Reversing the Linked List #1

Open
wants to merge 2 commits 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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# DATA-STRUCTURES

_IMPLIMENTATION OF DS PROGRAMMES_
_IMPLIMENTATION OF DS_ALGO PROGRAMMES_
## Language

- C++

## INCLUDES:-

### Data Structures
- ARRAY
- LINKED LIST
- QUEUE
- STACK
- GRAPH

### Algorithms
- Searches
- Sorting
- Hashing
- Graphs.

88 changes: 88 additions & 0 deletions linkedlist/Reverse_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;

// Creating a NODE Structure
struct node
{
int data; // data
node *next; // link to next node
};

// Creating a class LIST
class list
{
node *start;
public:
void create(); // to create a list
void show(); // show
void reverse(list); // Reverse the list
};

// Main function
int main()
{
list l1;
cout<<"Enter Elements in ascending order.";
l1.create(); // to create a first list
cout<<"Original List : "<<l1.show()<<"\n";

list l2 = l1;
l1.reverse(l2);
cout<<"Reversed List : <<l2.show()<<"\n";
return 0;
}

// Functions

// Creating a new node
void list::create()
{
struct node *new_node,*pre_node;
int value,no,i;
start=new_node=pre_node=NULL;
cout<<"How many Elements : ";
cin>>no;
cout<<"Enter "<<no<<" Elements: ";
for(i=1;i<=no;i++)
{
cin>>value;
new_node=new node;
new_node->data=value;
new_node->next=NULL;
if(start==NULL)
start=new_node;
else
pre_node->next=new_node;
pre_node=new_node;
}
}

// Displaying LIST
void list::show()
{
struct node *ptr=start;
cout<<"\nThe List is : ";
while(ptr!=NULL)
{
cout<<ptr->data<<" -> ";
ptr=ptr->next;
}
cout<<"NULL";
}

void list::reverse(list& l2)
{
struct node *prevNode = NULL, *currNode = l2, *nextNode = NULL;

while(currNode != NULL)
{
nextNode = currNode->next;
currNode->next = prevNode;
prevNode = currNode;
currNode = nextNode;
}

l2 = prev;
}