Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "stdlib.h"
- typedef struct st{
- int data;
- struct st *next;
- }node;
- node* new_node(){
- return (node *) malloc(sizeof(node));
- }
- void add(int num, int ch, node **head, node **start){
- node* temp = new_node();
- temp->data = num;
- // At the end
- if(!ch){
- temp->next = NULL;
- temp->data = num;
- // printf("%d ", temp->data);
- (*head)->next = temp;
- //printf("%d \n", (*head)->next);
- *head = temp;
- //printf("%d \n", *head);
- return;
- }
- //Front
- temp->next = *start;
- *start = temp;
- }
- void display(node *head){
- if(head == NULL)
- return;
- if(head->data != -1)
- printf("%d ", head->data);
- display(head->next);
- }
- // Only deletes from Front
- void delete_node(node **start){
- node* temp = *start;
- *start = temp->next;
- }
- int main(){
- node *head = new_node(), *start, *temp;// = new_node();Still works
- head->next = NULL;
- head->data = -1;
- start = head;
- // printf("%d\n", start->data);
- int n, num, ch;
- scanf("%d", &n);
- while(n--){
- scanf("%d %d", &ch, &num);
- //scanf("%d", &num);
- add(num, ch, &head, &start);
- // display(temp = start);
- printf("\n");
- }
- delete_node(&start);
- display(temp = start);
- // printf("%d", head->next);
- /* head = start;
- while(head != NULL){
- if(head->data != -1)
- printf("%d ", head->data);
- head = head->next;
- // display(start , head);
- }*/
- return 0;
- }
Add Comment
Please, Sign In to add comment