
Sample Code:
[pastacode lang=”javascript” manual=”%23include%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0Aint%20main()%0A%7B%0A%20%20%20struct%20node%0A%20%20%20%7B%0A%20%20%20%20%20%20int%20num%3B%0A%20%20%20%20%20%20struct%20node%20*ptr%3B%0A%20%20%20%7D%3B%0A%20%20%20typedef%20struct%20node%20NODE%3B%0A%20%20%20NODE%20*head%2C%20*first%2C%20*temp%3D0%3B%0A%20%20%20int%20count%20%3D%200%3B%0A%20%20%20int%20choice%20%3D%201%3B%0A%20%20%20first%20%3D%200%3B%0A%20%20%20while(choice)%0A%20%20%20%7B%0A%20%20%20%20%20%20head%20%3D(NODE*)%20malloc(sizeof(NODE))%3B%0A%20%20%20%20%20%20printf(%22Enter%20the%20data%20item%3A%20%22)%3B%0A%20%20%20%20%20%20scanf(%22%25d%22%2C%20%26head-%3E%20num)%3B%0A%20%20%20%20%20%20if(first%20!%3D%200)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20temp-%3Eptr%20%3D%20head%3Btemp%20%3D%20head%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20else%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20first%20%3D%20temp%20%3D%20head%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20fflush(stdin)%3B%0A%20%20%20%20%20%20printf(%22Do%20you%20want%20to%20continue(Type%200%20or%201)%3F%5Cn%5Cn%22)%3B%0A%20%20%20%20%20%20scanf(%22%25d%22%2C%20%26choice)%3B%0A%20%20%20%7D%0A%20%20%20temp-%3Eptr%20%3D%200%3B%0A%20%20%20temp%20%3D%20first%3B%20%2F*%20reset%20temp%20to%20the%20beginning*%2F%0A%20%20%20printf(%22%5Cn%20Status%20of%20the%20linked%20list%20is%5Cn%22)%3B%0A%20%20%20while(temp!%3D0)%0A%20%20%20%7B%0A%20%20%20%20%20%20printf(%22%25d%3D%3E%22%2C%20temp-%3Enum)%3B%0A%20%20%20%20%20%20count%2B%2B%3B%0A%20%20%20%20%20%20temp%20%3D%20temp%20-%3E%20ptr%3B%0A%20%20%20%7D%0A%20%20%20printf(%22NULL%5Cn%22)%3B%0A%20%20%20printf(%22No.%20of%20nodes%20in%20the%20list%20%3D%20%25d%5Cn%22%2C%20count)%3B%0A%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]
Output:
[pastacode lang=”markdown” manual=”Enter%20the%20data%20item%3A%2010%0ADo%20you%20want%20to%20continue%20%3CType%200%20or%201%3E%3F%0A1%0AEnter%20the%20data%20item%3A%2020%0ADo%20you%20want%20to%20continue%3CType%200%20or%201%3E%3F%0A1%0AEnter%20the%20data%20item%3A%2030%0ADo%20you%20want%20to%20continue%3CType%200%20or%201%3E%3F%0A0%0AStatus%20of%20the%20Linked%20List%20is%0A10%3D%3E20%3D%3E30%3D%3ENULL%0ANo.of%20nodes%20in%20the%20list%20%3D%203″ message=”” highlight=”” provider=”manual”/]
Advantages of Linked List
- Linked list is dynamic in nature which allocates the memory when required.
- In linked list, stack and queue can be easily executed.
- It reduces the access time.
- Insert and delete operation can be easily implemented in linked list.
Disadvantages of Linked List
- Reverse traversing is difficult in linked list.
- Linked list has to access each node sequentially; no element can be accessed randomly.
- In linked list, the memory is wasted as pointer requires extra memory for storage.