Linked List Implementation of List Adt: Ex NO: 4 DATE:16-09-2021
Linked List Implementation of List Adt: Ex NO: 4 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
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;
}
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;
}
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;
}
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;
}
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.