Friday, October 29, 2010

SINGLY LINKED LIST



In computer science, a linked list is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (i.e., a link) to the next record in the sequence.

Singly-linked-list.svg
A linked list whose nodes contain two fields: an integer value and a link to the next node

Linked lists are among the simplest and most common data structures; they provide an easy implementation for several important abstract data structures, including stacks, queues, associative arrays, and symbolic expressions.

The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk. For that reason, linked lists allow insertion and removal of nodes at any point in the list, with a constant number of operations.

On the other hand, linked lists by themselves do not allow random access to the data, or any form of efficient indexing. Thus, many basic operations — such as obtaining the last node of the list, or finding a node that contains a given datum, or locating the place where a new node should be inserted — may require scanning most of the list elements.

Linked lists can be implemented in most languages. Languages such as Lisp and Scheme have the data structure built in, along with operations to access the linked list. Procedural languages, such as C, or object-oriented languages, such as C++ and Java, typically rely on mutable references to create linked lists.

----------------------------------------------------------------------------------------------

A C PROGRAM TO IMPLEMENT OPERATIONS IN SINGLY LINKED LIST

COMPILER EMPLOYED: DEV C++ COMPILER-4.9.9.2

SOURCE FILE SIZE :4 kb

EXE FILE SIZE :23 kb

NOTE: PLEASE INCLUDE THE DESIRED HEADER FILE

---------------------------------------------------------------------------------------------

C PROGRAM SOURCE & EXE DOWNLOAD:

Click download button to download

DISCLAIMER: The following program cannot be ensured of perfection.so any flaws in the program can be notified in the comments section.

----------------------------------------------------------------------------------------------

CODE:

#include

struct link
{
int item;
struct link *NEXT;
};
typedef struct link NODE;
void INSF();
void INSL();
void INSM();
void DELF();
void DELL();
void DELM();
void DISPLAY();
NODE *HEAD=NULL;
int main()
{
int CH,OPT;

do
{
printf("\n LIST OPERATIONS-SINGLY LINKED LIST-IMPLEMENTATION");
printf("\n MENU:");
printf("\n [1].INSERTION-FIRST POSITION");
printf("\n [2].INSERTION-GIVEN POSITION");
printf("\n [3].INSERTION-LAST POSITION");
printf("\n [4].DELETION-FIRST POSITION");
printf("\n [5].DELETION-GIVEN POSITION");
printf("\n [6].DELETION-LAST POSITION");
printf("\n [7].TRAVERSAL");
printf("\n [8].EXIT");
printf("\n\n ENTER OPTION:");
scanf("%d",&CH);
switch(CH)
{
case 1:
printf("\n INVOKING INSERTION OPERATION...");
INSF();
DISPLAY();
break;
case 2:
printf("\n INVOKING INSERTION OPERATION...");
INSM();
DISPLAY();
break;
case 3:
printf("\n INVOKING INSERTION OPERATION...");
INSL();
DISPLAY();
break;
case 4:
printf("\n INVOKING DELETION OPERATION...");
DELF();
DISPLAY();
break;
case 5:
printf("\n INVOKING DELETION OPERATION...");
DELM();
DISPLAY();
break;
case 6:
printf("\n INVOKING DELETION OPERATION...");
DELL();
DISPLAY();
break;
case 7:
printf("\n INVOKING DISPLAY OPERATION...");
DISPLAY();
break;
case 8:
break;
default:
printf("\n INVALID CHOICE...\n");
break;
}
printf("\n\n DO YOU WISH TO CONTINUE:1~0");
scanf("%d",&OPT);
}while(OPT==1);
printf("\n TERMINATING PROCESS...");

return 0;
}

void INSF()
{
printf("\n\n INSERTION OPERATION INVOKED...");
NODE *TEMP;
TEMP=(NODE *)malloc(sizeof(NODE));
printf("\n ENTER DATA:");
scanf("%d",&TEMP->item);
TEMP->NEXT=HEAD;
HEAD=TEMP;
}

void INSM()
{
printf("\n\n INSERTION OPERATION INVOKED...");
int i=1,pos;
NODE *CUR=HEAD,*TEMP;
printf("\n ENTER POSITION:");
scanf("%d",&pos);
while(pos!=i+1&&CUR!=NULL)
{
CUR=CUR->NEXT;
i++;
}
if(pos==i+1)
{
TEMP=(NODE *)malloc(sizeof(NODE));
printf(" ENTER DATA:");
scanf("%d",&TEMP->item);
TEMP->NEXT=CUR->NEXT;
CUR->NEXT=TEMP;
}
}

void INSL()
{
printf("\n\n INSERTION OPERATION INVOKED...");
NODE *TEMP,*CUR=HEAD;
TEMP=(NODE *)malloc(sizeof(NODE));
printf("\n ENTER DATA");
scanf("%d",&TEMP->item);
while(CUR->NEXT!=NULL)
{
CUR=CUR->NEXT;
}
TEMP->NEXT=CUR->NEXT;
CUR->NEXT=TEMP;
}

void DELF()
{
printf("\n\n DELETION OPERATION INVOKED...");
NODE *TEMP=HEAD;
HEAD=HEAD->NEXT;
printf("DELETED NODE:%d",TEMP->item);
free(TEMP);
}

void DELM()
{
printf("\n\n DELETION OPERATION INVOKED...");
int i=1,pos;
NODE *CUR=HEAD,*TEMP;
printf("ENTER THE POSITION TO BE DELETED:");
scanf("%d",&pos);
while(pos!=i+1&&CUR->NEXT!=NULL)
{
CUR=CUR->NEXT;
i++;
}
if(pos==i+1)
{
TEMP=CUR->NEXT;
CUR->NEXT=TEMP->NEXT;
printf("DELETED ITEM:%d",TEMP->item);
free(TEMP);
}
}

void DELL()
{
printf("\n\n DELETION OPERATION INVOKED...");
NODE *TEMP,*CUR=HEAD;
while(CUR->NEXT->NEXT!=NULL)
{
CUR=CUR->NEXT;
}
TEMP=CUR->NEXT;
CUR->NEXT=NULL;
printf("DELETED ITEM:%d",TEMP->item);
free(TEMP);
}

void DISPLAY()
{
printf("\n\n DISPLAY OPERATION INVOKED...");
NODE *CUR=HEAD;
printf("\n NODES:");
while(CUR!=NULL)
{
printf("\n %d",CUR->item);
CUR=CUR->NEXT;
}
printf("->NULL\n");
}



----------------------------------------------------------------------------------------------
Your's friendly,

[MOHANRAM.G],
ADMIN...

0 comments:

Post a Comment