Results 1 to 4 of 4

Thread: Insert an element after a specific item.

  1. #1
    Join Date
    May 2010
    Beans
    2

    Post Insert an element after a specific item.

    how to create a function that would be able to insert an element after a specific item.
    Here's my list:


    Code:
     struct list{
    char c;
    list *next;
    }*p,*cur,*prev,*top;
    
    void show(void);
    void push(char value);
    void pop(char value);
    
    int main()
    {
        top=0;
        char key,value;
        int done=false;
        while(!done){
            system("cls");
            show();
            printf("\nA)dd\nD)elete\nQ)uit\n");
            key=getchar();
            switch(toupper(key))
            {
            case 'A':
        value=getch();
        push(value);
        break;
            case 'D':
                printf("\nDel:\n");
                value=getch();
                pop(value);
                break;
            case 'Q':
                done=true;
                break;
            }
        }
        return 0;
    }
    void push(char value)
    {
        printf("\nInput:\n");
        p=new list;
        p->c=value;
        p->next=NULL;
        while(cur && p->c > cur->c)
        {
        prev=cur;
        cur=cur->next;
        }
        if(prev==NULL)
        {
        p->next=top;
        top=p;
        }
        else
        {
        p->next=cur;
        prev->next=p;
        }
    }
    void pop(char value)
    {
        prev=top;cur=top->next;
        if(value==top->c)
        {
        p=top;
        top=top->next;
        free (p);
        }
        else
        {
        while(cur!=NULL && value!=cur->c)
        {
        prev=cur;
        cur=cur->next;
        }
        if(cur!=0)
        {
        p=cur;
        prev->next=cur->next;
        free (p);
        }
             }
    }
    void show()
    {
        p=top;
        if(p==0)
        printf("\n list is empty\n");
        else
        printf("\nLIST:\n");
        while(p)
        {
        printf("%c\n",p->c);
        p=p->next;
        }
    }

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Insert an element after a specific item.

    seems like homework.
    homework questions are not allowed.

    If you wrote the push and pop functions it should not take much thought to figure out the insert by yourself.

  3. #3
    Join Date
    May 2010
    Beans
    2

    Re: Insert an element after a specific item.

    Quote Originally Posted by MadCow108 View Post
    seems like homework.
    homework questions are not allowed.

    If you wrote the push and pop functions it should not take much thought to figure out the insert by yourself.
    There is an attempt, but it does not work
    void add(char value)
    {
    p=new list;
    if(p==NULL) { printf("Error");getch();exit(1);}
    p->c=value;
    p->next=NULL;
    top=prev;
    for(int i=0;top!=NULL;i++)
    {
    if(i==value)
    {
    p->next=top->next;
    }
    top->next=p; break;
    }
    top=top->next;
    }

  4. #4
    Join Date
    Jan 2007
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Insert an element after a specific item.

    Well this looks like a linked list so if you mean after I assume after the order of the element.

    What you do is you need to know the pointer to the struct to the specific item. How you do that is up to you. Then you make a temp pointer to hold the next item of list. Create what you are going to insert. If you already have that pointer it is good. Now you point the next pointer in the specific item to the inserted one. From the inserted next pointer you point it to the next pointer you point to the temp pointer.

    So if you have a list like this:

    a -> b -> c -> d

    And want to insert e between b and c you have

    temp = c;
    b.next = e;
    e.next = temp;

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •