0% found this document useful (0 votes)
24 views22 pages

List - Array and Pointer Implementation 15

A linked list is a linear data structure that uses pointers to link elements, allowing for dynamic sizing and ease of insertion and deletion compared to arrays. However, it has drawbacks such as lack of random access and additional memory overhead for pointers. The document outlines various operations on linked lists, including adding, deleting, and searching for elements, as well as applications in data structures like stacks, queues, and graphs.

Uploaded by

Piyush Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views22 pages

List - Array and Pointer Implementation 15

A linked list is a linear data structure that uses pointers to link elements, allowing for dynamic sizing and ease of insertion and deletion compared to arrays. However, it has drawbacks such as lack of random access and additional memory overhead for pointers. The document outlines various operations on linked lists, including adding, deleting, and searching for elements, as well as applications in data structures like stacks, queues, and graphs.

Uploaded by

Piyush Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Linked List

Linked List using Pointers


• Linked List is a linear data structure. Unlike
arrays, linked list elements are not stored at a
contiguous location; the elements are linked
using pointers.

Why Linked List?
Arrays can be used to store linear data of
similar types, but arrays have the following
limitations.
• 1) The size of the arrays is fixed:
• 2) Inserting a new element in an array of
elements is expensive
Advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion
• Drawbacks:
1) Random access is not allowed..
2) Extra memory space for a pointer is
required with each element of the list.
3) Not cache friendly. Since array elements are
contiguous locations, there is locality of
reference which is not there in case of linked
lists.
Representation
// A linked list node
struct Node {
int data;
struct Node* next;
} *start,*p,*q;
Operations on Linked List
1.Add First ( Insert at the Beginning )
2.Add End ( Insert at the End )
3.Add After ( Insert in between )
4.Display
5.Delete (at the Beginning, at the End, in between)
6. Search
Add beginning
addbeg(int n)
{
node *q;
q=new node; // malloc
q->link=start;
q->data=n;
start=q;
}
Add end
addend(int n) else
{ {
node *q,*t; q=start;
if(q==NULL) while(q->link!=NULL)
{ q=q->link;
q=new node;
q->data=n; t=new node;
q->link=NULL; t->data=n;
start=q t->link=NULL;
} q->link=t;
}
}
Add between/after
addafter(int c,int n)
t=new node;
{ node *q,*t;
t->data=n;
int i; t->link=q->link;
for(i=0,q=start; i<c; i++) q->link=t;
{ }
q=q->link;
if(q==NULL)
{ cout<<“no index found to
insert
elements";
return;
}
Display
listshow()
{
node *q;
for(q=Start;q!=NULL;q=q->link)
cout<<q->data;
}
Delete(int n)
Delete
while(q!=NULL)
{ { if(q->data==n)
node *q,*r; { r->link=q->link;
q=start; delete q; // DEL AFTER
if(q->data==n) return;
{
}
start=q->link;
else
delete q; //BEGINNING
{ r=q;
return;
} q=q->link;
else }
{ } // while
r=q; } // else
q=q->link; cout<<"Element" <<n<< "is not in the list";
}
Searching
Listsearch()
{
node *q;
int x;
printf(“ enter the element to search”);
Scanf(“%d”,&x);
for(q=Start;q!=NULL;q=q->link)
if (q->data == x)
{ print (“ The element is found “);
return;
}
Prinf(“ The given number is not present in the list “);
}
int main()

Traversal
{
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
struct Node {
int data; // allocate 3 nodes in the heap
struct Node* next; head = (struct Node*)malloc(sizeof(struct Node));
}; second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
// This function prints contents
// of linked list starting from head->data = 1; // assign data in first node
// the given node head->next = second; // Link first node with second
void printList(struct Node* n)
{
second->data = 2; // assign data to second node
second->next = third;
while (n != NULL) {
printf(" %d ", n->data);
third->data = 3; // assign data to third node
n = n->next;
third->next = NULL;
}
} printList(head);

return 0;
}
Applications of linked list

• Implementation of Stacks and Queues


• Implementation of graphs : Adjacency list
representation of graphs is most popular which is
uses linked list to store adjacent vertices.
• Dynamic memory allocation : We use linked list
of free blocks.
• Maintaining directory of names
• Manipulation of polynomials by storing constants
and coefficients in the node of linked list
Polynomial addition

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy