/* Ajith - Syntax Higlighter - End ----------------------------------------------- */

4.25.2010

Check Implementation of Singly Linked List for theoretical explanation regarding implementation of singly linked lists.

  #include <stdio.h>
#include <stdlib.h>

//Structure containing a Data part & a
//Link part to the next node in the List

struct Node
{
int Data;
struct Node *Next;

// Counting number of elements in the List

int length()
{
struct Node *cur_ptr;
int count=0;

while(cur_ptr != NULL)
{
cur_ptr=cur_ptr->Next;
count++;
}
return(count);
}

// Deleting a node from List depending upon the data in the node.

int delNodeData(int num)
{
struct Node *prev_ptr, *cur_ptr;

while(cur_ptr != NULL)
{
if(cur_ptr->Data == num)
{
{
free(cur_ptr);
return 0;
}
else
{
prev_ptr->Next=cur_ptr->Next;
free(cur_ptr);
return 0;
}
}
else
{
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->Next;
}
}

return 1;
}

// Deleting a node from List depending upon the location in the list.

int delNodeLoc(int loc)
{
struct Node *prev_ptr, *cur_ptr;
int i;

if(loc > (length()) || loc <= 0)
{
printf("\nDeletion of Node at given location is not possible\n ");
}
else
{
// If the location is starting of the list
if (loc == 1)
{
free(cur_ptr);
return 0;
}
else
{
for(i=1;i<loc;i++)
{
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->Next;
}

prev_ptr->Next=cur_ptr->Next;
free(cur_ptr);
}
}
return 1;
}

//Adding a Node at the end of the list

{
struct Node *temp1, *temp2;

temp1=(struct Node *)malloc(sizeof(struct Node));
temp1->Data=num;

// Copying the Head location into another node.

{
// If List is empty we create First Node.
}
else
{
// Traverse down to end of the list.
while(temp2->Next != NULL)
temp2=temp2->Next;

// Append at the end of the list.
temp1->Next=NULL;
temp2->Next=temp1;
}
}

// Adding a Node at the Beginning of the List

{
struct Node *temp;

temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data = num;

{
//List is Empty
}
else
{
}
}

// Adding a new Node at specified position

{
int i;
struct Node *temp, *prev_ptr, *cur_ptr;

if(loc > (length()+1) || loc <= 0)
{
printf("\nInsertion at given location is not possible\n ");
}
else
{
// If the location is starting of the list
if (loc == 1)
{
}
else
{
for(i=1;i<loc;i++)
{
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->Next;
}

temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=num;

prev_ptr->Next=temp;
temp->Next=cur_ptr;
}
}
}

// Displaying list contents

void display()
{
struct Node *cur_ptr;

if(cur_ptr==NULL)
{
printf("\nList is Empty");
}
else
{
printf("\nElements in the List: ");
while(cur_ptr!=NULL)
{
printf(" -> %d ",cur_ptr->Data);
cur_ptr=cur_ptr->Next;
}
printf("\n");
}
}

void reverse()
{
struct Node *prev_ptr, *cur_ptr, *temp;

prev_ptr=NULL;

while(cur_ptr != NULL)
{
temp=prev_ptr;
prev_ptr=cur_ptr;

cur_ptr=cur_ptr->Next;
prev_ptr->Next=temp;
}

}

int main(int argc, char *argv[])
{
int i=0;

while(1)
{
printf("\n####################################################\n");
printf("####################################################\n");
printf(" \nInsert a number \n1. At the Beginning");
printf(" \n2. At the End");
printf(" \n3. At a Particular Location in the List");
printf(" \n\n4. Print the Elements in the List");
printf(" \n5. Print number of elements in the List");
printf(" \n6. Reverse the linked List");
printf(" \n\nDelete a Node in the List");
printf(" \n7. Delete a node based on Value");
printf(" \n8. Delete a node based on Location\n");
printf(" \n\n9. Exit\n");
printf(" \nChoose Option: ");
scanf("%d",&i);

switch(i)
{
case 1:
{
int num;
printf(" \nEnter a Number to insert in the List: ");
scanf("%d",&num);
display();
break;
}

case 2:
{
int num;
printf(" \nEnter the Number to insert: ");
scanf("%d",&num);
display();
break;
}

case 3:
{
int num, loc;
printf("\nEnter the Number to insert: ");
scanf("%d",&num);
printf("\nEnter the location Number in List at which \
the Number is inserted: ");
scanf("%d",&loc);
display();
break;
}

case 4:
{
display();
break;
}

case 5:
{
display();
printf(" \nTotal number of nodes in the List: %d",length());
break;
}

case 6:
{
reverse();
display();
break;
}

case 7:
{
int num;
printf(" \nEnter the number to be deleted from List: ");
scanf("%d",&num);
delNodeData(num);
display();
break;
}

case 8:
{
int num;
printf(" \nEnter the location of the node to \
be deleted from List: ");
scanf("%d",&num);
delNodeLoc(num);
display();
break;
}

case 9:
{
struct Node *temp;

{
}
exit(0);
}

default:
{
printf("\nWrong Option choosen");
}
}/* end if switch */
}/* end of while */
}/* end of main */

1. Awesome. _/\_

2. Thanks a lot!
This page has been of immense use to me and my girlfriend while preparing for the exam.
Nowhere on the net there is a better program for linked list, I guess that's what your aim was.
Keep up the good work man!

3. I totally agree with Spectator!
10x a lot for the help with the understanding!!! Your post was really useful for me too!

4. this is an excellent material for linked list basics

5. Thanks man, former Java guy, getting BACK into C after 10 years. It's for a stratup, long story, but has been fun mostly. Not been easy, this is very helpful. MANY of the examples and posts out there won't compile, much less anything else useful

6. the reverse logic was very good
can I do this without the help of temporary pointers???

7. reverse logic was quite interesting
can i do it without temporary pointers???????

8. I had combed the whole internet for a good tutorial on linked list,all to no avail,until i stumbled upon this.Awesome !

9. Gud tutorial.. useful to beginner... thanks man keep it up.

10. Hats off for your code... amazing....

11. Superb code...

12. Clear and easy to understand. Great tutorial! :D

13. It was clear and very easy to understand, its too great. Thanks a lot for creating this program

14. I have been trying to understand this from a very long time. Never did I come across anything that's more clear.Thanks, lots!

15. Can you add a find element in the code/options?

16. thank you
best code that i can find (for homework) , : D

17. it was awesome...thank you so much...nt evn gt a single error while executing.

18. It is awesome thank you!

19. This is awesome!!!!!!!!!!

20. awsome..

21. could you please tell me what is the difference between line 36 and 42 ??
Thank You

22. Can you pls explain reverse logic ?