Link to home
Start Free TrialLog in
Avatar of Leo79
Leo79

asked on

Example Linked List??

Hi,
I am looking for some example linked lists. Is there any good ones about?
I'd like to see one that works in
co-ordination with a stack. Also one that canbe ordered on the basis of the users inputs, i.e the user can assign an ID each node to state which is next in the list?..
Thanks
Avatar of stochastic
stochastic
Flag of India image

Leo79,

I think you'll need to amplify your question. Do you want a practical example of using linked lists? Or do you want code that will support linked list creation with user input of IDs?

And when you say 'co-ordination with a stack', what do you exactly mean? If you specify exactly what you need, I think you will get a lot of responses.

- stochastic
Avatar of vijay_a73
vijay_a73

Leo79,
  Your question is answerable only in parts. ie., when the linked lit is modelled as a stack we cannot assign id to the nodes and order them on some arbitrary merit. Remember a stack is a LIFO data structure.
  On the other hand we can design a linked list ordering the nodes on the basis of an id.
  Which one do you want?
-  vijay_a73
    Hi there Le079. I have here a code involving linked lists, with variable number of nodes, depending on user inputs. All you have to do is to compile this one (I used Visual Studio ver. 6.0), and run it to see the result. Here is the complete code.

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


typedef struct s_alm_dat AlmDat, *AlmDatPtr;
struct s_alm_dat {
      char Name[30];
      char Surname[30];
      int Age;
      char Status[2];
      AlmDatPtr next;
      AlmDatPtr Single_next;
};

AlmDatPtr headPtr, tailPtr;
AlmDatPtr SinglePtr, Single_tailptr;

void Input_Data();
void Display_Data();
void Display_Single_Only();
void Get_Single();



void main()
{
      Input_Data();
      Get_Single();
      Display_Data();
      Display_Single_Only();
}



void Input_Data()
{
      char name[30];
      char surname[30];
      char age[4];
      int age2;
      int cmp;
      char status[2];
      AlmDatPtr nodePtr;

      while(1)
      {
            printf("\n\nPlease enter name: ");
            gets(name);
            cmp = strcmp(name, "X");
            if(cmp==0)
                  break;

            printf("Please enter surname: ");
            gets(surname);
            printf("Please enter age : ");
            gets(age);
            age2 = atoi(age);
            printf("Please enter status : ");
            gets(status);

            nodePtr = (AlmDatPtr)malloc(sizeof(AlmDat));
            if (nodePtr == NULL){
                  printf("Error in allocation\n");
            }
            strcpy(nodePtr->Name, name);
            strcpy(nodePtr->Surname, surname);
            strcpy(nodePtr->Status, status);
            nodePtr->Age = age2;
            nodePtr->next = NULL;
            nodePtr->Single_next = NULL;

            if(headPtr == NULL)
            {
                  headPtr = nodePtr;
                  tailPtr = nodePtr; /* copy of the headPtr (from tail side) */
                                     /* head and tail both starts frm same address */
            }
            else
            {
                  tailPtr->next = nodePtr; /* by advancing tail, the head also advances */
                  tailPtr = nodePtr;  /* set the value of tail to the latest value again */
            }
      }
}



void Display_Data()
{
    AlmDatPtr workingPtr;
      int x;

      for(x=0; x<6; x++)
            printf("\n");

    workingPtr = headPtr;
    printf("\t\tName\t\tSurname\t\tAge\t\tStatus\n");
      printf("\t\t------------------------------------------------------\n");
    while(workingPtr != NULL){
        printf("\t\t%s\t\t%s\t\t%d\t\t%s\n", workingPtr->Name,
                      workingPtr->Surname,workingPtr->Age,workingPtr->Status);
        workingPtr = workingPtr->next;  
      }
      printf("\n\n");
}

void Display_Single_Only()
{
      AlmDatPtr workingPtr;

      workingPtr = SinglePtr;
      printf("\t\tName\t\tSurname\t\tAge\t\tStatus\n");
      printf("\t\t------------------------------------------------------\n");
    while(workingPtr != NULL){
            printf("\t\t%s\t\t%s\t\t%d\t\t%s\n", workingPtr->Name,
                      workingPtr->Surname,workingPtr->Age,workingPtr->Status);
        workingPtr = workingPtr->Single_next;  
      }
      printf("\n\n");
}



