Professional Documents
Culture Documents
5 2 7
Common Operations
➔ All operations start with the ‘start’ node or head node. We need the start
node for any operation.
◆ start node is a parameter in all operations.
◆ Except for display operation, the start node will be returned back from the
function. [return type : node *]
➔ To check whether the list is empty or not.
◆ if (start == NULL)
➔ To create a new node (node1)
◆ Used in all insertions operation and createNew operation.
● node *node1 = new node();
➔ To assign ‘n’ to node1
◆ Used in all insertion operations and createNew operation.
● node1 -> data = n;
➔ Creating a pointer to the node
◆ node *ptr, *preptr;
Common Operations
➔ Pointing the pointer to the start node
◆ ptr=start;
◆ preptr=start;
◆ node *ptr=start; //Initialization
➔ Moving the pointer to the next node
◆ ptr = ptr->next;
➔ Checking whether the pointer has reached the last node.
◆ while (ptr->next != NULL)
◆ while (ptr!=NULL)
● //display operation, sort operation, delete any, delete all
➔ preptr following ptr
◆ preptr = ptr;
➔ Linking ptr to another node
◆ Ex. ptr->next = node1;
➔ Making a node as the last node
◆ Ex. node1->next = NULL;
Node
✔A node is defined using a structure.
struct node
{
int data;
node* next;
};
Create New List
✔Case 1: start == NULL
node1
n NULL
start
Algorithm Pseudocode
node *node1 = new node();
➔ Create a new node. node1->data = n;
➔ Assign ‘n’ to the data part. node1->next=NULL;
➔ Assign NULL to the next part. //Case 1 (if start == NULL)
➔ Case 1: creating node1 as the first node. start = node1;
◆ Make node1 as the start node //Case 2 (else)
➔ Case 2: Adding node1 as the last node. node *ptr=start;
➔ Point ptr to the start node. while (ptr->next != NULL)
➔ Move ptr till it reaches the last node. ptr=ptr->next;
➔ Link node1 to ptr ptr->next = node1;
➔ Return the start node return start;
Source Code: createNew
node* createNew(node *start,int n) else
{ {
node *node1, *ptr; ptr=start;
node1=new node(); while (ptr->next!=NULL)
node1->data=n; ptr=ptr->next;
if (start==NULL) ptr->next=node1;
{ }
start=node1; node1->next=NULL;
} return start;
}
Display : Algorithm
Algorithm Pseudocode
node *ptr;
➔ Create a pointer ptr.
➔ Point ptr to the start node. ptr=start;
➔ Move ptr till it reaches the last while (ptr!=NULL)
node. {
cout<<ptr->data<<'\t';
➔ Display the data of the current node.
➔ Move ptr to the next node. ptr=ptr->next;
}
Display
void display(node *start)
{ node *ptr;
ptr=start;
while (ptr!=NULL)
{ cout<<ptr->data<<'\t';
ptr=ptr->next;
} }
Insert at the Beginning
node1
n 10 12 15
start start
node1->next=start;
start=node1;
return start; }
Insert at the End
node1
10 12 15 n NULL
start
ptr ……………………………………...ptr………………………………………...ptr
Algorithm
➔ Create a new node. (node1)
➔ Assign n to the data.
➔ Assign NULL to the next pointer.
➔ Point a pointer (ptr) to the start node.
➔ Move ptr to the last node.
➔ Attach ptr to node1.
Insert at the End [code]
node1
10 12 15 n NULL
start
ptr …………………………………...ptr………………………………………...ptr
node* insertEnd(node *start, int n) while(ptr->next != NULL)
{ ptr=ptr->next;
node *node1=new node();
ptr->next=node1;
node *ptr;
return start;
node1->data=n;
}
node1->next=NULL;
ptr=start;
Insert Before
node1
n
start
10 12 15 25 NULL
ptr
preptr
Assume val = 15
i.e we should insert before 15
ptr (blue pointer) moves till val
preptr (pink pointer) follows ptr
*Dotted line (for pink and blue) indicates the pointer is moved from that node
Insert Before (Algorithm)
{ {
preptr=ptr;
node *node1=new node();
ptr=ptr->next;
node *ptr, *preptr;
}
node1->data=n;
preptr->next=node1;
ptr=start; node1->next=ptr;
return start;
}
Insert After
node1
n
start
10 12 15 25 NULL
ptr
preptr
Assume val = 15
i.e we should insert after 15
preptr (pink pointer) moves till val
preptr (pink pointer) follows ptr (blue pointer)
*Dotted line (for pink and blue) indicates the pointer is moved from that node
Insert After (Algorithm)
ptr
node* delBeg(node* start)
{ Algorithm
node *ptr = start; ➔ Point a pointer ptr to the start
start=start->next; node.
delete ptr; ➔ Make the next node as the start
return start;
node.
➔ Delete ptr.
}
Delete Last Node
start
10 12 15 NULL 25 NULL
ptr
preptr
ptr
preptr
a[j] = t; }
}
Sorting a Singly Linked List
node *sort(node *start) if (i->data>j->data)
{ {
node *i, *j; t=i->data;
int t; i->data=j->data;
for (i=start;i->next != NULL; i=i->next) j->data=t;
for (j=i->next;j!=NULL;j=j->next) }
return start;
}
Thank you!