Creating Linked List Using Hashing and Stack

This is a discussion on Creating Linked List Using Hashing and Stack within the C Programming forums, part of the General Programming Boards category; Hi All, For a while I've been stuck on something and I couldn't get out of it, Simply, I'm trying ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    28

    Unhappy Creating Linked List Using Hashing and Stack

    Hi All,

    For a while I've been stuck on something and I couldn't get out of it,
    Simply, I'm trying to create and linked list while using "stack" method, and as I'm doing this, I must
    use Hashing algorithm, I'm so alien to linked list, after I worked on them I understood the logic of
    it, but still Im having many problems.

    First Of All, while doing hashing in linked list, I have to declare an Array and Each item in
    the array is a pointer to the linked list (stack) of the input(ID,number..) mapped to that index.

    According to my understandings I've created this kind of algorithm but first I ve tried to do it
    without using any hashing, I tried to do simple stack program, now I want to add hashing function
    into this, however I got confused, everything got confused...

    Here is my program, what do you think thet where I'm wrong at, and where I'm supposed to work on or change,

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define LENGTH 10
    
    typedef struct student {
    int id;
    struct student* next;
    char name[10];
    char surname[10];
    }students;
    
    void push( students** head, int key,char name[30],char surname[30] );
    int menu(); // I gotta pass something after created hash and array
    void insert(); // I gotta pass something after created hash and array
    void search();// I gotta pass something after created hash and array
    void readfile();// I gotta pass something after created hash and array
    void writefile();// I gotta pass something after created hash and array
    
    int key;
    char name[30];
    char surname[30];
    students* head = NULL;
    students* current;
    students* current2;
    void main() {
    
    	typedef struct students_* student; // not used in program
    	student students_[LENGTH]; //not used in program
    
    	students* head = NULL;
    	int choice;
    	readfile(&head);
    	while(choice!=0){
    	choice=menu();
    	switch(choice){
    	case 0:
    		break;
    	case 1:
    		search(&head);
    		break;
    	case 2:
    		insert();
    		break;
    /*	case 3:
    		count();
    		break;*/
    	case 4:
    		list(&head);
    		break;
    	}
    }
    writefile(&head);
    
    }
    void push( students** head, int key,char name[30],char surname[30] ) {
    
    	students* new;
    	new = malloc(sizeof(students));
    	new->id = key;
    	strcpy(new->name,name);
    	strcpy(new->surname,surname);
    	new->next = *head;
    	*head = new;
    }
    int menu(){
    	int x;
    	printf("\nTo Exit Press\t0\nTo Search Press\t1\nTo Insert Press\t2\nTo Count Press\t3\nTo List Press\t4\n");
    		scanf("%d",&x);
    		clrscr();
    	return x;
    	}
    
    //------------------------------------------------------------------------
    void insert(){
    do {
    	printf("Enter The ID To Exit Press 0\n");
    	scanf("%d", &key);
    	if(key==0) 
    		break;
    	printf("Enter The Name\n");
    	scanf("%s",name);
    	printf("Enter The Surname\n");
    	scanf("%s",surname);
    	if ( key != 0 )
    	push( &head, key,name,surname);
    } while ( key != 0 );
    I must've done somethings wrong, because even doing a simple stack, everything goes wrong, because when I enter an ID and name, then I searched it, or list it, after listing or searching, if I want to list or search second time, it doesnt give anything, I 'm not sure but I think because of some reason program lost the head of stack. Maybe I declared something globally that's why it happens but I dont know

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have a local variable called 'head', and a global variable called 'head'. Which one are you trying to actually use? The function main is not a void function. It returns an int; read the FAQ on it. You last function listed is missing a }, but I'll assume it got missed in the copy-paste.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Actually I gotta use the head which is in main one, but while I'm trying to fix the program up, I must have put the second head as globally... As I said I got so confused ...
    And last } got missed in the copy paste as u said.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21