xxxxxxxxxx
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
void setData();
void printData();
};
node* head =0;
void node::setData()
{
int choice=1;
head=new node();
cout <<"Enter data : ";
cin>>head->data;
head->next=NULL;
node* temp;
temp=head;
node* newnode;
cout<<"Do you want to link another list if yes press 1 else press 2 : ";
cin>>choice;
while(choice==1)
{
newnode=new node();
cout <<"\nEnter data : ";
cin>>newnode->data;
newnode->next=0;
temp->next=newnode;
temp=temp->next;
cout<<"\nDo you want to link another list if yes press 1 else press 2 : ";
cin>>choice;
}
}
void node::printData ()
{
cout<<"\n\nPrinting Linked List........\n\n";
node* temp;
temp=head;
while(temp!=0)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main(){
node obj;
obj.setData();
obj.printData();
}
display singly in c++
xxxxxxxxxx
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
void display() {
struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The linked list is: ";
display();
return 0;
}
xxxxxxxxxx
Singly Linked list is a type of linear data structure that stores data of same
type and data is stored in linear fashion. Singly linked list contains a node
and each node has two section one is data/value and second section contains
the memory address of next node . In this way nodes are connected to each other.
xxxxxxxxxx
#include<iostream>
#include<string>
using namespace std;
class Node
{
public:
int data; //Data to be stored in the node
Node* next; //Next pointer poiting the next node
Node(int val) //Constructor
{
data = val;
next = NULL; //It will always keep the next pointer of the last Node as NULL
}
};
void InsertAtHead(Node* &head,int val)
{
Node* n= new Node(val); //Creates a new Node next to the head, Remember that every old Node is a head to a new Node.
n->next=head; //This will make the new node to point towards the head.
head=n; //We assigned the new node n as the head.
}
void InsertAtTail(Node* &head,int val)// passing head by reference so when we replace it by temp and do changes in temp the head doesnt get effected by the changes done to temp
{
Node* n= new Node(val); //Creates a new Node next to the head, Remember that every old Node is a head to a new Node.
if(head==NULL ) //if head is NULL then we assign the already created Node n as head.
{
head=n;
return;
}
Node* temp=head; //Temp is a Node datatype pointer varaible which stores the address of the head.
while(temp->next!=NULL)
{
temp=temp->next; //It collects the next Node in the previous head.
}
//Now we are at the last Node
temp->next=n; //It links the new Node with the link list by giving the Node to the last Node next and the new Node is already consisting NULL at its next.
}
void display(Node* head) // passing by value because no more modification in print function
{
Node* temp=head; //Again assigned temp as pointer variable for head
while(temp!=NULL)
{
cout<<temp->data<<"->"; //Printing the data in temp
temp = temp->next; //Storing next Node in the temp
}
cout<<"NULL "<<endl;
}
int main()
{
Node* head = NULL; //Assigening Null to head
InsertAtTail(head,1);
InsertAtTail(head,2);
InsertAtTail(head,3);
display(head);
InsertAtHead(head,4);
display(head); //Displays the
}