void Get_Single()
{
      AlmDatPtr workingPtr;

      workingPtr = headPtr;
      while(workingPtr != NULL)
      {
            if(workingPtr->Status[0]=='S')
            {
                  if(SinglePtr==NULL)
                  {
                        SinglePtr = workingPtr;
                        Single_tailptr = workingPtr;
                  }
                  else
                  {
                        Single_tailptr->Single_next = workingPtr;
                        Single_tailptr = workingPtr;
                  }
            }
            workingPtr = workingPtr->next;
      }
}



NOTE:
        When the program prompts for Status, you should input either S for single or M for married. Only these two inputs are accepted. And if you would want to exit the program, just type X when the prompt "Please enter name:" appears.
        Two sets of displays will appear afterwards, the first shows all the nodes of the linked list produced, while the second one shows the nodes whose status is S (for single) only.
        I hope this could help you. The routine which checks all nodes containing S status is similar to redirecting the tail of linked lists anywhere while traversing all nodes. So if you want you can modify this routine to fit your needs. Good luck and happy studying,....


euy



     
Avatar of Leo79

ASKER

Hi again,

I am looking for a linked list that will be linked on the basis of an ID.
It doesn't have to sort the list. I mean i don't want it to be order alphabetically or numerically.
The next node will be the one specificied order by the <<linked to>> field.

Here's the structure i have it might make it a bit clearer.

struct links{
char ID[];
int input1;
int input2;
int result;
char linkedto[];
};

.. Also what i wanted to do was to added the numbers in Input1 and Input2. Assign the result to Result and push it to the next node using a stack.
Any examples similiar to this ??
Anything to do with stacks will be a help.

Thanks

Avatar of Leo79

ASKER

Hi again,

I am looking for a linked list that will be linked on the basis of an ID.
It doesn't have to sort the list. I mean i don't want it to be order alphabetically or numerically.
The next node will be the one specificied order by the <<linked to>> field.

Here's the structure i have it might make it a bit clearer.

struct links{
char ID[];
int input1;
int input2;
int result;
char linkedto[];
};

... Also what i wanted to do was to added the numbers in Input1 and Input2. Assign the result to Result and push it to the next node using a stack.
Any examples similiar to this ??
Anything to do with stacks will be a help.

Thanks



Are the ID fields always unique, or do the values repeat frequently?
Also, does the linkedto field contain the same values as the ID field?

When you say 'stack', that implies that not only do you want to add structures to the 'stack', but you also need to remove them too?  Would a dynamic array of structures do the job?
Avatar of Leo79

ASKER

The ID fields are always unique.
And the Linkedto field contains the same
values as those in the ID field.

The idea i had of the stack was to pass the output from one structure , as an input to the next structure. A dynamic array might help , but how would i get the output from one structure , to the next structure??
The reason I mentioned the array was since your linkto field does not actually point* to the next structure in the tree, you will have to search all of the records to find it.

The bigger your database becomes the longer it will take to add new records.  One of the advantages of using a linked list is that you don't need to traverse the entire database in order to add/delete a new record.

May I suggest:

struct links{
                    char ID[MAXIDLEN];  // need fixed size here!
                    int input1;
                    int input2;
                    int result;
                    struct links *pParent;  // do you need this?
                    struct links *pNext;     // pointer to next node
                    };

Then, keep an index of names and pointers, so that the user can select a name from a list, and you add the associated pointer to the linked list.

I believe you said this was a digital logic simulator, right?  So, you will be doing a time vs. signal propagation through the tree, right?  You might want to add a field in the links structure for propagation time, since this varies.  How do you handle a single input going to multiple gates?  Or what if the gates have different types of inputs, how do you know which input is which?

Food for thought:  your database is built around representing the various gates.  Perhaps it should represent the connection nodes instead, where a gate is a collection of connections and the relationships that define them.
ASKER CERTIFIED SOLUTION
Avatar of Vinay
Vinay

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Leo79

ASKER

Hey Vinay,
Definately, that would be a great help.
My e-mail is : annamaria50@yahoo.co.uk
Can you send me all that you have.
Thanks