2.Implementation of singly linked list (1)
2.Implementation of singly linked list (1)
Description :
A Linked List is a linear data structure that consists of two parts: one is the
data part and the other is the address part.
A linked list is represented by a pointer to the first node of the linked list. The
first node is called the head of the linked list. If the linked list is empty, then the
value of the head points to NULL.
A Data Item (we can store integer, strings, or any type of data).
Pointer (Or Reference) to the next node (connects one node to another) or An
address of another node
Operations to be performed:
Program:
#include<stdio.h>
#include<stdlib.h>
//#include<conio.h>
void insertAt(int,int);
void insertEnd(int);
void insertBegin(int);
void deleteElement(int);
void deleteAt(int);
void traverse();
int search(int);
int IsEmpty();
int IsEmpty()
{
if(head == NULL)
return 1;
return 0;
}
void insertEnd(int element)
{
Node *temp,*p;
p = head;
if(p == NULL)
insertBegin(element);
else{
temp = (Node*) malloc(sizeof(Node));
temp->data = element;
while(p->next!=NULL)
{
p = p->next;
}
temp->next = p->next;
p->next = temp;
size++;
printf("Inserted successfully");
}
}
void insertBegin(int element){
Node *temp;
temp = (Node*) malloc(sizeof(Node));
temp->data = element;
temp->next = head;
head = temp;
printf("Inserted successfully");
size++;
}
void insertAt(int element, int pos)
{
Node *temp,*p;
int counter;
temp = (Node*) malloc(sizeof(Node));
temp->data = element;
temp->next = NULL;
if(pos==1)
{
temp->next = head;
head = temp;
size++;
printf("Inserted successfully");
}
else if (pos>1 && pos<=size+1)
{
p = head;
counter=1;
while(counter<pos-1)
{
p = p->next;
counter++;
}
temp->next = p->next;
p->next = temp;
size++;
printf("Inserted successfully");
}
else
{
printf("Error: Invalid Position");
}
}
void main()
{
int option,element,pos,after;
//clrscr();
size=0;
while(1)
{
printf("\nLinked List ADT operations:\n");
printf("____________________\n");
printf("1.InsertAt\n");
printf("2.InsertBegin\n");
printf("3.InsertEnd\n");
printf("4.DeleteElement\n");
printf("5.DeleteAt\n");
printf("6.Search\n");
printf("7.Traverse list\n");
printf("8.Exit\n");
printf("____________________\n");
printf("Enter option:");
scanf("%d",&option);
if(option == 1)
{
printf("Enter element and position for insertion:");
scanf("%d %d", &element, &pos);
insertAt(element, pos);
traverse();
}
else if(option == 2)
{
printf("Enter element for insertion:");
scanf("%d", &element);
insertBegin(element);
traverse();
}
else if(option == 3)
{
printf("Enter element for insertion:");
scanf("%d", &element);
insertEnd(element);
traverse();
}
else if(option == 4)
{
printf("Enter element for deletion:");
scanf("%d", &element);
deleteElement(element);
traverse();
}
else if(option == 5)
{
printf("Enter position of element for deletion:");
scanf("%d", &pos);
deleteAt(pos);
traverse();
}
else if(option == 6)
{
printf("Enter element to search:");
scanf("%d", &element);
pos=search(element);
if(pos!=-1)
printf("Element found at %d",pos);
else
printf("Element not found");
}
else if(option == 7)
{
traverse();
}
else
{
exit(0);
}
}
}