Find the nth Element from last in a linked list.

/*
Find the nth Element from last in a linked list.
*/

#include

using namespace std;

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

struct node* createlist()
{
    node *nodeHolder,*pointerToTheFirstNode, *temp;

    int elementCount=0;
    pointerToTheFirstNode = (node*)malloc(sizeof(node));
    temp=pointerToTheFirstNode;
    cout<<"Enter the number of elements you want in the list:    ";
    cin>>elementCount;
    cout<<endl;

    while(elementCount!=0)
    {
    temp->next=(node*)malloc(sizeof(node));
    cout<<"Enter the elements for the nodes:    ";
    cin>>temp->data;
    nodeHolder=temp;   
    temp=temp->next;
    elementCount–;
    }
    nodeHolder->next =NULL;
    free(temp);
    return pointerToTheFirstNode;
}

void display(struct node *headerPointer)
{

    node *ptr;
    ptr = headerPointer;
    cout<<endl<<"The list that we just created is:    ";
    while(ptr!=NULL)
    {
        cout<data<<" ";
        ptr=ptr->next;
    }
    cout<<endl;
}

int lenlist(struct node *header)
{
    node *ptr;
    int len=0;
    ptr=header;
    while(ptr!=NULL)
    {
        len=len+1;
        ptr=ptr->next;
    };
    return len;
}

void findElementFromLast(struct node *header, int intPos)
{
    node *ptr;
    ptr=header;
    int matchPos=0;
    int listLen = lenlist(ptr);
    cout<<"The length of the list is:    "<<listLen<<endl;
    cout<<"The element that is at "<< intPos <<" from the last node will be at "<<listLen-intPos<< " from the first node."<<endl;

    while(ptr!=NULL)
    {
        matchPos=matchPos+1;
        if(matchPos ==     (listLen-intPos))
        {
            cout<<"The value of the node that is at "<< intPos <<"th position from last is: "<data;
        }
        ptr=ptr->next    ;
    }
}

void main()
{
    node *header;
    int intPosFromLast=0;

    header=createlist();
    display(header);

    cout<<"Enter the position element that you want to find in the list we just created:    ";
    cin>>intPosFromLast;
    findElementFromLast(header,intPosFromLast);
}

Advertisements

Find the nth Element in a linked list.

/*
Find the nth Element in a linked list.
*/

#include

using namespace std;

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

struct node* createlist()
{
    node *nodeHolder,*pointerToTheFirstNode, *temp;

    int elementCount=0;
    pointerToTheFirstNode = (node*)malloc(sizeof(node));
    temp=pointerToTheFirstNode;
    cout<<"Enter the number of elements you want in the list:    ";
    cin>>elementCount;
    cout<<endl;

    while(elementCount!=0)
    {
    temp->next=(node*)malloc(sizeof(node));
    cout<<"Enter the elements for the nodes:    ";
    cin>>temp->data;
    nodeHolder=temp;   
    temp=temp->next;
    elementCount–;
    }
    nodeHolder->next =NULL;
    free(temp);
    return pointerToTheFirstNode;
}

void display(struct node *headerPointer)
{

    node *ptr;
    ptr = headerPointer;
    while(ptr!=NULL)
    {
        cout<data<<" ";
        ptr=ptr->next;
    }
    cout<<endl;
}

void findElement(struct node *header, int intPos)
{
    node *ptr;
    int matchPos=0;
    ptr=header;
    while(ptr!=NULL)
    {
        matchPos=matchPos+1;
        if(matchPos == intPos)
        {
            cout<<"The value at the node at position "<< intPos << " is: "<data;
        }
        ptr=ptr->next    ;
    }
}

void main()
{
    node *header;
    int intPos=0;

    header=createlist();
    display(header);

    cout<<"Enter the position element that you want to find in the list we just created:    ";
    cin>>intPos;
    findElement(header,intPos);
}

Write a function to reverse a linked list.

/*
    Write a function to reverse a linked list. No double pointers please.
*/

#include

using namespace std;

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

