Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct elem elem;
- typedef struct elem {
- int value;
- elem *right, *left;
- }elem;
- void add(elem *head, int x) {
- elem *cur = head;
- while (cur->right != NULL) {
- cur = cur->right;
- }
- elem *new_elem = (elem*)malloc(sizeof(elem));
- new_elem->value = x;
- new_elem->right = NULL;
- new_elem->left = cur;
- cur->right = new_elem;
- }
- void delite(elem *head, int x) {
- elem *cur = head;
- while (cur->value != x) {
- cur = cur->right;
- }
- if (cur == NULL) {
- return;
- }
- if (cur->left != NULL) {
- (cur->left)->right = cur->right;
- }
- if (cur->right != NULL) {
- (cur->right)->left = cur->left;
- }
- free(cur);
- }
- void print_list(elem * head) {
- elem * cur = head;
- cur = cur -> right;
- while (cur != NULL) {
- printf("%d ", cur->value);
- cur = cur -> right;
- }
- printf("\n");
- }
- int main() {
- elem *head = (elem*)malloc(sizeof(elem));
- head->value = 0;
- head->right = NULL;
- head->left = NULL;
- char c;
- while (c != 'q') {
- scanf("%c", &c);
- if (c == '+') {
- int m;
- scanf("%d", &m);
- add(head, m);
- }
- if (c == '-') {
- int m;
- scanf("%d", &m);
- delite(head, m);
- }
- if (c == '!') {
- print_list(head);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement