Advertisement

05.15.2008 at 12:06AM PDT, ID: 23404167
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Linked list help!

Tags: c
Hi, i was thinking of creating a linked list like the one in the example. however, to add a node u will always need to pass the list returned from linked_list_new(). is there anyway to modify it ?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
//linked list struct
struct linked_list
{
    struct node *head;
    int count;
};
 
//node struct
struct node
{
    int data;
    struct node *next;
};
 
// allocates a new list, inits object
struct linked_list* linked_list_new()
{
    struct linked_list* newList;
    newList = malloc(sizeof(struct linked_list));
 
    newList->count = 0;
    newList->head = NULL;
 
    return newList;
}
// adds in FIFO fashion
void linked_list_add( struct linked_list* list, int data)
{
    struct node* cur = list->head;
 
    if (cur == NULL)
    {
        cur = malloc(sizeof(struct node));
        cur->data = data;
        cur->next = NULL;
       
        list->head = cur;
 
        return;
    }
 
    while(cur)
    {
        if(cur->next == NULL)
        {
            struct node* newNode;
            newNode = malloc(sizeof(struct node));
            newNode->data = data;
            newNode->next = NULL;
 
            cur->next = newNode;
            list->count ++;
        }
        else
        {
            cur = cur->next;
        }
    }
 
}
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: pothios
Solution Provided By: sunnycoder
Participating Experts: 3
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
05.15.2008 at 12:10AM PDT, ID: 21571187

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:12AM PDT, ID: 21571194

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:14AM PDT, ID: 21571201

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:17AM PDT, ID: 21571220

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:19AM PDT, ID: 21571237

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:20AM PDT, ID: 21571238

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:21AM PDT, ID: 21571242

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:27AM PDT, ID: 21571274

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:32AM PDT, ID: 21571297

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:35AM PDT, ID: 21571311

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:38AM PDT, ID: 21571318

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:39AM PDT, ID: 21571327

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:39AM PDT, ID: 21571328

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:44AM PDT, ID: 21571339

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 12:55AM PDT, ID: 21571387

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.15.2008 at 12:10AM PDT, ID: 21571187

Rank: Sage

>> is there anyway to modify it ?

What do you mean ? How do you want to modify it ?

linked_list_new creates a new node - how do you want to insert a node if you don't create it ?
 
05.15.2008 at 12:12AM PDT, ID: 21571194

Rank: Sage

Ah, sorry, I misinterpreted your code.

linked_list_new creates a new (empty) list. Why do you need to create a new list when you want to add a node ?

A node should be added to an existing list (which should be passed as the first parameter to the add function).
 
05.15.2008 at 12:14AM PDT, ID: 21571201

Rank: Sage

You can make the linked_list variable a global but then
1. You will have to maintain a global for every linked list that you wish to create
2. You will end up with more globals - namespace pollution etc.

Is there a specific reason why you wish to change it? I would recommend sticking with it ... This is proper uise of functions and would get you comfortable with pointer parameter passing over a period of time.
Accepted Solution
 
05.15.2008 at 12:17AM PDT, ID: 21571220
so if i have 3 linked list, i nid to pass them all into one function that makes use of all 3? :o
 
05.15.2008 at 12:19AM PDT, ID: 21571237

Rank: Sage

No ... you make another function call
 
05.15.2008 at 12:20AM PDT, ID: 21571238
you can mdify the signature to
struct linked_list* linked_list_new( node* Behind /*= NULL*/, node* Ahead /*= NULL*/)

and inside it :
1. allocate the memory for one 'node' and not the list.
2. wire this new node onto desired place.
        a. if behind node* is given, then you need to add this new node as the next node to the 'behind' node.
        b. if Ahead node is provided, then you need to add 'Ahead' node as next node to this new node.
dont put
if a
else if b

put
if a
if b
becoz both conditions can coexist.
 
05.15.2008 at 12:21AM PDT, ID: 21571242

Rank: Sage

linked_list_add( list1, 5);
linked_list_add( list2, 15);
linked_list_add( list3, 101);
 
05.15.2008 at 12:27AM PDT, ID: 21571274
i think my question is misinterpretated.

lets say i have 3 linked list and i create it when program starts
int main () {
   struct list1 *one;
   struct list2 *two;
   struct list3 *three;
   one = linked_list1_new();
   two= linked_list2_new();
   three= linked_list3_new();
   generate_data(one, two, three, string[]);  
}
 
05.15.2008 at 12:32AM PDT, ID: 21571297

Rank: Sage

Assuming from name that generate_data is supposed to generate data, there is no need to pass the lists to it. If you were to add another list, why would it affect the data generator?

Also assuming that string is a parameter used to generate data and same data would be added to all 3 lists, your calling code should look like this

int data = generate_data(my_string);
linked_list_add( one, data);
linked_list_add( two, data);
linked_list_add( three, data);

An even more elegant way would be to keep all three lists in another list/array and iterate over them to add the data.
 
05.15.2008 at 12:35AM PDT, ID: 21571311

Rank: Sage

What does generate_data do ?
 
05.15.2008 at 12:38AM PDT, ID: 21571318
well because i have to do some processing on the string, then assign them to different linked list for different conditions. dats why :(
 
05.15.2008 at 12:39AM PDT, ID: 21571327

Rank: Sage

Can you provide more details? It should be possible to isolate data processing from details of data storage.
 
05.15.2008 at 12:39AM PDT, ID: 21571328
generate data reads a line of text and put them in their respective linked list.

e.g employee xx xxx goes into employee list
       user xx xx goes into user list
      and so on
 
05.15.2008 at 12:44AM PDT, ID: 21571339

Rank: Sage

generate_data can return a struct employee that has values filled into fields
your add_data will accept pointer to struct employee and all lists. This function will add data to respective lists. If you were to add fields, then it would affect both generate_data and add_data. If you were to add only a list, it would affect only add_data.
If you need to add different data to different lists then either you need to make separate calls to same add_data function (the approach I recommended earlier) or you need to have a function that accepts multiple lists as arguments and adds data to appropriate lists. Which one to use depends on your requirements.
 
05.15.2008 at 12:55AM PDT, ID: 21571387

Rank: Sage

>> generate data reads a line of text and put them in their respective linked list.

Something like this maybe ?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
typedef enum {
  LL_EMPLOYEE = 0,
  LL_USER = 1,
  LL_MANAGER = 2
} LL_Type;
 
void add_data(struct linked_list lists[3], const char *str) {
  LL_Type listid = /* determine list type based on str */;
  int data = /* get data from str */;
  linked_list_add(&(lists[listid]), data);
}
 
 
/* and then : */
 
struct linked_list lists[3] = { 0 };
char str[128] = "";
while (/* read line into str */) {
  add_data(lists, str);
}
Open in New Window
Assisted Solution
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628