473,662 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

auxiliary linked list index array

hello, can someone please help...
Im interested in the use of an auxiliary
array to act like pointers for another
array, allowing the sort order to be
traversed. See code below.

I dont have an application in mind,
its more academic at this stage - but not
a homework question!

I have tried to avoid the use of a separate
variable to point to the head of the list by
using the 0th element of the indexing array.
This doesnt always work as show with array b[]

Im looking for suggestions on arranging x[] so
that it can always be used without the use of
an extra variable. Im convinced it can be done
simply because there is a 'spare' slot if you
look at x[] and y[].

Thanks for any help given...

Hal
/*----------------------------------------------------------*/
/*linked list in an array*/
#include <stdio.h>
#define nl() putchar('\n')
#define SMAX 6
/*----------------------------------------------------------*/
int main()
{
/* 0, 1, 2, 3, 4, 5 */
char a[]= {'v','n','f','j ','r','b'};
int w[]= {-1, 4, 3, 1, 0, 2}; /* -1 means end of list*/
int f=5; /*head of list*/
/* 0, 1, 2, 3, 4, 5 */
int x[]= { 5, 4, 3, 1, 0, 2}; /*head is x[0]*/

/* 0, 1, 2, 3, 4, 5 */
char b[]= {'L','H','X','P ','D','T'};
int y[]= { 3, 0, -1, 5, 1, 2};
int e=4; /*head of list*/
/*
b[] cannot be indexed using a representation
like x[] because the index element zero needs
to be 4 at the start and 3 later on:-
int z[]= { 4 then 3, 0, -1, 5, 1, 2};
*/
int i,p;

nl();
for(i=0,p=f; i<SMAX; i++,p=w[p])
printf("%d %c\n", p, a[p]);
nl();
for(i=0,p=x[0]; i<SMAX; i++,p=x[p])
printf("%d %c\n", p, a[p]);
nl();
for(i=0,p=e; i<SMAX; i++,p=y[p])
printf("%d %c\n", p, b[p]);
nl();
return 0;
}
Nov 14 '05 #1
1 2057
"Hal Styli" <no_spam@all> wrote:
I have tried to avoid the use of a separate
variable to point to the head of the list by
using the 0th element of the indexing array.
This doesnt always work as show with array b[]
You cannot do that. There isn't room.
Im looking for suggestions on arranging x[] so
that it can always be used without the use of
an extra variable. Im convinced it can be done
simply because there is a 'spare' slot if you
look at x[] and y[].
The problem is _finding_ that spare slot.
/* 0, 1, 2, 3, 4, 5 */
char a[]= {'v','n','f','j ','r','b'};
int w[]= {-1, 4, 3, 1, 0, 2}; /* -1 means end of list*/
int f=5; /*head of list*/
/* 0, 1, 2, 3, 4, 5 */
int x[]= { 5, 4, 3, 1, 0, 2}; /*head is x[0]*/

/* 0, 1, 2, 3, 4, 5 */
char b[]= {'L','H','X','P ','D','T'};
int y[]= { 3, 0, -1, 5, 1, 2};
int e=4; /*head of list*/
/*
b[] cannot be indexed using a representation
like x[] because the index element zero needs
to be 4 at the start and 3 later on:-
int z[]= { 4 then 3, 0, -1, 5, 1, 2};
Exactly. You need to remember _two_ pieces of information: where the
logical start of the array is, and where the logical successor of
element 0 is. You cannot fit these two integers into one integer's
memory space, clearly.
It seems to work in the case of a[] because in that case the value of
these two bits of information just happens to be the same, but obviously
you cannot rely on that at all.
nl();


Bletch. What's wrong with plain putchar('\n'); or even puts("");?

Why don't you use a normal index array? Like this:

char b[]= {'L','H','X','P ','D','T'};
int bi[]= { 4, 1, 0, 3, 5, 2};

for (i=0; i<SMAX; i++)
printf("%d %d %c\n", i, bi[i], b[bi[i]]);

Richard
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7950
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value' => 'aaa') , array( 'id' => 'B', 'parent_id' => 'A', 'value' => 'bbb') , array( 'id' => 'C', 'parent_id' => 'B', 'value' => 'ccc') , array( 'id' => 'D', 'parent_id' => 'A', 'value' => 'ddd')
10
15120
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
17
1817
by: darrell.blake | last post by:
I've just written a doubly linked list but when I tell the iterator to move forward in the list it removes all the previous elements in the list. i.e. If I were to do: List list; list.AddAtEnd(20); list.AddAtEnd(30); list.AddAtEnd(40); list.AddAtBeginning(10);
9
4040
by: Daniel Vukadinovic | last post by:
I want to implement an index/counter to my linked list. Why? I wrote a search function which searches the list for elements based on their values (say I add an element and I assign the value 54 and the next element 789 and so on...) now I'd like to write a search function which would search the list for elements based on their index. Say first element is 54 and index 1, second element is 789 and index 2 so when I want to search, I'll do...
12
3943
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
6
6953
by: Nick Valeontis | last post by:
I know how to use Icomparable. However, I can't figure out how to sort a generic linked list? (without writing the algorithm) Lets say I have something like this: class DisJunctiveConstraintList : LinkedList<DisjunctiveConstraint>
9
4899
by: Sheldon | last post by:
Hi, I am trying to understand linked lists and the different ways to write a linked list and double linked list. I have been trying to get this function called insert_word to work but to no avail. The function receives a string from main and then inserts in the list the new word in alphabetical order. Here is my function: struct word { // double linked list. Here the list is global and is first built
5
9706
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. To represent and store these matrices, I'd like to implement the sparse matrices as a doubly-linked list, in which each non-zero cell is stored roughly as follows: int rownum int colnum
8
718
by: dmp | last post by:
What are Linked list? Please somebody show some ready made programs of linked list
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.