Thursday, November 25, 2010

Oh, yeah, linked list!

  After discussion, the text file is using linked list again.The example demonstrates how to use Fardad's linked list. It's quite simple. He did great amount of job for us. I think dynamic array should be very simple, too. But I don't understand it. By the way, in Java, there is also a class implement linked list just similar to Fardad's code. You don't need to know how linked list builds, just use it. The following are the example. Copy it and just to run it. You will understand it.


#include "strque.h"
#include "iostream"
int main()
{
 StrQue A;
 //insert hello world into the end of the linked list
 //and the current pointer point to "hello world"
 //use visit to get the string that current points to
 char str[100]="Hello, world!";
 A.Append(str);
 printf("%s\n",A.Visit());
 //add "welcome to oop344" to the end of linked list
 //current points to "welcome to oop344"
 strcpy(str,"Welcome to OOP344");
 A.Append(str);
 printf("%s\n",A.Visit());
 //go back to the head
 //that means current points to "welcome to oop344"
 A.GoHead();
 printf("%s\n",A.Visit());
 //if you know the position in the linked list, you can go directly by accessing as an array
 //a.operator[](1) prints the 2nd string
 //that means "welcome to oop344"
 //from calling visit function, you will see that operator[] would not change the current pointers
 printf("%s\n",A.operator [](1));
 printf("%s\n",A.Visit());
 //pirnt how many nodes in the linked list
 printf("%d\n",A.Size());
 //now, current points to "welcome to oop344"
 A.GoTail();
 strcpy(str,"The project is hard");
 A.Insert(str);
 //now, current points to "the project is hard"
    printf("%s\n", A.Visit());
 //you can the order for the whole linked list
 //"hello world"->"the project is hard"->"welcome to oop344"
 printf("%s\n",A.operator [](0));
 printf("%s\n",A.operator [](1));
 printf("%s\n",A.operator [](2));
 //current points to "hello world"
 //and store the current
 A.GoHead();
 A.StoreCur();
 //now, current move to the end, "welcome to oop344"
 A.GoTail();
 printf("%s\n",A.Visit());
 //after restore, current go back to the position you store
 A.RestoreCur();
 printf("%s\n",A.Visit());
 return 0;
}

No comments:

Post a Comment