DATA
STRUCUTRES
AND
LINKED LIST
WHAT ARE DATA STRUCTURES?
• Data Structure is a particular way of organizing data in a
computer so that it can be used efficiently.
• Anything that can store data can be called as a Data
structure, hence Integer, Float, Boolean, Char are DS and they
are also known as Primitive Data Structures.
• We also have some complex Data Structures, which are used
to store large and connected data known as Abstract Data
Structure.
• Examples of Abstract Data Structures are: Linked List, Graph,
Tree, Stack, etc.
Classification Of Data Structures
Data Structures
Built-in
Data Structures
User Defined
Data Structures
Integer Float Character Arrays Lists Files
Linear
Lists
Non-Liner Lists
Stack QueuesLinked List GraphsTree
LINKED
LISTS
WHAT IS LINKED LIST?
• Linked List is a linear data structure. Unlike arrays, linked list elements
are not stored at contiguous location; the elements are linked using
pointers.
Data
Next
A B C D NULL
• A linked list is represented by a pointer to the first node of the linked list. The first
node is called head. If the linked list is empty, then value of head is NULL.
• Each node in a list consists of at least two parts:
1) Data
2) Pointer to the next node
Head
WHY USE LINKED LIST?
 Both Arrays and Linked List can be used to store linear data of similar types
but, The size of the arrays is fixed ,So we must know the upper limit on the
number of elements in advance.
 Inserting a new element in an array of elements is expensive.
 For example, in a system if we maintain a sorted list of IDs in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040].And if we want to insert a new ID 1005, then
to maintain the sorted order, we have to move all the elements after 1000
100 101
 Linked List provide functionality of Dynamic Size(Size can be variable)
 It can grow and shrink according to data.
 Insertion of element is easily done as it uses pointer .
