#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void ReverseList (struct node **head)
{
struct node *prev = NULL, *curr = *head, *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
return;
}
int main(void)
{
int i;
struct node *head = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("List \r\n");
PrintList(&head);
ReverseList(&head);
printf("Reversed List\r\n");
PrintList(&head);
return 0;
}
CC = gcc
CFLAGS = -g -Wall
all: reverselist
reverselist: reverselist.o
${CC} ${CLAGS} -o $@ reverselist.o
clean:
-@rm *.o
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void ReverseList (struct node **head)
{
struct node *prev = NULL, *curr = *head, *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
return;
}
int main(void)
{
int i;
struct node *head = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("List \r\n");
PrintList(&head);
ReverseList(&head);
printf("Reversed List\r\n");
PrintList(&head);
return 0;
}
CC = gcc
CFLAGS = -g -Wall
all: reverselist
reverselist: reverselist.o
${CC} ${CLAGS} -o $@ reverselist.o
clean:
-@rm *.o
No comments:
Post a Comment