Assignment 7 Question 2

#include <stdio.h>
#include <stdlib.h>



struct node {
    int data;
    struct node *next;
};
typedef struct node NODE;

int main()
{
    NODE *head=NULL;
    NODE *tail=NULL;
    int a,n;
    scanf("%d",&a);

    while(a!=-1)
    {
        NODE *s;
        s=(NODE *)malloc(sizeof(NODE));
        s->data=a;
        s->next=NULL;
        if(head==NULL)
        {
             head=s;
             tail=s;
        }
        else
        {
            tail->next=s;
            tail=s;
        }



        scanf("%d",&a);
    }

    scanf("%d",&n);

    struct node *temp=head;
    int count=0;
    while(temp!=NULL)
    {
        count++;
        temp=temp->next;
    }

    int l=count-n;

    if(l<0)
    {
        printf("-1");
    }
    else
    {
        while(l!=0)
        {
            head=head->next;
            l--;

        }

         temp=head;

    while(temp!=NULL)
    {
        printf("%d",temp->data);
        printf(" ");
        temp=temp->next;
    }
    }


}

Comments

Popular posts from this blog

Assignment 8 Question 1

Assignment 6 Question 2