GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Aug-2005, 23:55
zidanefreak01 zidanefreak01 is offline
Invalid Email Address
 
Join Date: Jun 2005
Posts: 12
zidanefreak01 is on a distinguished road

qeues, stacks, lists


i have a project right now.. and i don't know how to do it.. it says that i will qeues, stacks, list, or whatever it is...

but here is a little bit of the code..


CPP / C++ / C Code:
cout<<"M E N U";
cout<<"[a] add a new student"; // adds a new student to the database
cout<<"[d] delete student"; // deletes a student
cout<<"[e] edit student"; // edits the students information
cout<<"[v] view student"; // view students information

CPP / C++ / C Code:
// if [a] is the choice
cout<<"enter the following info's"
cout<<"name: ";
cin>> // what should i exactly put in here
cout<<"address: ";
cin>> // same as the above
cout<<"scores: ";
cin>> // same
cout<<"age: ";
cin>>same;

// if [d],[e] or [v] is the choice
cout<<"enter student's name: ";

question:

1. how can i make a database using stack, list, or qeues??

pls. help me anyone... thanks...
Last edited by LuciWiz : 02-Aug-2005 at 01:18. Reason: Changed title
  #2  
Old 02-Aug-2005, 00:18
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
You haven't given enough information, and your questions are too general for me to form a good answer. Perhaps you should research the assignment a little better, then come up with questions regarding specific problems so that we can help you to the best of our abilities

What I can help you on is the cin statements. You need to store the data from the keyboard into a variable of some type. If you're storing a name, you should use a string type for the variable used with cin. A proper cin statement might look like this in your program:

CPP / C++ / C Code:
char name[50] = "";

cout << "Enter a name: ";
cin  >> name;
Of course, there's plenty of error checking to happen in between statments to ensure that the name is properly stored, but as long as you understand how cin works, you should be alright. As far as stacks and queues go, you will have to know what they are, how to use them, and what you are using them for in your assignment. I can't help you implement them unless I understand what you need them for. Good luck with your assignment!
__________________
-Aaron
  #3  
Old 02-Aug-2005, 00:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,372
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by zidanefreak01
i have a project right now.. and i don't know how to do it.. it says that i will qeues, stacks, list, or whatever it is...

1. how can i make a database using stack, list, or qeues??

pls. help me anyone... thanks...
So, what is a stack? a list? a queue?
If you must use one of these, your instructor obviously explained them.

Use that explanation in your description of what you want to accomplish. You must have some ideas.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #4  
Old 02-Aug-2005, 03:45
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

My suggestion whould be to implement a stack by list.


Quote:
Originally Posted by WaltP
So, what is a stack? a list? a queue?

try this :
CPP / C++ / C Code:
struct snode
{
   char *student_name;
   char *student_address;
   char *student_ID;
   struct snode *next;
};
typedef struct my_stack
{
   struct snode *head;
   struct snode *tail;
} stack;

Try to continue from here on your own.
Don't forget to allocate memory for char* type variables ...
  #5  
Old 03-Aug-2005, 12:21
05gtp 05gtp is offline
Awaiting Email Confirmation
 
Join Date: Aug 2005
Location: Clinton Township (Detroit Area), MI
Posts: 13
05gtp will become famous soon enough
I'm new here, so correct me if I am wrong (I'm sure you will).

Stacks, Queues, Trees, are all forms of Linked Lists, including a Linear Linked List and Circular Linked List. What makes them different is how the data is structurally linked and accessible within the list. A Stack is a linked list where the last node in, is the first node out (LIFO), similar to a stack of dishes (dinner plates). A Queue, is similar to standing in line at the DMV, first node in, first node out, or pop (FIFO). A Tree, I'm not very good with this one, but the nodes branch out from a root node, like an inverse tree and each child node is known as a leaf of the list. A linear linked list is my favorite, because it is the easiest for me to manage. Its a linear list of nodes. It has a head node and a tail node, and each node has a next pointer. The node's next pointer points to the next node, unless it the last (tail) node, which the next pointer is assigned to point to NULL. Within a linear linked list, any node is accessible, deleted or inserted. I believe, same for the next type, Circular Linked List. A Circular Linked List, is the same as a linear linked list, except the tail node's next pointer, points back to the head node. All linked lists are made from the use of "structures" and pointers. Usually with the use of next and previous pointers included as structure data members. Hope this helps explain what linked lists are.
  #6  
Old 08-Aug-2005, 08:59
zidanefreak01 zidanefreak01 is offline
Invalid Email Address
 
Join Date: Jun 2005
Posts: 12
zidanefreak01 is on a distinguished road
to the guy who gave me a bit of code.. can u please do the whole var declaration?? and i'll do the rest.. thanks in advance.. 8-) 8-)
  #7  
Old 09-Aug-2005, 16:48
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

I must be crazy


;-)
Quote:
Originally Posted by zidanefreak01
to the guy who gave me a bit of code.. can u please do the whole var declaration?? and i'll do the rest.. thanks in advance.. 8-) 8-)

Can't really sleep, so I wrote you some code to give you a direction.
I intentionally didn't write you the third function. Do it your self and post it here if there are any problems.

CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct snode
{
   char *student_name;
   char *student_address;
   char *student_ID;
   struct snode *next;
   struct snode *pre;
};

typedef struct my_stack
{
   struct snode *head;
   struct snode *tail;
} my_stack;

//Init the stack.
void init_stack(my_stack *the_stack);

//Add a record to the stack.
int add_record(my_stack *the_stack,char *name,char *address,char *ID);

//Search a record in the stack. Return a pointer to the node, if exists.
//else return NULL.
struct snode search_record(char *ID);

//Print stack by order, to the standard output.
void print_stack(my_stack *the_stack);

void main()
{
	char user_choice = '\0';
	my_stack the_stack;
	init_stack(&the_stack);
	while (user_choice != 'q')
	{
		//Print Menu to the screen.
		printf("Menu\n");
		printf("----\n");
		printf("[a] add a new student.\n");
		printf("[d] delete student.\n");
		printf("[e] edit student.\n");
		printf("[v] view student.\n");
		printf("[q] quit program.\n");	//You don't suppose the user want to grow old with the application :-)
		user_choice = getchar();
	}
	
	//Example code for you. These are the appropriate ways of using the "add_record" and "print_stack" functions.
	add_record(&the_stack,"Kobi","Israel","1234567");
	add_record(&the_stack,"You","Must","Do your own homework");
	print_stack(&the_stack);
}

void init_stack(my_stack *the_stack)
{
	the_stack->head = NULL;
	the_stack->tail = NULL;
}

int add_record(my_stack *the_stack,char *name,char *address,char *ID)
{
	int result = 1;
	//First, create the new stack node.
	struct snode *new_snode;
	new_snode = malloc(sizeof(struct snode));
	if (new_snode == NULL)
	{
		printf("Memory allocation failed.\n");
		result = 0;
	}
	else
	{
		//Allocate memory for record data.
		new_snode->student_name = malloc(sizeof(strlen(name) + 1));
		new_snode->student_address = malloc(sizeof(strlen(address) + 1));
		new_snode->student_ID = malloc(sizeof(strlen(ID) + 1));
		if (new_snode->student_name == NULL || new_snode->student_address == NULL
			|| new_snode->student_ID == NULL)
		{
			printf("Memory allocation failed.\n");
			result = 0;
		}
		else
		{
			//Store data in the new node.
			strcpy(new_snode->student_name,name);
			strcpy(new_snode->student_address,address);
			strcpy(new_snode->student_ID,ID);
			//Check if the stack is empty.If so, add record as first record.
			if (the_stack->head == NULL)
			{
				//Insert at the head.
				new_snode->next = NULL;
				new_snode->pre = NULL;
				the_stack->head = new_snode;
				the_stack->tail = new_snode;
			}
			else
			{
				//Insert at the tail.
				new_snode->pre = the_stack->tail;
				new_snode->next = NULL;
				the_stack->tail->next = new_snode;
				the_stack->tail = new_snode;
			}
		}
	}
	return (result);
}

void print_stack(my_stack *the_stack)
{
	struct snode *iterator = the_stack->head;
	while (iterator != NULL)
	{
		printf ("%s\t%s\t%s\n",iterator->student_name,iterator->student_address,iterator->student_ID);
		iterator = iterator->next;
	}
	
}

Good luck, and I recommend you buy "The C programming language" (by Brian W. Kernighan & (&) Dennis M. Ritchie.

Kobi Hikri.
  #8  
Old 10-Aug-2005, 03:49
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Please notice :


I treated the insertion as a queue ! This is a mistake !
Fix this by always inserting new records at the head.

Kobi Hikri.
  #9  
Old 10-Aug-2005, 20:04
zidanefreak01 zidanefreak01 is offline
Invalid Email Address
 
Join Date: Jun 2005
Posts: 12
zidanefreak01 is on a distinguished road
here are the errors when i compiled it....

Compiling...
record.cpp
Code:
c:\program files\microsoft visual studio\myprojects\project1\record.cpp(68) : error C2440: '=' : cannot convert from 'void *' to 'struct snode *' Conversion from 'void*' to pointer to non-'void' requires an explicit cast c:\program files\microsoft visual studio\myprojects\project1\record.cpp(77) : error C2440: '=' : cannot convert from 'void *' to 'char *' Conversion from 'void*' to pointer to non-'void' requires an explicit cast c:\program files\microsoft visual studio\myprojects\project1\record.cpp(78) : error C2440: '=' : cannot convert from 'void *' to 'char *' Conversion from 'void*' to pointer to non-'void' requires an explicit cast c:\program files\microsoft visual studio\myprojects\project1\record.cpp(79) : error C2440: '=' : cannot convert from 'void *' to 'char *' Conversion from 'void*' to pointer to non-'void' requires an explicit cast Error executing cl.exe. record.obj - 4 error(s), 0 warning(s)
Last edited by LuciWiz : 11-Aug-2005 at 00:58.
  #10  
Old 11-Aug-2005, 03:33
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

It compiles on error level 4, with zero errors, Dude.


Quote:
Originally Posted by zidanefreak01
here are the errors when i compiled it....

I changed the stack to work with insertion at the head, as it was supposed to be from the beginning.

This is the code, try to continue from here. If you have a problem, post a code and tell me what is wrong - I will try to help :

CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct snode
{
   char *student_name;
   char *student_address;
   char *student_ID;
   struct snode *next;
   struct snode *pre;
};

typedef struct my_stack
{
   struct snode *head;
   struct snode *tail;
} my_stack;

//Init the stack.
void init_stack(my_stack *the_stack);

//Add a record to the stack.
int add_record(my_stack *the_stack,char *name,char *address,char *ID);

//Search a record in the stack. Return a pointer to the node, if exists.
//else return NULL.
struct snode search_record(char *ID);

//Print stack by order, to the standard output.
void print_stack(my_stack *the_stack);

void main()
{
  char user_choice = '\0';
  my_stack the_stack;
  init_stack(&the_stack);
  while (user_choice != 'q')
  {
    //Print Menu to the screen.
    printf("Menu\n");
    printf("----\n");
    printf("[a] add a new student.\n");
    printf("[d] delete student.\n");
    printf("[e] edit student.\n");
    printf("[v] view student.\n");
    printf("[q] quit program.\n");  //You don't suppose the user want to grow old with the application :-)
    user_choice = getchar();
  }
  
  //Example code for you. These are the appropriate ways of using the "add_record" and "print_stack" functions.
  add_record(&the_stack,"Kobi","Israel","1234567");
  add_record(&the_stack,"You","Must","Do your own homework");
  add_record(&the_stack,"It","Compiles","Just fine");
  print_stack(&the_stack);
}

void init_stack(my_stack *the_stack)
{
  the_stack->head = NULL;
  the_stack->tail = NULL;
}

int add_record(my_stack *the_stack,char *name,char *address,char *ID)
{
  int result = 1;
  //First, create the new stack node.
  struct snode *new_snode;
  new_snode = malloc(sizeof(struct snode));
  if (new_snode == NULL)
  {
    printf("Memory allocation failed.\n");
    result = 0;
  }
  else
  {
    //Allocate memory for record data.
    new_snode->student_name = malloc(sizeof(strlen(name) + 1));
    new_snode->student_address = malloc(sizeof(strlen(address) + 1));
    new_snode->student_ID = malloc(sizeof(strlen(ID) + 1));
    if (new_snode->student_name == NULL || new_snode->student_address == NULL
      || new_snode->student_ID == NULL)
    {
      printf("Memory allocation failed.\n");
      result = 0;
    }
    else
    {
      //Store data in the new node.
      strcpy(new_snode->student_name,name);
      strcpy(new_snode->student_address,address);
      strcpy(new_snode->student_ID,ID);
      //Check if the stack is empty.If so, add record as first record.
      if (the_stack->head == NULL)
      {
        //Insert at the head.
        new_snode->next = NULL;
        new_snode->pre = NULL;
        the_stack->head = new_snode;
        the_stack->tail = new_snode;
      }
      else
      {
        //Insert at the head.
        new_snode->pre = NULL;
        new_snode->next = the_stack->head;
        the_stack->head = new_snode;
      }
    }
  }
  return (result);
}

void print_stack(my_stack *the_stack)
{
  struct snode *iterator = the_stack->head;
  while (iterator != NULL)
  {
    printf ("%s\t%s\t%s\n",iterator->student_name,iterator->student_address,iterator->student_ID);
    iterator = iterator->next;
  }
  
}

It compiles and runs on visual c++ 6.0 with zero errors and zero warnings .
What compiler are you using ?

Best regards,
Kobi Hikri.
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Formatting C / C++ code WaltP C Programming Language 1 07-Jan-2008 00:59
Guidelines for posting requests for help - UPDATED! WaltP C++ Forum 0 21-Apr-2005 03:44
Problem with int mixed with char,... leitz C++ Forum 17 07-Dec-2004 21:56
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 04:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:28.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.