struct node* createlist()
{
    int counter=0;
    node *first, *temp, *last;
    first = (node*)malloc(sizeof(node));
    temp=first;
    cin>>temp->data;
    cout<<"Please enter the number of elements in the list that you want:    ";
    cin>>counter;
    while(counter!=0)
    {
    temp->next=(node*)malloc(sizeof(node));
    cout<<"Enter node data:    ";
    cin>>temp->data;
    last=temp;
    temp=temp->next;
    counter–;
    }
    last->next =NULL;
    delete(temp);
    return first;
}

void display(struct node *header)
{
    node *ptr;
    ptr=header;
    cout<<"The list now looks like:    ";
    while(ptr!=NULL)
    {
        cout<data<<"    ";
        ptr=ptr->next;
    }
    cout<<endl;
}

int lenlist(struct node *header)
{
    node *ptr;
    int len=0;
    ptr=header;
    while(ptr->next!=NULL)
    {
        len=len+1;
        ptr=ptr->next;
    };
    return len;
}
struct node* reverselist(struct node *header)
{
    int counter=5, tempHolder=0;
    node *temp, *ptr,*prev, *last;
    prev=NULL;
    ptr=header;
    //Obtain the length of the linked list
    counter=lenlist(header);
    while(counter!=0)
    {
        while(ptr->next!=NULL && ptr!=last)
        {
            if(ptr!=NULL)
            {
                tempHolder=ptr->data;
                ptr->data=(ptr->next)->data;
                (ptr->next)->data=tempHolder;
                prev=ptr;
            }
            else
            {
                cout<<"IF YOU SEE THIS, THE CODE IS FAULTY. The value of ptr is NULL for counter value "<<counter<<endl;
            }
            ptr=ptr->next;
        };
        ptr=header;
        last=prev;
        counter–;
    }
    return header;       
}

void main()
{
    node *header, *latest;
    node *header1;
    header=createlist();
    display(header);
    latest=reverselist(header);   
    display(latest);
}

Linked list problems

/*
You have two “set” of numbers represented by “two” linked lists, where each node contains a single digit.
Write a function that adds the two numbers “in the corresponding nodes” and returns the sum as a “third” linked list

*/

#include
using namespace std;

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

void display(struct node *header)
{
    cout<<endl;
    node *ptr;
    ptr=header;
    cout<<"The list is:    ";
    while(ptr!=NULL)
    {
        cout<data<<"    ";
        ptr=ptr->next;
    }
}

struct node* createlist()
{
    int counter=0;
    node *first, *last, *temp;
    first= (node*)malloc(sizeof(node));
    temp=first;

    cout<<"Enter the number of elements:    ";
    cin>>counter;
    while(counter!=0)
    {
        temp->next=(node*)malloc(sizeof(node));
        cout<<"The node data is:    ";
        cin>>temp->data;
        last=temp;
        temp=temp->next;       
        counter–;
    }
        last->next=NULL;
        return first;
}

struct node* createlist(node *pointer3,int nodevalue)
{
    node *ptr;
    ptr=pointer3;
    if(ptr==NULL)
    {
        node *temp;
        temp=(node*)malloc(sizeof(node));
        temp->data=nodevalue;
        return temp;
    }
    else
    {
        node *last;
        last=pointer3;
        pointer3->next=(node*)malloc(sizeof(node));
        pointer3=pointer3->next;
        pointer3->data=nodevalue;
        return pointer3;
    }   
}

struct node* addlinkedlist(struct node *header1, struct node *header2)
{
    cout<<endl;
    node *firstlist,*secondlist, *pointer3, *baseptr;

    firstlist=header1;
    secondlist=header2;
    pointer3 = NULL;   

    while(firstlist !=NULL && secondlist !=NULL)
    {

        if(pointer3==NULL)
        {
            baseptr = createlist(pointer3,firstlist->data    + secondlist->data);
            pointer3=baseptr;
        }
        else   
        {
            pointer3 = createlist(pointer3,firstlist->data    + secondlist->data);           
        }

        secondlist=secondlist->next;
        firstlist= firstlist->next;
    }
    return baseptr;
}

void main()
{
    cout<<"You have two set of numbers represented by two linked lists, where each node contains a single digit.";
    cout<<"Write a function that adds the two numbers in the corresponding nodes and returns the sum as a third linked list";
    node *header1, *header2, *header3;
    header1 = createlist();
    header2=createlist();
    display(header1);
    display(header2);
    header3= addlinkedlist(header1, header2);
    display(header3);   
}