Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Node {
- int data;
- struct Node* next;
- };
- int main()
- {
- struct Node* A1 = NULL;
- struct Node* A2 = NULL;
- struct Node* A3 = NULL;
- struct Node* A4 = NULL;
- struct Node* p = NULL;
- A1 = (struct Node*)malloc(sizeof(struct Node));
- A2 = (struct Node*)malloc(sizeof(struct Node));
- A3 = (struct Node*)malloc(sizeof(struct Node));
- A4 = (struct Node*)malloc(sizeof(struct Node));
- A1->data=1;
- A1->next=A2;
- A2->data=2;
- A2->next=A3;
- A3->data=3;
- A3->next=NULL;
- p=A1;
- while (p!=NULL){
- printf("%d ", p->data);
- p=p->next;
- }
- // Inserta 4
- A4->data=4;
- A4->next=A1->next;
- A1->next=A4;
- p=A1;
- printf("\n");
- while (p!=NULL){
- printf("%d ", p->data);
- p=p->next;
- }
- // ELimia A2
- A4->next=A4->next->next;
- free(A2);
- p=A1;
- printf("\n");
- while (p!=NULL){
- printf("%d ", p->data);
- p=p->next;
- }
- }
- **********************************************************************************************************************************
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #define STACK_EMPTY (0)
- struct elt {
- struct elt *next;
- int value;
- };
- typedef struct elt *Stack;
- void stackPush(Stack *s, char value)
- {
- struct elt *e;
- e = malloc(sizeof(struct elt));
- assert(e);
- e->value = value;
- e->next = *s;
- *s = e;
- }
- int stackEmpty(const Stack *s)
- {
- return (*s == 0);
- }
- int stackPop(Stack *s)
- {
- char ret [20];
- struct elt *e;
- assert(!stackEmpty(s));
- ret = (*s)->value;
- /* patch out first element */
- e = *s;
- *s = e->next;
- free(e);
- return ret;
- }
- void stackPrint(const Stack *s)
- {
- struct elt *e;
- for(e = *s; e != 0; e = e->next)
- {
- printf("%d ", e->value);
- }
- putchar('\n');
- }
- int main(int argc, char **argv)
- {
- int i;
- Stack s;
- s = STACK_EMPTY;
- for(i = 0; i < 5; i++)
- {
- printf("push %d\n", i);
- stackPush(&s, i);
- stackPrint(&s);
- }
- while(!stackEmpty(&s)) {
- printf("pop gets %d\n", stackPop(&s));
- stackPrint(&s);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement