:cry: :cry: :cry: :cry:
Plzzzzzzzzzzzz my people, someone help me out here.
I have this VB .Net project on Stack,Queue and Linked List and I have NULL clue on how to do it.
So plz if ANYONE out there knows ANYTHING about these classes, plz holla at ur sista (who is desperate :cry: )...Thank you
Note:
Stack adds/gets element to and from the front
Queue adds elements to the rear and removes from the front
Linked List do both (Stack and Queue)
and all the classes have methods to do the stuff. My instructor decided not to teach us this topic but THEN decided TO GIVE it to us as a PROJECT (worth 20% but now 15%).........so plz help me out.
Ikon God sai looking! yanzu da zeron ce ma da na ci 8)
Anyways...I have finally managed to do the project...and it's pretty cool :o -I like it
Oh Sista,
Have not been coming round for some time. I really could have helped even though may not be to your test. But nevertheless this is what I understand from your question for future reference.
A good student of Data Structures must understand these three functions very well before he answers his name.
In programming, there are three segments:
STACK segment
DATA segment
and CODE segment
STACK is where variables are stored. DATA is where information stocked in the program are stored. And the CODE segment iis where instructions are stored. All these are tarried by your computer during compilation process.
In the stack, items or variables are stored ONE on TOP of the OTHER. In programing terminolgy we call this process PUSH. That is to say Variables are PUSHED into the STACK. And during the programming execution Items are required to be retreived from the STACK for processing. The process is called POP. That is to say variable are POPED out from the STACK.
There are ways in which items can be PUSHED or POPED out in or out from the STACK. The first one as you say LILO Last In Last Out. Since the item you push unto the STACK will only go on top of the last item pushed. It invariably means it will be the one that will come out first whenever items are to be retrieved from the PILE of items in the STACK.
I have a good Turbo Pascal program that demonstrates how this one works. I will soon get the program and put it up here for your scrutiny and anybody who may wish to learn. Once you see the one on STACK you are likely to be able to develop the one of QUEUE and LINK for yourself. You can even help convert the code to VB 6.0 for our learning.
Or pls you can get us that your project up here that we too can learn from your efforts.
Thank you once again.
Most grateful I remain
Quote from: "Ihsan"Ikon God sai looking! yanzu da zeron ce ma da na ci 8)
Anyways...I have finally managed to do the project...and it's pretty cool :o -I like it
To thank God (Alhamdulillah) share it with us, it will be apreciated. :D
Hey Mr. Waz, I know you for this long but yet you did not give me anything about this Stack. You really a friend?????????????????????
:)
Thankx Waz...
Ok...here's the code (the CListNode is first implemented and then ClsList followed by others (Stack and Queue)
Public Class CListNode
Private mData As Object
Private mNextNode As CListNode
Public Sub New(ByVal dataValue As Object, ByVal nextNodeValue As CListNode)
mData = dataValue
mNextNode = nextNodeValue
End Sub
Public Sub New(ByVal dataValue As Object)
MyClass.New(dataValue, Nothing)
End Sub
Public ReadOnly Property Data() As Object
Get
Return mData
End Get
End Property
Public Property NextNode() As CListNode
Get
Return mNextNode
End Get
Set(ByVal value As CListNode)
mNextNode = value
End Set
End Property
End Class
*************************************************************
Public Class Clist
Private mfirstNode As CListNode
Private mlastNode As CListNode
Sub New()
End Sub
Public Property firstNode() As CListNode
Get
Return MyClass.mfirstNode
End Get
Set(ByVal Value As CListNode)
MyClass.mfirstNode = Value
End Set
End Property
Public Property lastNode() As CListNode
Get
Return MyClass.mlastNode
End Get
Set(ByVal Value As CListNode)
MyClass.mlastNode = Value
End Set
End Property
Public Sub insertFront(ByVal itm As Object)
Dim obj As CListNode = New CListNode(itm)
If MyClass.isEmpty Then
obj.NextNode = Nothing
firstNode = obj
lastNode = obj
Else
obj.NextNode = firstNode
firstNode = obj
End If
End Sub
Public Sub insertBack(ByVal itm As Object)
Dim obj As CListNode = New CListNode(itm)
If MyClass.isEmpty Then
obj.NextNode = Nothing
firstNode = obj
lastNode = obj
Else
lastNode.NextNode = obj
lastNode = lastNode.NextNode
End If
End Sub
Public Function removeFront() As Object
Dim CurrentNode As CListNode = firstNode.NextNode
Dim PreviousNode As CListNode = firstNode
If MyClass.isEmpty Then
Return Nothing
ElseIf lastNode Is firstNode Then
firstNode = Nothing
lastNode = Nothing
Console.WriteLine("First item removed")
Else
PreviousNode = Nothing
firstNode = CurrentNode
Console.WriteLine("First item removed")
End If
End Function
Public Function removeBack() As Object
Dim CurrentNode As CListNode = firstNode
Dim PreviousNode As CListNode = firstNode
If MyClass.isEmpty Then
Return Nothing
ElseIf lastNode Is firstNode Then
firstNode = Nothing
lastNode = Nothing
Console.WriteLine("Last item removed")
Else
Do While Not CurrentNode.NextNode Is Nothing
PreviousNode = CurrentNode
CurrentNode = CurrentNode.NextNode
Loop
lastNode = PreviousNode
lastNode.NextNode = Nothing
Console.WriteLine("Last item removed")
End If
End Function
Public Sub makeEmpty()
Do While Not MyClass.isEmpty
MyClass.removeBack()
Loop
Console.WriteLine("Emptied")
End Sub
Public Function isEmpty() As Boolean
Return firstNode Is Nothing
End Function
Public Sub display()
Dim CurrentNode As CListNode = firstNode
If isEmpty() Then
Console.WriteLine("Empty")
End If
Do While Not CurrentNode Is Nothing
Console.WriteLine(CStr(CurrentNode.Data))
CurrentNode = CurrentNode.NextNode
Loop
End Sub
End Class
Public Class Cstack
Inherits Clist
Private mlist As Clist
Public Sub New()
List = New Clist
End Sub
Public Property List() As Clist
Get
Return mlist
End Get
Set(ByVal Value As Clist)
mlist = Value
End Set
End Property
Public Sub push(ByVal itm As Object)
List.insertFront(itm)
End Sub
Public Sub pop()
List.removeFront()
End Sub
Public Sub StkDisplay()
List.display()
End Sub
Public Function top() As Object
If List.isEmpty Then
Console.WriteLine("The stack is empty")
Else
Return List.firstNode.Data
End If
End Function
Public Function StkIsEmpty() As Object
If List.isEmpty() Then
Console.WriteLine("The stack is empty")
End If
End Function
Public Sub StkMakeEmpty()
List.makeEmpty()
End Sub
End Class
**************************************************************************************************************************
Public Class Cqueue
Inherits Clist
Private mlist As Clist
Public Sub New()
List = New Clist
End Sub
Public Property List() As Clist
Get
Return mlist
End Get
Set(ByVal Value As Clist)
mlist = Value
End Set
End Property
Public Sub enqueue(ByVal itm As Object)
List.insertBack(itm)
End Sub
Public Sub dequeue()
List.removeFront()
End Sub
Public Function QisEmpty() As Object
If List.isEmpty() Then
Console.WriteLine("The Queue is empty")
End If
End Function
Public Sub QMakeEmpty()
List.makeEmpty()
End Sub
Public Sub Qdisplay()
List.display()
End Sub
End Class
**************************************************************************************************************************
Module Module1
Dim choice As String
Dim item As Object
Dim stk As Cstack = New Cstack
Dim qu As Cqueue = New Cqueue
Dim lst As Clist = New Clist
Sub Main()
Console.WriteLine()
Console.WriteLine("Main Menu")
Console.WriteLine("Select your choice")
Console.WriteLine("A: for stack")
Console.WriteLine("B: for Queue")
Console.WriteLine("C: for List")
Console.WriteLine("D: to End")
Try
choice = Console.ReadLine
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Select Case UCase(choice)
Case "A"
StackMethod()
Case "B"
QueueMethod()
Case "C"
ListMethod()
Case "D"
End
End Select
End Sub
Private Sub StackMethod()
Console.WriteLine()
Console.WriteLine("Select the operation you would like to perform on the stack")
Console.WriteLine("A: To add item to top of stack <Push>")
Console.WriteLine("B: To remove item from the top of stack <Pop>")
Console.WriteLine("C: To view the item on the top of the stack <Top>")
Console.WriteLine("D: To clear the stack")
Console.WriteLine("E: To display all the items in the stack")
Console.WriteLine("F: To exit system")
Console.WriteLine("G: To return to Main Menu")
Console.WriteLine("H: To perform another operation on the stack")
Try
choice = Console.ReadLine
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Select Case UCase(choice)
Case "A"
Console.WriteLine("Write the item you want to add to the top of the stack")
item = Console.ReadLine
stk.push(item)
Case "B"
stk.pop()
Case "C"
Console.WriteLine(stk.top())
Case "D"
Try
If CBool(stk.StkIsEmpty) Then
Err.Raise(3)
End If
Catch When Err.Number = 3
Console.WriteLine("This is an empty Stack")
End Try
stk.StkMakeEmpty()
Case "E"
stk.StkDisplay()
Case "F"
End
Case "G"
Main()
Case "H"
StackMethod()
Case Else
Console.WriteLine("You have entered an invalid option")
End Select
StackMethod()
End Sub
Private Sub QueueMethod()
Console.WriteLine()
Console.WriteLine("Select the operation you would like to perform on the queue")
Console.WriteLine("A: To add item to the end of the queue <Enqueue>")
Console.WriteLine("B: To remove an item from the top of the queue <Dequeue>")
Console.WriteLine("C: To Display all items in the queue")
Console.WriteLine("D: To clear all the items in the queue")
Console.WriteLine("E: To exit the system")
Console.WriteLine("F: To return to Main Menu")
Console.WriteLine("G: To perform another operation on the queue")
Console.WriteLine()
Try
choice = Console.ReadLine
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Select Case UCase(choice)
Case "A"
Console.WriteLine("enter the item u want to add to the queue")
item = Console.ReadLine
qu.enqueue(item)
Case "B"
qu.dequeue()
Case "C"
qu.Qdisplay()
Case "D"
Try
If CBool(qu.QisEmpty) Then
Err.Raise(3)
End If
Catch When Err.Number = 3
Console.WriteLine("The list is already empty")
End Try
qu.QMakeEmpty()
Case "E"
Exit Select
Case "F"
Main()
Case "G"
QueueMethod()
Case Else
Console.WriteLine("You have entered an invalid option")
End Select
QueueMethod()
End Sub
Private Sub ListMethod()
Console.WriteLine()
Console.WriteLine("A: To add item to the top of the list")
Console.WriteLine("B: To add item to the end of the list")
Console.WriteLine("C: To remove item from top of the list")
Console.WriteLine("D: To remove item from end of the list")
Console.WriteLine("E: To delete all items in the list")
Console.WriteLine("F: To Display all the items in the list")
Console.WriteLine("G: To exit the system")
Console.WriteLine("H: To return to Main Menu")
Console.WriteLine("I: To perform another operation on the list")
Try
choice = Console.ReadLine
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Select Case UCase(choice)
Case "A"
Console.WriteLine("enter the item u want to add to the front of the list")
item = Console.ReadLine
lst.insertFront(item)
Case "B"
Console.WriteLine("enter the item u want to add to the back of the list")
item = Console.ReadLine
lst.insertBack(item)
Case "C"
lst.removeFront()
Case "D"
lst.removeBack()
Case "E"
Try
If lst.isEmpty Then
Err.Raise(3)
End If
Catch When Err.Number = 3
MsgBox("The list is already empty")
End Try
lst.makeEmpty()
Case "F"
lst.display()
Case "G"
End
Case "H"
Main()
Case "I"
ListMethod()
Case Else
Console.WriteLine("You have entered an invalid option")
End Select
ListMethod()
End Sub
End Module
Yoruba Land ( Kola), you mean you've never seen dis prog. wid me in da office 2 yrs back???????????
Ihsan this is what I have in Pascal. I will take sometime study your codes in visual Basic. It is really a great thing that we are learning here. Below is d code:
*************************************************************
Program Stak;
Uses
Crt;
Const
StackSize = 10;
Type
StackItem = Record
StName : String[15];
Age : Integer;
Height : Real;
End;
Stack = Record
Item : Array[1..StackSize] of StackItem;
Top : Integer;
End;
Var
Item : StackItem;
S : Stack ;
Endprog : Boolean ;
StackInit : Boolean;
Function Isempty(S: Stack): Boolean;
Begin
If S.Top = 0 Then
Isempty := True
Else
Isempty := False
End;
Function StackFull(S:Stack) : Boolean;
Begin
If S.Top > StackSize Then
StackFull := True
Else
StackFull := False;
End;
Procedure Create(Var S:Stack);
Begin
S.Top := 0
End;
Procedure Add (I : StackItem ;Var S: Stack);
Const
ErrorMessage = 'Attempt to push into a full stack' ;
Begin
S.Top := S.Top + 1 ;
If StackFull(S) Then
Begin
Writeln(ErrorMessage);
Readln;
Exit;
End Else
S.Item[S.Top] := I
End;
Procedure Delete(Var S:Stack);
Const
ErrorMessage = 'Attempt to pop an empty stack';
Begin
If Isempty(S) Then
Begin
Writeln(ErrorMessage);
Readln;
Exit;
End Else
S.Top := S.Top - 1;
End;
Function MainMenu: Char;
Var
Ch : Char;
Begin
ClrScr;
Writeln('1 . Push ');
Writeln;
Writeln('2 . Pop' );
Writeln;
Writeln('3 . Quit' );
Repeat
Ch := ReadKey;
Until KeyPressed;
MainMenu := Ch;
End;
Begin
StackInit := False;
Endprog := False;
While Not Endprog Do
Begin
Case MainMenu of
'1' : Begin
Begin
ClrScr;
If not StackInit Then
Begin
Create(S);
StackInit := True
End;
With Item Do Begin
Write('Name:');
Readln(StName);
Write('Age:');
Readln(Age);
Write('Height:');
Readln(Height);
End;
Add(Item,s);
End;
End;
'2' : Delete(S);
'3' : Endprog := True;
Else
End;
End;
End.
Ihsan I definitely agree with your words.