0 1 2 3 4
102103 104 105
5
INSERTION AND DELETION IN LINKED LIST
A B C
1.Insertion of new node at before head node.
2.Insertion of new node at specified position.
3.Deletion of node.
NULL
THE CODE
Language: C++
Functions:
▸ Create Node
▸ Insert At Begning
▸ Insert at Last
▸ Insert at position
▸ Delete
▸ Count no of nodes
▸ Display
#include<iostream.h>
#include<conio.h>
#include<process.h>
class node
{
public:
int data;
node *next;
};
class List
{
node *head;
public:
List() //constructor
{
head=NULL;
}
void create();
void display();
void insert_beg();
void insert_end();
void inserin();
void delete_node();
void count();
};
void List::create()
{
node *newn, *temp;
int n, v,i;
char ch;
cout<<"How many notes you want: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Data: ";
cin>>v;
newn = new node();
newn->data=v;
newn->next=NULL;
if(head==NULL)
head=newn;
else
temp->next=newn;
temp=newn;
}
}
void List::display()
{
node *temp;
temp=head;
if(temp==NULL)
cout<<"The List is empty..."<<endl;
else
{
cout<<"The Element of List is: "<<endl;
while(temp!=NULL)
{
cout<<temp->data<<" => ";
temp=temp->next;
}
cout<<endl;
}
}
void List::insert_beg()
{
node *newn;
int x;
cout<<"Enter data for new node: ";
cin>>x;
newn = new node;
newn->data=x;
newn->next=head;
head=newn;
}
void List::insert_end()
{
node *temp,*newn;
int x;
cout<<"Enter data at the end: ";
cin>>x;
newn = new node;
newn->data=x;
newn->next=NULL;
if(head==NULL)
head=newn;
for(temp=head;temp->next!=NULL;temp=temp->next);
temp->next=newn;
}
void List::insert_in()
{
node *newn,*temp;
int x,y;
newn = new node;
cout<<"Enter the data for new node: ";
cin>>x;
newn->data=x;
newn->next=NULL;
cout<<"Insert the position: ";
cin>>y;
for(temp=head;temp!=NULL && temp->data!=y;temp=temp->next);
if(temp!=NULL)
{
newn->next=temp->next;
temp->next=newn;
}
else
cout<<"Data not found...";
}
void List::count()
{
node *temp;
int cnt=0;
temp=head;
while(temp!=NULL)
{
temp=temp->next;
cnt++;
}
cout<<"Total Number of elements are: "<<cnt;
}
void main()
{
List L1;
int ch;
clrscr();
do
{
cout<<"n1. Create List"<<endl;
cout<<"2. Display List"<<endl;
cout<<"3. Insert Note At Begining"<<endl;
cout<<"4. Insert Node At End"<<endl;
cout<<"5. Insert Node In Between"<<endl;
cout<<"6. Delete Node"<<endl;
cout<<"7. Count Nodes"<<endl;
cout<<"8. Exit "<<endl;
cout<<"Enter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1: L1.create();
break;
case 2: L1.display();
break;
case 3: L1.insert_beg();
break;
case 4: L1.insert_end();

What is Link list? explained with animations

  • 1.
  • 2.
    WHAT ARE DATASTRUCTURES? • Data Structure is a particular way of organizing data in a computer so that it can be used efficiently. • Anything that can store data can be called as a Data structure, hence Integer, Float, Boolean, Char are DS and they are also known as Primitive Data Structures. • We also have some complex Data Structures, which are used to store large and connected data known as Abstract Data Structure. • Examples of Abstract Data Structures are: Linked List, Graph, Tree, Stack, etc.
  • 3.
    Classification Of DataStructures Data Structures Built-in Data Structures User Defined Data Structures Integer Float Character Arrays Lists Files Linear Lists Non-Liner Lists Stack QueuesLinked List GraphsTree
  • 4.
  • 5.
    WHAT IS LINKEDLIST? • Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers. Data Next A B C D NULL • A linked list is represented by a pointer to the first node of the linked list. The first node is called head. If the linked list is empty, then value of head is NULL. • Each node in a list consists of at least two parts: 1) Data 2) Pointer to the next node Head
  • 6.
    WHY USE LINKEDLIST?  Both Arrays and Linked List can be used to store linear data of similar types but, The size of the arrays is fixed ,So we must know the upper limit on the number of elements in advance.  Inserting a new element in an array of elements is expensive.  For example, in a system if we maintain a sorted list of IDs in an array id[]. id[] = [1000, 1010, 1050, 2000, 2040].And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 100 101  Linked List provide functionality of Dynamic Size(Size can be variable)  It can grow and shrink according to data.  Insertion of element is easily done as it uses pointer . 0 1 2 3 4 102103 104 105 5
  • 7.
    INSERTION AND DELETIONIN LINKED LIST A B C 1.Insertion of new node at before head node. 2.Insertion of new node at specified position. 3.Deletion of node. NULL
  • 8.
    THE CODE Language: C++ Functions: ▸Create Node ▸ Insert At Begning ▸ Insert at Last ▸ Insert at position ▸ Delete ▸ Count no of nodes ▸ Display
  • 9.
    #include<iostream.h> #include<conio.h> #include<process.h> class node { public: int data; node*next; }; class List { node *head; public: List() //constructor { head=NULL; } void create(); void display(); void insert_beg(); void insert_end(); void inserin(); void delete_node(); void count(); };
  • 10.
    void List::create() { node *newn,*temp; int n, v,i; char ch; cout<<"How many notes you want: "; cin>>n; for(i=0;i<n;i++) { cout<<"Enter Data: "; cin>>v; newn = new node(); newn->data=v; newn->next=NULL; if(head==NULL) head=newn; else temp->next=newn; temp=newn; } }
  • 11.
    void List::display() { node *temp; temp=head; if(temp==NULL) cout<<"TheList is empty..."<<endl; else { cout<<"The Element of List is: "<<endl; while(temp!=NULL) { cout<<temp->data<<" => "; temp=temp->next; } cout<<endl; } }
  • 12.
    void List::insert_beg() { node *newn; intx; cout<<"Enter data for new node: "; cin>>x; newn = new node; newn->data=x; newn->next=head; head=newn; }
  • 13.
    void List::insert_end() { node *temp,*newn; intx; cout<<"Enter data at the end: "; cin>>x; newn = new node; newn->data=x; newn->next=NULL; if(head==NULL) head=newn; for(temp=head;temp->next!=NULL;temp=temp->next); temp->next=newn; }
  • 14.
    void List::insert_in() { node *newn,*temp; intx,y; newn = new node; cout<<"Enter the data for new node: "; cin>>x; newn->data=x; newn->next=NULL; cout<<"Insert the position: "; cin>>y; for(temp=head;temp!=NULL && temp->data!=y;temp=temp->next); if(temp!=NULL) { newn->next=temp->next; temp->next=newn; } else cout<<"Data not found..."; }
  • 15.
    void List::count() { node *temp; intcnt=0; temp=head; while(temp!=NULL) { temp=temp->next; cnt++; } cout<<"Total Number of elements are: "<<cnt; }
  • 16.
    void main() { List L1; intch; clrscr(); do { cout<<"n1. Create List"<<endl; cout<<"2. Display List"<<endl; cout<<"3. Insert Note At Begining"<<endl; cout<<"4. Insert Node At End"<<endl; cout<<"5. Insert Node In Between"<<endl; cout<<"6. Delete Node"<<endl; cout<<"7. Count Nodes"<<endl; cout<<"8. Exit "<<endl; cout<<"Enter Your Choice: "; cin>>ch; switch(ch) { case 1: L1.create(); break; case 2: L1.display(); break; case 3: L1.insert_beg(); break; case 4: L1.insert_end();