Showing posts with label linked list. Show all posts
Showing posts with label linked list. Show all posts

20130330

How to write cpp program to reverse a linked list



//C++ program for reversing single Linked list nodes 
Cpp program for implement reversing Single Linked List
Single linked list program for reversing the nodes
How to reverse linked list 
#include<iostream>
using namespace std;

struct node
{
node *next;
int data;
};
class ll
{
private:
node *start;
public:
ll(){start=NULL;}

void insert(int a)
{
cout<<"Inside Insert."<<endl;
node *newnode=new node;
newnode->data=a;
newnode->next=start;
start=newnode;
//return;
}
void display();
void del();
void reverse();
};

20120113

How to use Linked list in C++

//Program in cpp to demonstrate linkedlist, linkedlist.cpp
#include<iostream>
using namespace std;
struct node
        {
        public:
        int data;
        node * next;
        };
class linkedlist
        {
        private:
        node* first;
        public:
        linkedlist()
                {
                first=NULL;
                }
        void getdata(int d);
        void display();
        };
void linkedlist::getdata(int dd)
        {
        node* newnode=new node;
        newnode->data=dd;
        newnode->next=first;
        first=newnode;
        }
void linkedlist::display()
        {
        node* current=first;
        while(current!=NULL)
                {
                cout<<current->data<<endl;
                current=current->next;
                }
        }
int main()
{
linkedlist li;
li.getdata(10);
li.getdata(15);
li.getdata(20);
li.getdata(18);
li.getdata(19);
li.display();
return 0;
}
You may also Like:
C++ Program for Circular Queue implementation using linked list  
C++ program for Circular Queue implementation using Array
C++ program for reversing single Linked list nodes 
C++ Program for sorted single linked list with class
C++ Program for BST-Binary Search Tree  

Previous                             Home                               Next