I am learning C and tried to make a basic singly linked list. I am looking for some feedback on my implementation. I have implemented deletion and reversal functions. Be hard on me I am trying to improve.
#include <stdio.h>
#include <stdlib.h>
//very basic linked list implimentation in C.
//basic node structure. No typedef.
struct node
{
int val;
struct node *next;
};
struct node *deleteNode(struct node *first, int num)
{
struct node *prev=NULL;
struct node *root = first;
while (root != NULL)
{
if (root->val == num)
{
if (prev != NULL && root->next != NULL) //middle elements
{
prev->next = root -> next;
free(root);
}
else if (prev == NULL) //first element
{
free(first);
first = root->next;
root = root->next;
}
else if (root->next == NULL) //last element
{
prev->next = NULL;
free(root);
}
}
prev = root;
root = root -> next;
}
return first;
}
struct node *reverseList(struct node *root)
{
struct node *temp= NULL;
struct node *prev= NULL;
while (root!= NULL)
{
temp = root->next;
root->next = prev;
prev = root;
root = temp;
}
return prev;
}
void printList(struct node *root)
{
while (root!= NULL)
{
printf("Value: %d\n", root -> val);
root = root -> next;
}
}
int main()
{
struct node *root = NULL, *tail = NULL;
int i=1;
for (;i<=20; ++i)
{
struct node *current = malloc(sizeof(struct node));
current -> val = i;
current -> next = NULL;
if (root==NULL) root = current;
else tail -> next = current;
tail = current;
}
//delete first,last and middle element
root = deleteNode(root, 20);
root = deleteNode(root, 1);
root = deleteNode(root, 10);
root = reverseList(root);
printList(root);
return 0;
}
EDIT: this gist I have the current state of this code after the suggestions given: https://gist.github.com/r-s-/6395640 or alternatively here is the most recent code:
#include <stdio.h>
#include <stdlib.h>
//very basic linked list implimentation in C.
//basic node structure. No typedef.
struct node
{
int val;
struct node *next;
};
void deleteList(struct node *first)
{
struct node *tmp;
while (first != NULL)
{
tmp = first->next;
free(first);
first = tmp;
}
}
struct node *deleteNodes (struct node *first, int num)
{
struct node *prev = NULL, *active = NULL;
if (!first) return NULL;
if (first && first->val == num)
{
struct node *temp = first;
first = first->next;
free(temp);
}
prev = first;
active = first->next;
while (active)
{
if (active->val == num)
{
prev->next = active->next;
free(active);
active = prev->next;
}
else
{
prev = active;
active = active ->next;
}
}
return first;
}
struct node *reverseList(struct node *root)
{
struct node *temp= NULL;
struct node *prev= NULL;
while (root!= NULL)
{
temp = root->next;
root->next = prev;
prev = root;
root = temp;
}
return prev;
}
void printList(struct node *root)
{
struct node *temp = root;
while (temp!= NULL)
{
printf("Value: %d\n", temp-> val);
temp= temp-> next;
}
}
int main()
{
struct node *root = NULL, *tail = NULL;
int i=1;
for (;i<=20; ++i)
{
struct node *current =(struct node*) malloc(sizeof(struct node));
current -> val = i;
current -> next = NULL;
if (root==NULL) root = current;
else tail -> next = current;
tail = current;
}
//delete first,last and middle element
root = deleteNodes(root, 20);
root = deleteNodes(root, 1);
root = deleteNodes(root, 10);
//reverse list
root = reverseList(root);
//print list
printList(root);
//delete list
deleteList(root);
return 0;
}
deleteNodes
is wrong. What happens if the first two elements are to be deleted? – Vedran Šego Sep 4 '13 at 22:38