Linked List Implementation in C
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node* next;
};
void display( struct node** head);
void appendNode( struct node** node1, int val);
void addAtBeginning( struct node** head, int val);
int listLength( struct node** head);
void addNodeAtLocation( struct node** head, int loc, int val);
void reverseList( struct node** head);
int main(){
struct node* head ;
head = NULL ;
//clrscr();
appendNode( &head, 1);
appendNode( &head, 2);
appendNode( &head, 3);
appendNode( &head, 4);
appendNode( &head, 5);
appendNode( &head, 6);
printf("The length of the list is %d \n", listLength(&head) );
display( &head);
reverseList( &head);
display( &head);
printf("The node at 1st is %d \n",getNodeAtLocation( &head, 1));
printf("The node at 2nd is %d \n",getNodeAtLocation( &head, 2));
printf("The node at 3rd is %d \n",getNodeAtLocation( &head, 3));
printf("The node at 4th is %d \n",getNodeAtLocation( &head, 4));
addNodeAtLocation( &head, 3, 400);
addNodeAtLocation( &head, 5, 500);
addAtBeginning( &head, 100);
addAtBeginning( &head, 200);
printf("The node at 1st is %d \n",getNodeAtLocation( &head, 1));
printf("The node at 2nd is %d \n",getNodeAtLocation( &head, 2));
printf("The node at 3rd is %d \n",getNodeAtLocation( &head, 3));
printf("The node at 4th is %d \n",getNodeAtLocation( &head, 4));
addNodeAtLocation( &head, 3, 400);
addNodeAtLocation( &head, 5, 500);
printf("The length of the list is %d\n", listLength(&head) );
display( &head);
return 0;
}
int getNodeAtLocation(struct node** head, int loc){
struct node* temp;
temp = *head;
int i ;
for( i = 1; i < loc ; i++){
temp = temp->next;
}
return temp->value;
}
void addNodeAtLocation( struct node** head, int loc, int val){
struct node *temp, *temp1;
temp = *head;
int i ;
for( i = 1; i < loc ; i++){
temp = temp->next;
}
temp1 = (struct node*)malloc(sizeof(struct node));
temp1->value = val;
temp1->next = temp->next;
temp->next = temp1;
}
void addAtBeginning( struct node** head, int val){
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->next = *head;
temp->value = val;
*head = temp;
}
void display( struct node** head){
struct node* temp ;
if( *head == NULL){
printf("The linked list is empty \n");
}
temp = *head;
while( temp != NULL){
printf("The value is %d\n", temp->value );
temp = temp->next;
}
}
void appendNode( struct node** node1, int val){
struct node* temp;
struct node* temp1;
if( *node1 == NULL){
temp = (struct node*)malloc(sizeof(struct node));
temp->value = val;
temp->next = NULL;
*node1 = temp;
} else {
temp1 = *node1;
while( temp1->next != NULL){
temp1 = temp1->next;
}
temp = (struct node*)malloc(sizeof(struct node));
temp->value = val;
temp->next = NULL;
temp1->next = temp;
}
}
int listLength( struct node** head){
struct node* temp;
temp = *head ;
int length = 0;
if(*head == NULL){
return length;
} else {
while( temp != NULL){
length++;
temp = temp->next;
}
return length;
}
}
void reverseList( struct node** head){
struct node *temp, *temp2, *temp3;
temp = *head;
temp3 = NULL;
if( temp == NULL || listLength(head) == 1 ){
return;
}
while( temp != NULL){
temp2 = temp->next;
temp->next = temp3;
temp3 = temp;
temp = temp2;
}
*head = temp3;
}