[Courses] [C] Lesson 16: answers for exc 2 and 3

Morgon Kanter admin at surgo.net
Sun Jan 5 17:13:21 EST 2003


Here they are, the others coming soon

/* Begin exc2.c */
#include <stdio.h>
#include <stdlib.h>

/*********************** TYPES ************************/
struct node {
	int number;
	struct node *next;
};

/********************* FUNCTIONS **********************/

/* Finds the offset node in the linked list */
struct node *seek(struct node *first, short offset)
{
	struct node *current;
	short i;

	current = first;
	for(i=0;i < offset;i++) {
		current = current->next;
	}

	return current;
}

struct node *delete_head(struct node *head)
{
	struct node *current;

	current = head->next;
	free(head);
	head = current;

	return head;
}

int main()
{
	char line[3];
	short num;
	short i;
	struct node *first;
	struct node *current;

	first = malloc(sizeof(struct node));
	current = first;

	for(i=0;i < 15;i++) {
		current->number = i;
		current = current->next = malloc(sizeof(struct node));
	}

	current = first;
	while(current->next != NULL) {
		printf("current->number = %i\n", current->number);
		current = current->next;
	}

	do {
		printf("Enter the number to delete: ");
		fgets(line, sizeof(line), stdin);
		sscanf(line, "%hi", &num);
	} while(!(num > -1 || num < 15));
	
	if(num == 0) {
		first = delete_head(first);
	} else {
		current = seek(first, num-1);
	
		{
			struct node *previous;
			previous = current;
			current = current->next;

			/* Changes the links so there is no pointer to 
			 * the current */
			previous->next = current->next;
			free(current);
			current = NULL;
		}
	}
	
	current = first;
	while(current->next != NULL) {
		printf("current->number = %i\n", current->number);
	
		/* Now free the nodes */
		first = current->next;
		free(current);
		current = first;
	}

	return 0;
}
/* End exc2.c */

And #3
/* Begin exc3.c */
#include <stdio.h>
#include <stdlib.h>

/******************** TYPES ********************/
enum direction { forwards, backwards };

struct node {
	int number;
	struct node *next;
	struct node *prev;
};

/******************* FUNCTIONS ******************/
struct node *delete_first(struct node *head)
{
	struct node *current;

	current = head->next;
	current->prev = NULL;
	free(head);
	
	return current;
}

struct node *delete_last(struct node *tail)
{
	struct node *current;

	current = tail->prev;
	current->next = NULL;
	free(tail);

	return current;
}

struct node *seek(struct node *start, short offset, enum direction dir)
{
	struct node *current = start;
	short i;
	if(dir == forwards) {
		for(i=0;i<offset;i++) {
			if(current->next == NULL) {
				return NULL;
			} else {
				current = current->next;
			}
		}
	} else if(dir == backwards) {
		for(i=0;i<offset;i++) {
			if(current->prev == NULL) {
				return NULL;
			} else {
				current = current->prev;
			}
		}
	} else {
		return NULL;
	}

	return current;
}

int main() {
	struct node *first;
	struct node *last;
	struct node *current;
	struct node *previous;
	enum direction dir;
	char line[10];
	short num;
	short i;

	current =  first = malloc(sizeof(struct node));
	for(i=0;i < 14;i++) {
		previous = current;
		current->number = i;
		current = current->next = malloc(sizeof(struct node));
		current->prev = previous;
	}
	last = current;
	last->prev = previous;
	last->number = i;

	current = first;
	for(i=0;i < 15;i++) {
		printf("%i  ", current->number);
		current = current->next;
	}
	putchar('\n');

	current = last;
	for(i=0;i < 15;i++) {
		printf("%i  ", current->number);
		current = current->prev;
	}
	putchar('\n');

	do {
		printf("Enter a number to delete: ");
		fgets(line, sizeof(line), stdin);
		sscanf(line, "%hi", &num);
	} while(!(num > -1 && num < 15));

	if(num == 0) {
		first = delete_first(first);
		last = delete_last(last);
	} else {
		current = seek(first, num, forwards);
		current->prev->next = current->next;
		current->next->prev = current->prev;
		free(current);
	}

	current = first;
	while (current != NULL) {
		printf("%i  ", current->number);
		current = current->next;
	}
	putchar('\n');
	current = last;
	while(current != NULL) {
		printf("%i  ", current->number);
		current = current->prev;
	}
	putchar('\n');

	puts("Cleaning up");
	current = previous = first;
	while(current != NULL) {
		current = current->next;
		free(previous);
		previous = current;
	}

	return 0;
}
/* End exc3.c */

Am I the only one doing this?

Morgon
--
grab my updated (12/21/02) public key from http://www.surgo.net/pubkey.asc



More information about the Courses mailing list