0% found this document useful (0 votes)
112 views8 pages

Linked List Implementation of List Adt: Ex NO: 4 DATE:16-09-2021

The document describes a C program to implement a linked list using pointers. It includes functions to initialize a linked list, insert and delete nodes, find a node, retrieve the element at a given position, print the list, and delete the entire list. The main steps of each function are outlined, such as creating new nodes, traversing the list using temp pointers, updating next pointers, and freeing memory. The program code defines a node structure, initializes a header node, and includes a menu-driven main function to test the linked list operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views8 pages

Linked List Implementation of List Adt: Ex NO: 4 DATE:16-09-2021

The document describes a C program to implement a linked list using pointers. It includes functions to initialize a linked list, insert and delete nodes, find a node, retrieve the element at a given position, print the list, and delete the entire list. The main steps of each function are outlined, such as creating new nodes, traversing the list using temp pointers, updating next pointers, and freeing memory. The program code defines a node structure, initializes a header node, and includes a menu-driven main function to test the linked list operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Ex NO: 4

LINKED LIST IMPLEMENTATION OF LIST ADT


DATE :16-09-
2021

Aim
To write a program to implement the linked list using pointers.

Algorithm

1: Start
2: Include all the header files which are used in the program.
3: Declare all the user defined functions.
4: Define a Node structure with two members data and next
5: Define a Node pointer 'head' and set it to NULL.
6: Implement the main method by displaying operations menu and make suitable function calls
in
the main method to perform user selected operation.
7: Stop

Inserting At Specific location in the list (After a Node)


The following steps to insert a new node after a node in the single linked list...

1: Create a newNode with given value.


2: Check whether list is Empty (head == NULL)
3: If it is Empty then, set newNode → next = NULL and head = newNode.
4: If it is Not Empty then, define a node pointer temp and initialize with head.
5: Keep moving the temp to its next node until it reaches to the node after which we want to
insert
the newNode (until temp1 → data is equal to location, here location is the node value
after which
we want to insert the newNode).
6: Every time check whether temp is reached to last node or not. If it is reached to last node then
display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the
function. Otherwise move the temp to next node.
7: Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'

Deleting a Specific Node from the list


The following steps to delete a specific node from the single linked list...

1: Check whether list is Empty (head == NULL)


2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
3: If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1'
with
head.
4: Keep moving the temp1 until it reaches to the exact node to be deleted or to the last node. And
every time set 'temp2 = temp1' before moving the 'temp1' to its next node.
5: If it is reached to the last node then display 'Given node not found in the list! Deletion not
possible!!!'. And terminate the function.
6: If it is reached to the exact node which we want to delete, then check whether list is having
only
one node or not
7: If list has only one node and that is the node to be deleted, then set head = NULL and delete
temp1 (free(temp1)).
8: If list contains multiple nodes, then check whether temp1 is the first node in the list
(temp1 == head).
9: If temp1 is the first node then move the head to the next node (head = head → next) and delete
temp1.
10: If temp1 is not first node then check whether it is last node in the list (temp1 → next
==NULL).
11: If temp1 is last node then set temp2 → next = NULL and delete temp1 (free(temp1)).
12: If temp1 is not first node and not last node then set temp2 → next = temp1 → next and delete
temp1 (free(temp1)).

Displaying a Single Linked List


The following steps to display the elements of a single linked list...
1: Check whether list is Empty (head == NULL)
2: If it is Empty then, display 'List is Empty!!!' and terminate the function.
3: If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
4: Keep displaying temp → data with an arrow (--->) until temp reaches to the last node
5: Finally display temp → data with arrow pointing to NULL (temp → data ---> NULL).
PROGRAM:
#include <stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node {
int element;
struct node *next;
};

typedef struct node *llist;

llist init();
llist insert(llist L,int ch, int pos);
llist del(llist L, int pos);
llist find(llist L, int ch);
int findkth(llist L, int pos);
void printlist(llist L);
void deletelist(llist L);

llist init ()
{
llist L;
L = (struct node *)malloc(sizeof(struct node));
L -> element = -1;
L -> next = NULL;
return L;
}

llist insert ( llist L , int ch , int pos )


{
int i;
llist p, n;

if (pos < 0) {
printf("invalid index");
return L;
}
p = L;
i = 0;
while ((i < pos) && ( p != NULL ))
{
p = p -> next;
++i;
}
if (p == NULL) {
printf("Invalid index ");
return L; }
n = (struct node *)malloc(sizeof(struct node));
n -> element = ch;
n -> next = p -> next;
p -> next = n;
return L;
}

llist del ( llist L , int pos )


{
int i;
llist p, temp;

if (pos < 0) {
printf("invalid index");
return L;
}
p = L;
i = 0;
while ((i < pos) && (p -> next != NULL)) {
p = p -> next;
++i;
}
if (p -> next == NULL)
{
printf("Invalid index ");
return L;
}
temp=p ->next;
p -> next = temp -> next;
free(temp);
return L;
}
llist find( llist L , int ch )
{
llist p;

p = L -> next;
while (p != NULL)
{
if (p -> element == ch)
return p;
p = p -> next;
}
return NULL;
}

int findkth ( llist L , int pos )


{
int i;
llist p;

i = 0;
p = L -> next;
while ((i < pos) && (p != NULL)) {
p = p -> next;
++i;
}
if (p == NULL)
{
printf("Invalid index ");
return -1;
}
return p -> element;
}

void printlist( llist L )


{
llist p;

p = L -> next;
while (p != NULL)
{
printf("%d", p -> element);
p = p -> next;
}
}
void deletelist(llist L)
{
llist p, temp;
p=L->next;
L->next=NULL;
while(p!=NULL)
{
temp=p;
p=p->next;
free(temp);
}
}
int main()
{
typedef struct node *llist;
int n, pos,ch;
llist L;
L=init();
char i;

do
{
printf("1. insert\n2. delete\n3. find\n4. findkth\n5. print list\n6. delete list\nEnter
choice: ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nEnter character and position: ");
scanf("%d%d",&ch,&pos);
L= insert(L,ch,pos);
printf("\nCurrent list= ");
printlist(L);
break;
case 2:
printf("\nEnter position: ");
scanf("%d",&pos);
L= del(L, pos);
break;
case 3: printf("\nEnter character to be found: ");
scanf("%d",&ch);
printf("\nPosition of %d is %d",ch ,find(L, ch));
break;
case 4: printf("\nEnter position: ");
scanf("%d",&pos);
printf("\nElement at %d is %d", pos, findkth(L, pos));
break;
case 5: printf("\nThe list: ");
printlist(L);
break;
case 6: printf("\nList has been deleted.");

deletelist(L);
break;
default:
printf("\nInvalid choice");
break;
}
printf("\nDo you want to continue?<< Enter y if yes >> ");
scanf("%s",&i);
}while(i=='y');
}

OUTPUT:
Result

Thus, the ‘C’ program to implement linked list using pointers has been executed and the
output is verified successfully.

You might also like