LINKED LIST

If you have any queries please leave a message here
Your Message
×


The first element of a list is called head of list and the last element is called the tail of list. The next element to the head of the list is called its successor. The previous element to the tail (if it is not the head of the list) of the list is called its predecessor.Therefore a head does not have a predecessor and a tail does not have a successor. Any other element of the list has both one successor and one predecessor.

For an empty list the head and tail pointer have the value NULL. When the list has one element the head and tail point to the same.

In a singly linked list program each node is divided two parts the first part holds or points to information about the node and second part holds the address of next node or a NULL value to represent an empty or end of the list.



In a double linked list program each node is divided three parts. The first part holds the address of previous node or a NULL value to represent starting of a list, the second part holds the data and the third part holds the address of next node or a NULL value to represent end of the list.





Linked List Programming in C
/* Header-File declaration */
#include<stdio.h>
#include<stdlib.h>

/* Structure declaration */
struct LinkedList
{
int number;
struct LinkedList *next;
};
typedef struct LinkedList NODE;

/* Global variable declaration */
NODE *head,*temp,*p;

/* Global function declaration */
void insertData(NODE q);
NODE deleteData();
void display();

void main()
{
int choice;
NODE e;
head=NULL;
/* Start infinite loop */
while(1)
{
/* switch-case for insert, delete and display operation */
printf("\n Press 1 for insert Operation");
printf("\n Press 2 for delete operation");
printf("\n Press 3 for display list data");
printf("\n Press 4 for exit window");
printf("\n Enter your choice...");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter the number...");
scanf("%d",&e.number);
insertData(e);
break;
case 2:
e=deleteData();
if(head != NULL)
printf("Deleted Number is - %d",e.number);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n WRONG INPUT");
}
}
}

/* Insert element end of the list */
void insertData(NODE q)
{
if(head == NULL)
{
head=(NODE *) malloc(sizeof(NODE));
head->number=q.number;
head->next=NULL;
}
else
{
p=(NODE *) malloc(sizeof(NODE));
p->number=q.number;
p->next=NULL;
temp=head;
while(temp->next != NULL)
temp=temp->next;
temp->next=p;
}
}

/* Delete element from end of the list */
NODE deleteData()
{
NODE *previous,q;
if(head != NULL)
{
temp=head;
while(temp->next != NULL)
{
previous=temp;
temp=temp->next;
}
q->number=temp->number;
if(temp == head)
head=NULL;
else
previous->next=NULL;
free(temp);
}
else
{
printf("\n No element present in the list");
getch();
}
return q;
}

/* Display list elements */
void display()
{
if(head != NULL)
{
temp=head;
while(temp != NULL)
{
printf("\t%d",temp->number);
temp=temp->next;
}
}
else
printf("\n No element present in the list");
}



Insert element beginning of the list

void insertBeginning(NODE q)
{
NODE *p;
if(head == NULL)
{
head=(NODE *) malloc(sizeof(NODE));
head->number=q.number;
head->next=NULL;
}
else
{
p=(NODE *) malloc(sizeof(NODE));
p->number=q.number;
p->next=head;
head=p;
}
}



Insert element end of the list

void insertEnd(NODE q)
{
NODE *temp,*p;
if(head == NULL)
{
head=(NODE *) malloc(sizeof(NODE));
head->number=q.number;
head->next=NULL;
}
else
{
p=(NODE *) malloc(sizeof(NODE));
p->number=q.number;
p->next=NULL;
temp=head;
while(temp->next != NULL)
temp=temp->next;
temp->next=p;
}
}



Insert element after a particular element in the list

void insertAfterElement(NODE q,int position)
{
NODE *temp,*p;
int count=1;
temp=head;
while(count < position && temp->next != NULL)
{
temp=temp->next;
count++;
}
if(count != position || head == NULL)
{
printf("\n Invalid position number");
getch();
}
else
{
p=(NODE *) malloc(sizeof(NODE));
p->number=q.number;
if(temp->next == NULL)
{
p->next=NULL;
temp->next=p;
}
else
{
p->next=temp->next;
temp->next=p;
}
}
}



Insert element before a particular element in the list

void insertBeforeElement(NODE q,int position)
{
int count=1;
NODE *previous,*temp,*p;
temp=head;
while(count < position && temp->next != NULL)
{
previous=temp;
temp=temp->next;
count++;
}
if(count != position || head == NULL)
{
printf("\n Invalid position number");
getch();
}
else
{
p=(NODE *) malloc(sizeof(NODE));
p->number=q.number;
if(position == 1)
{
p->next=head;
head=p;
}
else
{
p->next=temp;
previous->next=p;
}
}
}



Delete element from beginning of the list

NODE deleteFromFirst()
{
NODE *temp,q;
if(head != NULL)
{
q.number=head->number;
temp=head;
head=head->next;
free(temp);
}
else
{
printf("\n No element present in the list");
getch();
}
return q;
}



Delete element from end of the list

NODE deleteFromLast()
{
NODE *previous,*temp,q;
if(head != NULL)
{
temp=head;
while(temp->next != NULL)
{
previous=temp;
temp=temp->next;
}
q.number=temp->number;
if(temp==head)
head=NULL;
else
previous->next=NULL;
free(temp);
}
else
{
printf("\n No element present in the list");
getch();
}
return q;
}



Delete element from any position of the list

NODE deleteParticularElement(int position)
{
int count=1;
NODE *previous,*temp,q;
temp=head;
while(count < position && temp->next != NULL)
{
previous=temp;
temp=temp->next;
count++;
}
if(count != position || head == NULL)
{
printf("\n Invalid position number");
getch();
}
else
{
q.number=temp->number;
if(position == 1)
{
head=head->next;
free(temp);
}
else
{
previous->next=temp->next;
free(temp);
}
}
return q;
}



Display list in reverse order

void reverseDisplay(NODE *p)
{
if(head == NULL)
printf("\n No element present in the list");
else
{
if(p->next != NULL)
{
reverseDisplay(p->next);
}
printf("\t%d",p->number);
}
}


ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us