0% found this document useful (0 votes)
13 views

2.Implementation of singly linked list (1)

Uploaded by

rishi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

2.Implementation of singly linked list (1)

Uploaded by

rishi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2.

Implementation of singly linked list

Aim : To implement of singly linked list in data structure.

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.

Each node in a list consists of at least two parts:

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:

 traverse(): To see the contents of the linked list, it is necessary to


traverse the given linked list. The given traverse() function
traverses and prints the content of the linked list.
 insertBegin(): This function simply inserts an element at the
front/beginning of the linkedlist.
 insertEnd(): This function inserts an element at the end of the linked list.
 insertAt(): This function inserts an element at a specified position in the linked
list.
 deleteAt(): This function deletes an element from a specified position in the
linked list.
 Search(X): This function checks if X is present in the linked list or not. And
return the position of element, if it is found.

Program:

#include<stdio.h>
#include<stdlib.h>
//#include<conio.h>

typedef struct node


{
int data;
struct node* next;
} Node;
Node* head= NULL;
int size;

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 deleteElement(int element)


{
int pos;
pos = search(element);
deleteAt(pos);
}

void deleteAt(int pos)


{
Node *p,*prev;
prev= NULL;
p= head;
int counter;
if(pos==1)
{
head = head ->next;
realloc(p,0);
size--;
printf("Deleted successfully");
}
else if (pos>1 && pos<=size)
{
counter=1;
while(counter<pos-1)
{
prev = p;
p = p->next;
counter++;
}
prev->next = p->next;
realloc(p,0);
size--;
printf("Deleted successfully");
}
else
{
printf("Error: Invalid Position");
}
}
int search(int element)
{
Node *p;
int counter = 1;
p = head;
if(IsEmpty())
printf("Error: List is Empty");
else
{
while(p!= NULL)
{
if(p->data == element)
return counter;
p = p->next;
counter++;
}
return -1;
}
}
void traverse()
{
Node* p;
printf("\nList Items are: \n");
p = head;
printf("Head:|%X|--->",p);
while(p!= NULL)
{
printf("|%d|%X|--->", p->data, p->next);
p = p->next;
}
}

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);
}
}
}

You might also like