9818 Dsa Exp2
9818 Dsa Exp2
Class: S.E. ECS(Semester III) Subject Name: Data Structures and Algorithms (ECC 303)
Problem Statement:
Write C Program to implement linked list.
Theory:
● A linked list is a data structure consisting of group of nodes which together
represent a sequence. Under the simplest form, each node is composed of a data and
a pointer (in other words, a link) to the next node in the sequence. This structure
allows for efficient insertion or removal of elements from any position in the
sequence.
• A linked list has the following properties.
⮚ Successive elements are connected by pointers
⮚ The last element points to NULL
⮚ Can grow or shrink in size during execution of a program
⮚ Can be made just as long as required (until systems memory exhausts)
⮚ Does not waste memory space (but takes some extra memory for pointers). It
allocates memory as list grows.
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
Disadvantages of Arrays:-
1. Pre allocates all needed memory up front and wastes memory space for indices in
the array that are empty.
2. Fixed size: The size of the array is static (specify the array size before using it).
3. One block allocation: To allocate the array itself at the beginning, sometimes it may
not be possible to get the memory for the complete array (if the array size is big).
4. Complex position-based insertion: To insert an element at a given position, we may
need to shift the existing elements
• Linked list provides following two advantages over arrays: a) Dynamic size b) Ease
of insertion/deletion
Source Code:
//linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
struct node* create(struct node*);
void display(struct node*);
struct node* insert_beg(struct node*);
struct node* insert_end(struct node*);
struct node* insert_pos(struct node*);
struct node* delete_beg(struct node*);
struct node* delete_end(struct node*);
struct node* delete_pos(struct node*);
void main()
{
int n=1,option;
do
{
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
printf("\nEnter 1:create 2: display 3:insert node in the beginning 4: insert at the end 5:
insert at specific position");
printf(" \n6:delete the first node 7: Delete node at the end 8:Delete specific node");
scanf("%d",&option);
switch(option)
{
case 1:
start=create(start);
break;
case 2:
display(start);
break;
case 3:
start=insert_beg(start);
break;
case 4:
start=insert_end(start);
break;
case 5:
start=insert_pos(start);
break;
case 6:
start=delete_beg(start);
break;
case 7:
start=delete_end(start);
break;
case 8:
start=delete_pos(start);
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
break;
default:
printf("\n wrong option");
break;
}
printf("\nenter 0 to exit");
scanf("%d",&n);
}while(n!=0);
}
struct node* create(struct node* start)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
int data;
printf("\n enter data");
scanf("%d",&data);
newnode->info=data;
if (start==NULL)
{
start=newnode;
newnode->next=NULL;
}
else
{
struct node*temp=start;
while(temp->next!=NULL)
temp=temp->next;
newnode->next=NULL;
temp->next=newnode;
}
return start;
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
}
void display(struct node* start)
{
if (start==NULL)
printf("\n empty linked list");
else
{
struct node*temp=start;
while(temp!=NULL)
{
printf("\n%d",temp->info);
temp=temp->next;
}
}
}
struct node* insert_beg(struct node *start)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
int data;
printf("\n enter data");
scanf("%d",&data);
newnode->info=data;
newnode->next=start;
start=newnode;
return start;
}
struct node* insert_end(struct node *start)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
int data;
printf("\n enter data");
scanf("%d",&data);
newnode->info=data;
struct node*temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
newnode->next=NULL;
return start;
}
struct node* insert_pos(struct node *start)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
int data,search;
printf("\n enter data");
scanf("%d",&data);
newnode->info=data;
printf("\n enter search item");
scanf("%d",&search);
struct node*temp=start;
while(temp!=NULL)
{
if (search==temp->info)
break;
temp=temp->next;
}
newnode->next=temp->next;
temp->next=newnode;
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
return start;
}
struct node* delete_beg(struct node* start)
{
struct node* temp=start;
start= temp-> next;
free (temp);
return start;
}
struct node* delete_end(struct node* start)
{
struct node* temp=start;
while (temp -> next -> next != NULL)
{
temp = temp -> next;
}
struct node * ptr = temp -> next;
temp -> next = NULL;
free (ptr);
return start;
}
struct node* delete_pos(struct node* start)
{
struct node* temp=start;
int search;
printf("Enter search item: ");
scanf("%d",&search);
while (temp != NULL)
{
if (search == temp->info)
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
break;
temp = temp -> next;
}
struct node * ptr = start;
while (ptr != NULL)
{
if (search == ptr->next->info)
break;
ptr = ptr -> next;
}
ptr->next=temp-> next;
free (temp);
return start;
}
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
Evaluation: -