A linked list that can only be traversed in forward direction is called a singly linked list. A singly linked list is traversed from beginning through till end using the root pointer. root contains the address of start of list. Let’s, for example, create a singly linked list and traverse it,
/* create_sll.c -- program creates a singly linked list */ #include <stdio.h> #include <stdlib.h> #define TRAVERSE 1 #define INSERT 2 #define QUIT 3 /* structure declaration */ typedef struct NODE { struct NODE *link; int value; } Node; void insert_sll(Node **, const int); void traverse_sll(Node **); int main(void) { Node *root = 0; /* root is NULL */ Node **p2r = &root; /* pointer-to-root */ int value; int op; puts("\n**Let's create a Singly Linked List**\n"); printf("User, enter 1 for TRAVERSE, 2 FOR INSERT and 3 for QUIT : "); while (1) { while (scanf("%d", &op) == 1 && (op == INSERT || op == QUIT || op == TRAVERSE)) { if (op == TRAVERSE) { traverse_sll(p2r); } else if (op == INSERT) { printf("User, enter an integer value: "); scanf("%d", &value); insert_sll(p2r, value); } else if (op == QUIT) { printf("\nThank You!\n"); return 0; } printf("\nWant to do some more operations,\nenter 1 " "for TRAVERSE, 2 for INSERT and 3 for QUIT : "); } printf("\n\aEntered is a WRONG choice,\nenter 1 " "for TRAVERSE, 2 for INSERT, 3 for QUIT : "); } } void insert_sll(Node **linkp, const int value) { Node *current = 0; Node *newnode = 0; /* Let's create an ordered singly linked list */ /* firstly, ensure if value isn't already in the list */ /* if value is already in the list, we won't add duplicate */ while ((current = *linkp) != NULL && current->value < value) linkp = ¤t->link; /* if value is already in the list */ if (current != NULL && current->value == value) { printf("\n\aValue %d is already in the list.\n", value); return ; } /* value isn't already in the list */ /* Let's allocate memory to newnde */ newnode = (Node *)malloc(sizeof(Node)); /* If memory allocated to newnode successfully */ if (newnode == NULL) { printf("Not sufficient Memory!\n"); exit(EXIT_FAILURE); } /* write in value in value field of newnode */ newnode->value = value; /* insert newnode in the list */ /* between current and previous */ newnode->link = current; *linkp = newnode; } void traverse_sll(Node **linkp) { Node *current = *linkp; /* traverse the list */ if (current == NULL) { printf("\nList is EMPTY.\n"); } else { printf("\nList contains values : "); while (current != NULL) { printf("%d ", current->value); current = current->link; } printf("\n"); } }
Above program creates a Singly Linked List with user specified values. We can traverse this list in forward direction as root points to the beginning of the list and last node contains NULL in the link field which means there’s no more nodes in the list. Further, we can’t get back to a node we already traversed because we don’t have a pointer to it. The only way we can get back to any previous node is to traverse the list from the start or save pointers to desired nodes.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice BCA MCQs
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos