Computing.Net > Forums > Programming > Linked list

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Linked list

Reply to Message Icon

Name: macluvitch
Date: November 16, 2003 at 17:18:46 Pacific
OS: DOS
CPU/Ram: INTEL
Comment:

Hello guys

ok currently I'm strugling with a program (A simple data base) So I think I need to use linked list In my program to solve the problem ( Suggestion of a freind)

So I'm wodering how They can help me
And what's the utility of them



Sponsored Link
Ads by Google

Response Number 1
Name: borelli35
Date: November 17, 2003 at 00:25:41 Pacific
Reply:

See posting 7407

Yeah, I'd say this works pretty good...

borelli35


0

Response Number 2
Name: Gagey
Date: November 17, 2003 at 16:42:38 Pacific
Reply:

I'll just explain a bit about LinkedLists for u.

Basically they are similar to arrays, but:
They are NOT static in size and you can't index into them, only 'walk' the list (called traversing).

here's some basic C code for a linked list.
(Only single links here, u can use double links)

node = list->head ;#include <stdlib.h>
#include <iostream>

using namespace std ;

struct int_node {

int data ;
struct int_node *next ;
} ;

struct llist {

int count ;
struct int_node *head, *last ;
} ;

typedef struct int_node IntNode ;
typedef struct llist LinkList ;

LinkList * makeList() ;
void killList(LinkList *list) ;
void addValue(LinkList *list, int val) ;
void printList(LinkList *list) ;

int main() {

LinkList *list = makeList() ;
int i ;

for (i = 1 ; i <= 10 ; ++i)
addValue( list, i ) ;
printList( list ) ;
cout << list->count << endl ;

killList( list ) ;

return 0 ;
}

LinkList *makeList() {

LinkList *new_list = (LinkList*)malloc(sizeof(LinkList)) ;
new_list->count = 0 ;
new_list->head = NULL ;
new_list->last = NULL ;

return new_list ;
}

void killList(LinkList *list) {

if (list->count == 0)
free(list) ;
else {
IntNode *node, *tmp ;
for (node = list->head ; node != (IntNode*)NULL ;) {
tmp = node ;
node = node->next ;
free(tmp) ;
}
free(list) ;
}
}

void addValue(LinkList *list, int val) {

IntNode *node = (IntNode*)malloc(sizeof(IntNode)) ;
node->data = val ;
node->next = NULL ;

if ( list->count++ == 0 )
list->head = list->last = node ;
else {
list->last->next = node ;
list->last = node ;
}
}

void printList(LinkList *list) {

IntNode *node ;
for (node = list->head ; node != (IntNode*)NULL ; node = node->next)
cout << node->data << " " ;
cout << endl ;
}

There is HEAPS of info on this and other data structures on the Web so I won't explain this any further.


0

Response Number 3
Name: Gagey
Date: November 17, 2003 at 16:44:14 Pacific
Reply:

Sorry:

in line reading

node = list->head ;#include <stdlib.h>

ignode the node = list->head ; part, its a mistake!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


C ++ BUBBLE sort in array of S...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Linked list

Linked List de-allocation of pointe www.computing.net/answers/programming/linked-list-deallocation-of-pointe/12614.html

Linked List Help!!! www.computing.net/answers/programming/linked-list-help/6453.html

linked list in c www.computing.net/answers/programming/linked-list-in-c/7408.html