You are on page 1of 10

Batch:A-3

Roll No.: 1511053

Experiment / assignment / tutorial


No. 1
Grade: AA / AB / BB / BC / CC /
CD /DD

Signature of the Staff In-charge

TITLE: Implementation of different operations on linked list .


AIM: To understand the advantage of linked list over other structures like arrays
implementing the general linear list.
______________________________________________________________________
Expected OUTCOME of Experiment:

in

CO 1. Explain the different data structures used in problem solving.


_____________________________________________________________________
Books/ Journals/ Websites referred:
1. Data Structures A Psedocode Approach with C, Richard F. Gilberg & Behrouz
A. Forouzan, second edition, CENGAGE learning.
2. Data Structures Using C & C++, Rajesh K. Shukla, Wiley- india.

_____________________________________________________________________
Pre Lab/ Prior Concepts:
A linear list is a list where each element has a unique successor. There are
four common operations associated with linear list: insertion, deletion, retrieval, and
traversal. Linear list can be divided into two categories : general list and restricted
list.In general list the data can be inserted or deleted without any restriction wheras
in restricted list there is restrictions for these operations. Linked list and arrays are
commonly used to implement general linear list.A linked list is simply a chain of
structures which contain a pointer to the next element.It is dynamic in nature. Items
may be added to it or deleted from it at will.

A list item has a pointer to the next element, or to NULL if the current element is the
tail (end of the list). This pointer points to a structure of the same type as itself. This
structure that contains elements and pointers to the next structure is called a Node.
ALGORITHM TO INSERT A NEW NODE IN THE BEGINNING OF THE LINKED
LIST
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET START = New_Node
Step 7: EXIT

ALGORITHM TO DELETE THE NODE FROM THE LINKED LIST


Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PTR->DATA != NUM
Step 5:
SET PREPTR = PTR
Step 6:
SET PTR = PTR->NEXT
[END OF LOOP]
Step 7: SET PREPTR->NEXT = PTR->NEXT
Step 8: FREE PTR
Step 9: EXIT

ALGORITHM TO SEARCH A LINKED LIST


Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3 while PTR != NULL
Step 3:
IF VAL = PTR->DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR->NEXT
[END OF IF]
[END OF LOOP]

Step 4: SET POS = NULL


Step 5: EXIT
ALGORITHAM TO COUNT NO.OF NODES:
Step 1: [INITIALIZE] SET Count = 0
Step 2: [INITIALIZE] SET PTR = START
Step 3: Repeat Step 4 and 5 while PTR != NULL
Step 4:
SET COUNT = COUNT+1
Step 6:
SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: EXIT
ALGORITHM FOR TRAVERSING A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3:
Apply Process to PTR->DATA
Step 4:
SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT

Implementation details: (printout of code)


