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 *prev, *nxt;
- } Node;
- Node *newNode(int data, Node *prev, Node *nxt) {
- Node* node = (Node*)malloc(sizeof(Node));
- if (node == NULL) {
- perror("");
- exit(1);
- }
- node->data = data;
- node->prev = prev;
- node->nxt = nxt;
- return node;
- }
- void addToStart(Node **head, int data) {
- Node* node = newNode(data, NULL, *head);
- if ((*head) != NULL) {
- (*head)->prev = node;
- }
- (*head) = node;
- }
- void printList(const Node *const head) {
- for (Node* tmp = head; tmp != NULL; tmp = tmp->nxt) {
- printf("%d ", tmp->data);
- }
- putchar('\n');
- }
- Node* searchNode(const Node *const head, int data) {
- for (Node* tmp = head; tmp != NULL; tmp = tmp->nxt) {
- if (tmp->data == data) {
- return tmp;
- }
- }
- return NULL;
- }
- void deleteNode(Node **head, Node *target) {
- if (target == *head) {
- *head = target->nxt;
- return;
- }
- target->prev->nxt = target->nxt;
- if (target->nxt == NULL) {
- return;
- }
- target->nxt->prev = target->prev;
- }
- void deleteByValue(Node **head, int data) {
- Node* target = searchNode(*head, data);
- if (target != NULL) {
- deleteNode(head, target);
- }
- }
- int main(void) {
- Node* node = NULL;
- addToStart(&node, 3);
- addToStart(&node, 2);
- addToStart(&node, 1);
- deleteByValue(&node, 2);
- addToStart(&node, 12);
- printList(node);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement