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. Insert element beginning of the list

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); } }

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

WHAT WE DO

 Follow Us 