#include<conio.h>
#include<stdio.h>
#include<alloc.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *start=NULL,*newnode,*ptr,*preptr;
void create()
{
char ch;
do
{
newnode=(node*)malloc(sizeof(node));
printf("\nEnter data\n");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(start==NULL)
{
start=newnode;
}
else
{
ptr=start;
while(ptr->next!=NULL)

ptr=ptr->next;
ptr->next=newnode;
}
printf("\nCREATE ANOTHER FIELD ?< y-YES & n-NO >:");
ch=getche();
}while(ch!='n');
}
void display()
{
ptr=start;
printf("\nLINKED LIST :\nSTART--->");
while(ptr!=NULL)
{
printf("%d--->",ptr->data);
ptr=ptr->next;
}
printf("NULL");
}
void insert_beg()
{
newnode=(node*)malloc(sizeof(node));
printf("\nEnter the data to be inserted:");
scanf("%d",&newnode->data);
newnode->next=start;
start=newnode;
}
void insert_end()
{
newnode=(node*)malloc(sizeof(node));
printf("\nEnter the data to be inserted:");
scanf("%d",&newnode->data);
newnode->next=NULL;
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
ptr=newnode;
}
void insert_b4()
{
int val;
newnode=(node*)malloc(sizeof(node));
printf("\nEnter the data to be inserted");
scanf("%d",&newnode->data);
newnode->next=NULL;
printf("\nEnter the value before which field is to be inserted:");
scanf("%d",&val);
ptr=start;
while(ptr->data!=val)
{
preptr=ptr;
ptr=ptr->next;
}

preptr->next=newnode;
newnode->next=ptr;
}
void insert_aft()
{
int val;
newnode=(node*)malloc(sizeof(node));
printf("\nEnter the value of data to be inserted:");
scanf("%d",&newnode->data);
newnode->next=NULL;
printf("\nEnter the value after which field is to be inserted:");
scanf("%d",&val);
ptr=start;
preptr=ptr;
while(preptr->data!=val)
{
preptr=ptr;
ptr=ptr->next;
}
preptr->next=newnode;
newnode->next=ptr;
}
void delete_beg()
{
ptr=start;
if(start==NULL)
{
printf("\nList is already empty.No further deletion possible");
}
else
{
start=start->next;
free(ptr);
}
}
void delete_end()
{
ptr=start;
if(start==NULL)
{
printf("\nList is alredy empty.No further deletion possible");
}
else
{
while(ptr->next!=NULL)
{
preptr=ptr;
ptr=ptr->next;
}
preptr->next=NULL;
free(ptr);
}
}
void del_any()
{
int val;

printf("\nEnter the value to be deleted");


scanf("%d",&val);
ptr=start;
if(start==NULL)
printf("\nList is already empty.No futher deletion possible");
else
{
while((ptr->next!=NULL) && (ptr->data!=val))
{
preptr=ptr;
ptr=ptr->next;
}
if(ptr->data==val)
{
preptr->next=ptr->next;
free(ptr);
}
else
{
printf("\nValue not found");
}
}
}
void count()
{
int i=0;
ptr=start;
while(ptr!=NULL)
{
ptr=ptr->next;
i++;
}
printf("\nTotal no of node in linked list are %d",i);
}
void search()
{
int i=1,val,flag=0;
printf("\nEnter the number to be searched:");
scanf("%d",&val);
ptr=start;
while(ptr!=NULL)
{
if(ptr->data==val)
{
flag=1;
printf("\nThe element %d is found at position %d",val,i);
break;
}
i++;
ptr=ptr->next;
}
if(flag==0)
printf("\nElement is not found");
}
void sort()
{
node *ptr1,*ptr2;

int t;
ptr1=start;
while(ptr1->next!=NULL)
{
ptr2=ptr1->next;
while(ptr2!=NULL)
{
if(ptr1->data>ptr2->data)
{
t=ptr1->data;
ptr1->data=ptr2->data;
ptr2->data=t;
}
ptr2=ptr2->next;
}
ptr1=ptr1->next;
}
}
void invert()
{
node *follow,*ahead;
ptr=start;
follow=ahead=NULL;
while(ptr!=NULL)
{
ahead=ptr->next;
ptr->next=follow;
follow=ptr;
ptr=ahead;
}
start=follow;
}
void delete_LL()
{
ptr=start;
if(start!=NULL)
{
while(ptr!=NULL)
{
printf("\n%d is to be deleted",ptr->data);
delete_beg();
ptr=start;
}
printf("\nLinked list is now cleared");
}
}
void main()
{
clrscr();
int c;
loop1:
printf("\n*********\n1.CREATE\n2.DISPLAY\n3.INSERT AT
BEGINNING\n4.INSERT AT END\n5.INSERT BEFORE NODE\n6.INSERT AFTER
NODE\n7.DELETE FROM BEGINNING\n8.DELETE FROM END\n9.DELETE
ANY\n10.COUNT\n11.SEARCH\n12.SORT LINKED LIST\n13.REVERSE LINKED
LIST\n14.DELETE ENTIRE LIST\n15.EXIT\nPLEASE ENTER YOUR CHOICE:");

scanf("%d",&c);
switch(c)
{
case 1: create();
break;
case 2: display();
break;
case 3:insert_beg();
break;
case 4:insert_end();
break;
case 5:insert_b4();
break;
case 6:insert_aft();
break;
case 7:delete_beg();
break;
case 8:delete_end();
break;
case 9:del_any();
break;
case 10:count();
break;
case 11:search();
break;
case 12:sort();
break;
case 13:invert();
break;
case 14:delete_LL();
break;
case 15: exit(0);
break;
default:printf("\nPLEASE ENTER PROPER CHOICE");
}
goto loop1;
getch();
}

Conclusion: Thus we performed various types of insertion and deletion on the


linked list and executed.

Date: 23/09/2016

Signature of faculty in-charge

Post Lab Descriptive Questions (Add questions from examination point view)
1. Explain ADT. Give examples for linear and non-linear data structures.
Ans- In computer science, an abstract data type (ADT) is a mathematical model
for data types where a data type is defined by its behavior (semantics) from the point
of view of a user of the data, specifically in terms of possible values, possible
operations ondata of this type, and the behavior of these operations.

With an ADT users are not concerned with how the task is done BUT what can it do.
In other words ADT is an set of machine instructions to use functions while hiding
implementation.

Examples of linear data structures1. Array


2. Linked list
3. Stack
4. Queue
Examples of linear data structures1. Graph
2. Tree
3. Explain advantages of linked list over array in implementing linear list.
Ans- Both Arrays and Linked List can be used to store linear data of similar types,
but they both have some advantages and disadvantages over each other.
Following are the points in favour of Linked Lists.
(1) The size of the arrays is fixed: So we must know the upper limit on the number of
elements in advance. Also, generally, the allocated memory is equal to the upper
limit irrespective of the usage, and in practical uses, upper limit is rarely reached.
(2) Inserting a new element in an array of elements is expensive, because room has
to be created for the new elements and to create room existing elements have to
shifted.
For example, suppose we maintain a sorted list of IDs in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040, ..].
And if we want to insert a new ID 1005, then to maintain the sorted order, we have to
move all the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are
used. For example, to delete 1010 in id[], everything after 1010 has to be moved.
So Linked list provides following two advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion

4. List some applications of data structures.

Ans- Stack: In operating systems stack is to stored memory locations when a


function is called, when a function calls an another one the OS pushes the return
address on the top, when the current routine returns it pops the memory location and
the control jumps there. Think how recursive functions can be replaced with a stack?
Queue: While we work with a computer, several processes runs behind, each
process wants to get some CPU time. Some process needs a lot of time, some need
a very little time, some processes needs to processed very urgently. What OS does
is to put them in a queue. There are different CPU allocation methods such as batch,
time shared etc. In most cases priority queues are used.
Linked List: Think about an selection sort algorithm, each time we find the element
which needs to be put in the correct position we need to shift the entire elements by
one position if we use an array. That could take a lot of time, instead if we use a
linked list, all we need to update is 3 links.
Tree: Trees have a wide variety of applications, for example a binary tree is used in
applications were a lot of searching needs to be done. This is a very simple example.
Heap sort, which has minimal space complexity is implemented using a Tree.
Applications range from systems programming to social networking websites.

You might also like