»pls help or i am doomed

Discussion in 'Off-Topic' started by kmrox09, Jan 19, 2017.

  1. kmrox09

    kmrox09 Enthusiast Member

    Messages:
    135
    Ratings:
    +53 / -0
    #include<stdio.h>
    #include<stdlib.h>

    struct node
    {
    int data;
    struct node *next;
    }*head;



    void append(int num)
    {
    struct node *temp,*right;
    temp= (struct node *)malloc(sizeof(struct node));
    temp->data=num;
    right=(struct node *)head;
    while(right->next != NULL)
    right=right->next;
    right->next =temp;
    right=temp;
    right->next=NULL;
    }



    void add( int num )
    {
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if (head== NULL)
    {
    head=temp;
    head->next=NULL;
    }
    else
    {
    temp->next=head;
    head=temp;
    }
    }

    void insert(int num)
    {
    int c=0;
    struct node *temp;
    temp=head;
    if(temp==NULL)
    {
    add(num);
    }
    else
    {
    while(temp!=NULL)
    {
    if(temp->data<num)
    c++;
    temp=temp->next;
    }
    if(c==0)
    add(num);
    else if(c<count())
    addafter(num,++c);
    else
    append(num);
    }
    }



    int delete(int num)
    {
    struct node *temp, *prev;
    temp=head;
    while(temp!=NULL)
    {
    if(temp->data==num)
    {
    if(temp==head)
    {
    head=temp->next;
    free(temp);
    return 1;
    }
    else
    {
    prev->next=temp->next;
    free(temp);
    return 1;
    }
    }
    else
    {
    prev=temp;
    prev= temp->next;
    }
    }
    return 0;
    }


    void display(struct node *r)
    {
    r=head;
    if(r==NULL)
    {
    return;
    }
    while(r!=NULL)
    {
    printf("%d ",r->data);
    r=r->next;
    }
    printf("\n");
    }


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


    int main()
    {
    int i,num;
    struct node *n;
    head=NULL;
    while(1)
    {
    printf("\nList Operations\n");
    printf("===============\n");
    printf("1.Insert\n");
    printf("2.Display\n");
    printf("3.Size\n");
    printf("4.Delete\n");
    printf("5.Exit\n");
    printf("Enter your choice : ");
    if(scanf("%d",&i)<=0){
    printf("Enter only an Integer\n");
    exit(0);
    } else {
    switch(i)
    {
    case 1: printf("Enter the number to insert : ");
    scanf("%d",&num);
    insert(num);
    break;
    case 2: if(head==NULL)
    {
    printf("List is Empty\n");
    }
    else
    {
    printf("Element(s) in the list are : ");
    }
    display(n);
    break;
    case 3: printf("Size of the list is %d\n",count());
    break;
    case 4: if(head==NULL)
    printf("List is Empty\n");
    else{
    printf("Enter the number to delete : ");
    scanf("%d",&num);
    if(delete(num))
    printf("%d deleted successfully\n",num);
    else
    printf("%d not found in the list\n",num);
    }
    break;
    case 5: return 0;
    default: printf("Invalid option\n");
    }
    }
    }
    return 0;
    }
    @Arjenpro help not able to delete an input from the link list D:
     
    Last edited: Jan 19, 2017
    #1
  2. kuba1616

    kuba1616 kebab Member

    Messages:
    1,483
    Ratings:
    +1,115 / -0
    emfg tf??
     
    #2
  3. kmrox09

    kmrox09 Enthusiast Member

    Messages:
    135
    Ratings:
    +53 / -0
    I can't find the error in the syntax D:
     
    #3
  4. kmrox09

    kmrox09 Enthusiast Member

    Messages:
    135
    Ratings:
    +53 / -0
    I thought @Arjenpro is a developer he can help me I have to correct this
     
    #4
  5. a12d

    a12d Honorary Poster Member

    Messages:
    456
    Ratings:
    +102 / -0
    Basic c++ lul and arjen probs dont know cuz he dun c++
     
    #4
  6. Ditteamalie

    Ditteamalie Eternal Poster Member

    Messages:
    1,234
    Ratings:
    +647 / -0
    Dude u could just message arjen about this.What was the need of pasting a huge chunk of bs on a forum post -_-
     
    #5

Share This Page