0% found this document useful (0 votes)
57 views

Link List Code

This document describes how to implement a linked list data structure in C++. It defines a Node struct with data and next pointer fields. It shows how to create Node objects, link them together to form a list, and implement functions to add nodes, delete nodes, and print the list. A List class is defined with private Node struct and public functions. The functions allocate memory for new nodes, link them into the correct place in the list by updating next pointers, and delete nodes by unlinking them while preserving the list structure.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Link List Code

This document describes how to implement a linked list data structure in C++. It defines a Node struct with data and next pointer fields. It shows how to create Node objects, link them together to form a list, and implement functions to add nodes, delete nodes, and print the list. A List class is defined with private Node struct and public functions. The functions allocate memory for new nodes, link them into the correct place in the list by updating next pointers, and delete nodes by unlinking them while preserving the list structure.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Link List Implementation

Using struct (Create a Node)

struct node
{
int data;
node* next;
};

//Node
Data Next
Create Nodes

node* n; n

t
node* t; //temp node

h
node* h; //head node
Point to Newly Created Node

n = new node;
n Data Next

n  data = 1;
n 1 Next
Set Temporary Values
n
t = n; 1 Next
t

h = n; n
1 Next
t

h
Build a List

n = new node;

1 Next Data Next


t
n

h
Build a List

n  data = 2;

1 Next 2 Next
t
n

h
Build a List

t  next = n;
t = t  next;

t
1 Next 2 Next
n

h
n = new node;
n  data =3;
t  next =n;
n  next = NULL;

t
1 Next 2 Next 3 NULL
n

h
Link List Class

//List.h
Class List
{
Private:
struct node
{
int data;
node* next;
};
Public:
List();
void AddNode(int addData);
void DeleteNode(int delData);
void PrintList();
}
List.cpp

//Constructor

List :: List()
{
head = NULL;
current = NULL;
temp = NULL;
}
//AddNode Function

void List :: AddNode(int addData)


{
n = new node;
n  next =NULL;
n data = addData;

//what if we already have an element in the list


if(head != NULL )
{
current = head;
while (current  next != NULL) //at the end of the list
{
current = current  next; //advances the current pointer
}
current  next = n; //n will point to the next element
}
//what if we don’t have a list?
//fix the head
else
{
head =n;
}
}
Delete Node Function

void List :: DeleteNode(int delData)


{
delptr = new node;
temp = head;
current = head;
//Things to check in while loop
//1. Pass the whole list
// 2. OR exit

while (current != NULL && current  data != delData)


{
//traverse the list
temp = current;
current = current  next;
}
//traverse the list if data (current) is not found
if (current == NULL)
{
cout << delData << “NOT FOUND” <<endl;
}
//traverse the list if data (current) found
else
{
delptr = current;
current = current  next; //traversal
temp  next = current; //patches the list and delptr
//what if we have to delete from the head
if (delptr == head)
{
head = head  next;
temp = NULL;
}
//delete the data

delete delptr;

cout<< “Value” << delptr << “DELETED” << endl;


}

}
Print Function

void List :: PrintList()


{
current = head; //ptr to front
while(current != NULL)
{
cout << current  data << endl;
current = current  next; //traverse the whole list
}
}
main.cpp

int main()
{
List obj;
obj.AddNode(1);
obj.AddNode(2);
obj.AddNode(3);
obj.PrintList();
obj.DeleteNode(2);
obj.PrintList();
}

You might also like