Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * You will write a function list_add() to append an integer to the end
- * of a linked list. You will also write a function called list_find() that
- * will return the list node containing the integer value or NULL if the
- * value is not in the list.
- */
- #include <stdio.h>
- #include <stdlib.h>
- struct lnode {
- int value;
- struct lnode *next;
- };
- struct list {
- struct lnode *head;
- struct lnode *tail;
- };
- /* Append the value to the end of the linked list */
- void list_add(lst, value)
- struct list *lst;
- int value;
- {
- /*1. Allocate the new node in the heap. */
- struct lnode* newNode;
- newNode = malloc(sizeof(struct lnode));
- /*2. Set its value to value argument */
- newNode->value = value;
- /*3. Make the next pointer point to NULL, it's at the end of our list */
- newNode->next = NULL;
- /* Check if it's the first node in list */
- if (lst->head == NULL) {
- lst->head = newNode;
- lst->tail = newNode;
- /* If it's not, add it to the end of the list */
- } else {
- lst->tail->next = newNode;
- lst->tail = newNode;
- }
- }
- /* Search for the user-provided value in the given user-provided linked list */
- struct lnode* list_find(lst, value)
- struct list *lst;
- int value;
- {
- /* Create a pointer to a current node */
- struct lnode *cur;
- /* Iterate through the list searching for user-provided value */
- for (cur = lst->head; cur != NULL; cur = cur->next) {
- /* If we find the value, return the lnode */
- if (cur->value == value) {
- return cur;
- }
- }
- return NULL;
- }
- void list_remove(lst, value)
- struct list *lst;
- int value;
- {
- /* Remove the value from the linked list. */
- /* Search the linked list until you find the value */
- /* Need to keep track of the previous node so we can update the links */
- struct lnode* cur;
- struct lnode* prev;
- /* Initialize prev */
- prev = NULL;
- for (cur = lst->head; cur != NULL; cur = cur->next) {
- /* Value found */
- if (cur->value == value) {
- /* Value to be removed is at front of list */
- if (prev == NULL) {
- lst->head = cur->next;
- } else {
- prev->next = cur->next;
- }
- free(cur);
- /* deleted the value, end the loop */
- break;
- } else {
- /* Value not found */
- prev = cur;
- }
- }
- }
- void list_dump(lst)
- struct list *lst;
- {
- struct lnode *cur;
- printf("\nDump:\n");
- for(cur=lst->head; cur != NULL; cur=cur->next) {
- printf(" %d\n", cur->value);
- }
- };
- int main()
- {
- void list_add();
- void list_dump();
- void list_remove();
- struct lnode * list_find();
- struct list mylist;
- struct lnode * mynode;
- mylist.head = NULL;
- mylist.tail = NULL;
- list_add(&mylist, 10);
- list_add(&mylist, 20);
- list_add(&mylist, 30);
- list_dump(&mylist);
- list_remove(&mylist, 42);
- list_remove(&mylist, 10);
- list_dump(&mylist);
- list_remove(&mylist, 30);
- list_dump(&mylist);
- list_add(&mylist, 40);
- list_dump(&mylist);
- }
Advertisement
Add Comment
Please, Sign In to add comment