C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 4th, 2012, 12:23 AM
noobDev noobDev is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 1 noobDev User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 17 sec
Reputation Power: 0
Array-based linked list

I need help figuring out the algorithm to insert into an array of structs. I'm not allowed to use pointer since next will be pointing to the next node.

Code:
struct Inode { int item;int next; }; 

Inode node[10];

So, I have an array of 10 struct.
I initialize all the next to 1 to 9, with the last next to -1.

Code:
ptr = free; free = node[free].next; 
 if (head == -1) { node[ptr].item = newItem; 
node[ptr].next = -1; head = 0;}

That was for the first insertion.
so, I insert it into a free node, and set next to -1 to indicate that it is the end of the list.

I'm having trouble with inserting the 2nd item.

I need to change the next of the previous node to 1 instead of -1, and i'll be setting the next value to -1 on the 2nd node.

Basically, I would want to linked 'item' in a sorted order.

Reply With Quote
  #2  
Old October 4th, 2012, 02:01 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Please, please, please do not use free for your own identifier.
It is a very used function from the Standard library and seeing it with a different meaning is very troubling and makes following the code very very hard.

Reply With Quote
  #3  
Old October 4th, 2012, 12:12 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
Why would you start off with all the "next"'s connected?
I'd start with

node[0].next = -1;
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #4  
Old October 5th, 2012, 07:18 AM
TheTechguys TheTechguys is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 TheTechguys User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 49 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Want to become a programmer C + +? EBook Cprogramming.com, jumped to C + +, will walk you through it, step by step. Get a jump in C + + today! Learning from sample code written by other programmers

C + C source code + - complete software
C + + C source code - model scrapbook

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Array-based linked list

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap