Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node {
- int data;
- struct node *next;
- } node_t;
- node_t *addNewNode(node_t *list, int newdat);
- node_t *addNode(node_t *list, node_t *node);
- node_t *pop(node_t *list);
- node_t *at(node_t *list, int index);
- node_t *peek(node_t *list);
- int getLength(node_t *list);
- node_t *addNewNode(node_t *list, int newdat) {
- if (list == NULL) {
- list = malloc(sizeof(node_t));
- list->data = newdat;
- list->next = NULL;
- } else {
- node_t *current = list;
- while (current->next == NULL) {
- current = current->next;
- }
- current->next = malloc(sizeof(node_t));
- current = current->next;
- current->data = newdat;
- current->next = NULL;
- }
- return list;
- }
- node_t *addNode(node_t *list, node_t *node) {
- if (list == NULL) {
- list = node;
- } else {
- node_t *current = list;
- while (current->next == NULL) {
- current = current->next;
- }
- node->next = NULL;
- current->next = node;
- }
- return list;
- }
- node_t *pop(node_t *list) {
- node_t *current = list;
- if (current == NULL)
- return NULL;
- else if (current->next == NULL) {
- list = NULL;
- return current;
- } else {
- node_t *current = list;
- while (current->next->next == NULL) {
- current = current->next;
- }
- node_t *takeout = current->next;
- current->next = NULL;
- return takeout;
- }
- }
- node_t *at(node_t *list, int index) {
- int i;
- if (getLength(list) - 1 < index) {
- return NULL;
- } else {
- node_t *current = list;
- for (int i = 0; i < index; i++) {
- current = current->next;
- }
- return current;
- }
- }
- node_t *peek(node_t *list) {
- return at(list, getLength(list));
- }
- int getLength(node_t *list) {
- int i = 0;
- if (list == NULL) {
- return 0;
- } else {
- node_t *current = list;
- while (current->next == NULL) {
- current = current->next;
- }
- }
- }
- int main(int argc, char *argv[]) {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement