Basic Linear Linked List Code
Two operations of Basic Linear Linked List are designed in this code. In this code one linear linked list is created by using create() function and that basic linear linked list is displayed by using disp() function. Using dynamic memory allocation concept memory is occupied. This is a very simple code where negative value is not considered to be stored in the list, rather negative value will be treated for controlling the loop execution.
For Such more codes click here and for video tutorial click here.
#include
#include
typedef struct Linked {
int val;
struct Linked * next;
}
lnk;
lnk * createlink();
void disp();
int main() {
lnk * head;
head = createlink();
printf("Values in the linked list :: ");
disp(head);
}
lnk * createlink() {
lnk * h = NULL, * tmp, * ptr;
char ch;
int v;
while (1) {
printf("\nEnter the value for the node (enter any negative value to exit) : ");
scanf("%d", & v);
if (v < 0)
return h;
tmp = (lnk * ) malloc(sizeof(lnk));
tmp -> val = v;
tmp -> next = NULL;
if (h == NULL)
h = tmp;
else
ptr -> next = tmp;
ptr = tmp;
}
}
void disp(lnk * h) {
while (h != NULL) {
printf("%d,", h -> val);
h = h -> next;
}
printf("\b");
}