This Presentation expalins what are data structures and linked lists with animations.
It also contains a sample implementation of Linked List using C++language.
It uses C++ Pointers to define linked lists.
DATA STRUCUTRES AND LINKED LIST
WHAT ARE DATA STRUCTURES?
Classification Of Data Structures ?
WHAT IS LINKED LIST?
WHY USE LINKED LIST?
INSERTION AND DELETION IN LINKED LIST
LINKED LIST CODE
2. 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.
3. 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
5. 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
6. 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
7. 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
8. THE CODE
Language: C++
Functions:
▸ Create Node
▸ Insert At Begning
▸ Insert at Last
▸ Insert at position
▸ Delete
▸ Count no of nodes
▸ Display
13. 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;
}
14. 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...";
}
15. void List::count()
{
node *temp;
int cnt=0;
temp=head;
while(temp!=NULL)
{
temp=temp->next;
cnt++;
}
cout<<"Total Number of elements are: "<<cnt;
}
16. 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();