Linked List Data Structure PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

HNDIT Data Structure and Algorithm SLIATE

3. Linked List Data Structure

What is Pointer?
Pointer contains memory address of a particular type of data.
In C++, we use * to declare a pointer.
2cdf 1a5c c75c
int p; int* iptr; char* cptr; p iptr 1a5c cptr c75c

p = 9; *iptr = 5; *cptr=’$’; p 9 iptr 5 cptr $ t # /0

cout<<p; cout<<iptr; cout<<cptr; 9 1a5c $t#

cout<<&p; cout<<*iptr;cout<<*cptr; 2cdf 5 $


2cdf 1a5c
iptr = &p; p 9 iptr 2cdf 5 cptr $ t # /0

cout<<*iptr; 9

What is Structure (in C++)?


It is a group of data elements which may have different types of data.

struct employee emp


{ empNo
int empNo; salary
float salary;
} emp;
emp
emp.empNo=1005; empNo 1005
emp.salary=40000.00; salary 40000.00

cout<<emp.salary+1000.00; 41000.00

employee* eptr; eptr


eptr = new employee; empNo 1006
eptr->empNo=1006; salary 30000.00
eptr->salary=30000.00;
cout<<eptr->empNo; 1006

What is Linked List?


A linked list consists of nodes of data which are connected to other nodes.
Linked lists can be linear and non-linear data structures however arrays are linear data structures.
There are several types of linked lists.
E.g1:
data

data

data

data
next

next

next

next

head 5 9 6 7 /

E.g2:
data

data

data

data
next

next

next

next

head
front 5 9 6 7 /
rear

1
HNDIT Data Structure and Algorithm SLIATE

E.g3:

previous

previous

previous

previous
data

data

data

data
next

next

next

next
head / 5 9 6 7 /

Common operations of Linked List are:


initializeList() - initializes the list as empty list.
insertFirstElt(int elt) - inserts a new element into an empty list.
insertAtFront(int elt) - inserts an element at the beginning of the list.
insertAtEnd(int elt) - inserts an element at the end of the list (appendElt or appendNode).
insertAfter(int oldElt, int newElt) - inserts an element after a specified element.
deleteElt(int elt) - deletes a specified element.
displayList() - displays all the elements in the list
isEmpty() - returns true if the list has no elements, false otherwise.
isFull() - returns false if the list is full, false otherwise.

Structural Diagrams of Linked List Operations:

1. initializeList() head /

2. insertFirstElt(5)
data
next

head 5 /

3.insertAtFront(3)
data
next

head 5 /
data
next

newNode 3

4. insertAtEnd(8)
data

data
next

next

head 3 5

newNode 8 /

5. insertAfter(5,7)
data

data

data
next

next

next

head 3 5 8 /

newNode 7

2
HNDIT Data Structure and Algorithm SLIATE

6. deleteElt(5):

data

data

data

data
next

next

next

next
head 3 5 7 8 /

Implementation of Linked List Operations:


#include<iostream.h>
#include<conio.h>
class LinkedList
{
private:
struct listNode
{
int data;
listNode* next;
};

listNode* head;

public:
LinkedList();
void initializeList();
void insertFirstElt(int elt);
void insertAtFront(int elt);
void insertAtEnd(int elt);
void insertAfter(int oldElt, int newElt);
void deleteElt(int elt);
void displayList();
int isEmpty();
int isFull();
}

LinkedList::LinkedList() //Constructor
{
head=NULL;
}

void LinkedList::initializeList()
{
head=NULL;
}

void LinkedList::insertFirstElt(int elt)


{
head=new listNode;
head->data=elt;
head->next=NULL;
}

void LinkedList::insertAtFront(int elt)


{
listNode *newNode;
newNode=new listNode;
newNode->data=elt;

3
HNDIT Data Structure and Algorithm SLIATE

newNode->next=head;
head=newNode;
}

void LinkedList::insertAtEnd(int elt)


{
listNode *newNode, *curNode;
newNode=new listNode;
newNode->data=elt;
newNode->next=NULL;
if (!head)
head=newNode;
else
{
curNode=head;
while (curNode->next != NULL)
curNode = curNode->next;
curNode->next = newNode;
}
}

void LinkedList::insertAfter(int oldElt, int newElt)


{

void LinkedList::deleteElt(int elt)


{

void LinkedList::displayList()
{
listNode* curNode;
curNode=head;
while (curNode)
{
cout<<curNode->data<<" ";
curNode=curNode->next;
}
}

int LinkedList::isEmpty()
{
if (head== NULL)
return 1;
else
return 0;
}

int LinkedList::isFull() //It always returns false.

4
HNDIT Data Structure and Algorithm SLIATE

{
return 0;
}

void main()
{
clrscr();
LinkedList lst;
lst.insertAtEnd(4);
lst.insertAtEnd(6);
lst.insertAtEnd(5);
lst.displayList();
}

Advantages of Linked List:


Easy to insert and delete elements.
Unlike array, memory space is not wasted in linked list.

Disadvantages of Linked List:


Slow in searching.

You might also like