Practical+Algorithm of Singly Linkedlist
Practical+Algorithm of Singly Linkedlist
Program:
void insert_from_head(int x)
{
//create a new node
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = x;
ptr->next = NULL;
Algorithm:
Step 1: [Underflow?]
If AVAIL = NULL
then Write(‘AVAIBILITY STACK UNSERFLOW’)
Return(FIRST)
Step 4: [Initialize the fields of new node and its link to the list]
INFO(NEW) ← X
LINK(NEW) ← FIRST
if(head==NULL)
{
head = ptr;
return;
}
if (head->data >= k)
{
ptr->next = head;
head = ptr;
return;
}
Step 6: [Does the new node precede all others in the list?]
If INFO(NEW) ≤ INFO(FIRST)
then LINK(NEW) ← FIRST
Return(NEW)
Note that:
For all these algorithms
FIRST is head in our program,
NEW means our ptr (newly created node),
SAVE is temporary struct node pointer,
LINK(NODENAME) is nodename->next and
INFO(NODENAME) is nodename->data.
void insert_from_head(int x)
{
//create a new node
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = x;
ptr->next = NULL;
//add the new Node ptr at the end of the linked list
last->next = ptr;
}
}
if(head==NULL)
{
head = ptr;
return;
}
int main()
{
if(head==NULL)
{
printf("\nLinkedList Underflow on deletion");
return;
}
if(head->data == x)
{
del = head;
if(del->next==NULL)
{
printf("\nData not found.");
}
del = del->next;
q = q->next;
}
q->next = del->next;
free(del);
}
Algorithm:
Step 1: [Empty List?]
If FIRST = NULL
then Write(‘Underflow’)
Return
Step 3: [Find X]
Repeat thru step 5 while TEMP ≠ X and LINK(TEMP) ≠ NULL
Step 7: [Delete X]
If X = FIRST (Is X the first node?) then
FIRST ← LINK(FIRST)
else
LINK(PRED) ← LINK(X)
//add the new Node ptr at the end of the linked list
last->next = ptr;
}
}
void deletelast()
{
if(head==NULL)
{
printf("\nLinkedList Underflow on deletion");
return;
}
struct node *del, *q;
q = head;
del = q->next;
while(del->next)
{
del = del->next;
q = q->next;
}
free(del);
q->next=NULL;
}
void deleteValue(int x)
{
struct node *del, *q;
if(head==NULL)
{
printf("\nLinkedList Underflow on deletion");
return;
}
if(head->data == x)
{
del = head;
head = head->next;
free(del);
return;
}
q = head;
del = q->next;
while(del->data != x)
{
if(del->next==NULL)
{
printf("\nData not found.");
}
del = del->next;
q = q->next;
}
q->next = del->next;
int main()
{
int choice,k;
int i,data[100]={3, 6, 9, 12, 30};
for(i=0;i<5;i++)
{
createList(data[i]);
}
printf("Linked list before deletion : \n");
display();
printf("\nEnter Choice of deletion: ");
scanf("%d", &choice);
switch(choice)
{
case 1: deletefirst(); break;
case 2: deletelast(); break;
case 3: printf("\nEnter position of the node to be deleted :");
scanf("%d", &k);
delete_at_position(k);
break;
case 4: printf("\nEnter data to be deleted :");
scanf("%d", &k);
deleteValue(k);
break;
default: printf("\nEnter valid choice.");
}
printf("Linked list after deletion : \n");
void traverse ()
{
struct node *p = head;
printf("\n\nList elements are - \n");
while (p! = NULL)
{
printf("%d -->",p->data);
p = p->next;
}
printf("NULL");
}
Algorithm:
STEP 1: SET PTR = FIRST
STEP 2: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 6
END OF IF
STEP 3: REPEAT STEP 4 AND 5 UNTIL PTR!= NULL
STEP 4: PRINT PTR→ DATA
STEP 5: PTR = PTR → NEXT
[END OF LOOP]
STEP 6: EXIT
#include <stdio.h>
#include <stdlib.h>
// Node for linked list
struct node {
//add the new Node ptr at the end of the linked list
last->next = ptr;
}
}
// Driver Code
int main(void)
{
// Given nodes value
int data[] = { 1, 2, 3, 4, 5 };
int i;
void pop()//delete_from_head
{
if(head==NULL)
{
printf("\nStack underflow on pop");
}
struct node *del;
del = head;
head = head->next;
free(del);
count--;
}
void display()
{
int main()
{
int choice=0,k;
printf("Enter the size of stack:");
scanf("%d", &size);
while(choice != 3)
{
printf("\nEnter Choice of Operation: ");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("\nEnter data to be pushed :");
scanf("%d", &k);
push(k);
printf("\nStack after operation push is: \n");
display();
break;
case 2: pop();
printf("\nStack after operation pop is: \n");
display();
break;
void dequeue()//delete_from_head
{
if(head==NULL)
{
printf("\nStack underflow on dequeue");
}
struct node *del;
del = head;
head = head->next;
free(del);
count--;
}
void display()
{
struct node *p;
return 0;
}