0% found this document useful (0 votes)
25 views12 pages

9818 Dsa Exp2

Data structure algorithm

Uploaded by

durgeshadalvi15
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)
25 views12 pages

9818 Dsa Exp2

Data structure algorithm

Uploaded by

durgeshadalvi15
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/ 12

FR.

CONCEICAO RODRIGUES COLLEGE OF ENGINEERING


Department of Electronics and Computer Science

Class: S.E. ECS(Semester III) Subject Name: Data Structures and Algorithms (ECC 303)

Name of the Student: DURGESHA DALVI

Experiment No 2 Roll Number 9818


Date of Performance Date of Submission
Experiment Title Implementation of Linked List
CO Mapping ECC303.1

Problem Statement:
Write C Program to implement linked list.

Objective of the Experiment:


Understand basic operations like creation of linked list, insertion of
node, deletion of nodes in 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

Drawbacks of Linked lists:


1) Extra memory space for a pointer is required for each element of a linked list.
2) Random access is not allowed. Nodes in a linked list must be read in order from the
beginning as linked lists are inherently sequential access.
3) Difficulties arise in linked lists when it comes to traverse them in reverse direction. Singly
linked lists are extremely difficult to navigate backwards, and while doubly linked lists are
somewhat easier to read, memory is wasted in allocating space for a back pointer.
4) Arrays have better cache locality that can make a pretty big difference in performance.

Linked List Operations:


1) Add new node in the beginning Insert_First() :-The function creates a node and adds
it in the beginning of the list
2) Traversing Start with the head and access each node until you reach null. Do not
change the head reference.
3) addLast()- the method appends the node to the end of the list. This requires
traversing, but make sure you stop at the last node
4) Inserting "after" Find a node containing "key" and insert a new node after it.
5) Inserting "before" Find a node containing "key" and insert a new node before that
node.
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
6) Deletion Find a node containing "key" and delete it
7) Display Traverse all nodes of Linked List, display data part of nodes
8) Count : Traverse all nodes of Linked List, while traversing count the nodes, return
count
9) Reverse the list

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

The program was tested for different sets of inputs.


Program is working is SATISFACTORY NOT SATISFACTORY ( Tick appropriate outcome)

Evaluation: -

On Time Completion and Knowledge of the Implementation and Total (10)


Submission (2) topic (4) Output (4)
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science

Date and Signature of teacher:

You might also like