**********Singly Linked List Program:********************
Added by Aijaz Ahmad
********************************************
This is header file.
"single_linked_list.h"
// A linked list is a data structure which is built from structures and pointers.
// It forms a chain of "nodes" with pointers representing the links of the chain and
// holding the entire thing together.
//this class will have implementation of single linked list operations like
// 1) delete an element from the list.
// 2) search an elememt in the list.
// 3) Insert an element at the first place.
// 4) insert an element at the last.
// 5) Insert an element to the user entered position.
// 6) delete the element from the user entered position and user eneterd elment.
// 7) Sort the list
// 8) Count the number of element in the list
// 9) Print the list
class single_linked_list
{
private:
//creating structure for node which will have one node with one value and other(link) will be address of other node
struct node
{
int data;
node* link;
}*first_node;
public:
// default constructor
single_linked_list()
{
first_node = NULL;
}
//method for inserting an element at the first.
void insert_at_first(int element);
//method for inserting an element at the end
void insert_at_end(int element);
//method for inserting an element at the entered position
bool insert_at_pos(int pos, int element);
//delete element from the list
bool delete_element(int element);
//delete an element from the entered position
bool delete_element_from_pos(int pos);
//sort the list
void sort();
// count the elements in the list
int no_of_elements();
//print all the element of the list
void print_list();
};
Added by Aijaz Ahmad
********************************************
This is header file.
"single_linked_list.h"
// A linked list is a data structure which is built from structures and pointers.
// It forms a chain of "nodes" with pointers representing the links of the chain and
// holding the entire thing together.
//this class will have implementation of single linked list operations like
// 1) delete an element from the list.
// 2) search an elememt in the list.
// 3) Insert an element at the first place.
// 4) insert an element at the last.
// 5) Insert an element to the user entered position.
// 6) delete the element from the user entered position and user eneterd elment.
// 7) Sort the list
// 8) Count the number of element in the list
// 9) Print the list
class single_linked_list
{
private:
//creating structure for node which will have one node with one value and other(link) will be address of other node
struct node
{
int data;
node* link;
}*first_node;
public:
// default constructor
single_linked_list()
{
first_node = NULL;
}
//method for inserting an element at the first.
void insert_at_first(int element);
//method for inserting an element at the end
void insert_at_end(int element);
//method for inserting an element at the entered position
bool insert_at_pos(int pos, int element);
//delete element from the list
bool delete_element(int element);
//delete an element from the entered position
bool delete_element_from_pos(int pos);
//sort the list
void sort();
// count the elements in the list
int no_of_elements();
//print all the element of the list
void print_list();
};
****************************************CPP file:**************************
#include "stdafx.h"
#include "single_linked_list.h"
//insert at first
void single_linked_list::insert_at_first(int element)
{
if(first_node == NULL)
{
printf("First node of the element \n");
node* temp_node = new node();
temp_node->data = element;
temp_node->link = NULL;
first_node = temp_node;
}
else
{
node* new_node = new node();
new_node->data = element;
new_node->link = first_node;
first_node = new_node;
}
}