Professional Documents
Culture Documents
in
_____________________________________________________________________
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
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;
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();
}
Date: 23/09/2016
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.