tuesday, february 6, 2007 invest yourself in everything you do. there’s fun in being serious.”...

Post on 20-Dec-2015

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Tuesday, February 6, 2007

“Invest yourself in everything Invest yourself in everything you do. There’s fun in being you do. There’s fun in being

serious.”serious.”

- Wynton Marsalis

Linked Lists

struct node {

int data;

node* next;

};

Linked Lists - easy example first . . .

/*Build the list {1, 2, 3}*/

node* BuildOneTwoThree() {

node* head = NULL;

node* second = NULL;

node* third = NULL;

head = new node;

second = new node;

third = new node;

head->data = 1;

head->next = second;

second->data = 2;

second->next = third; third->data = 3; third->next = NULL; return head;}

Building the linked list . . .

BuildOneTwoThree() is fine as an example of pointer manipulation code, but it's not a general mechanism to build lists.

The best solution will be an independent function which adds a single new node to any list.

We can then call that function as many times as we want to build up any list.

Building the linked list . . .

The classic 3-Step Link In operation which adds a single node to the front of a linked list. The 3 steps are…

1) Allocate Allocate the new node in the heap and set its .data to whatever needs to be stored.

Building the linked list . . .

The classic 3-Step Link In operation which adds a single node to the front of a linked list. The 3 steps are…

1) Allocate Allocate the new node in the heap and set its .data to whatever needs to be stored.

node* myNode;

myNode = new node;

myNode->data = data_user_wants_stored;

Building the linked list . . .

2) Link Next Set the .next pointer of the new node to point to the current first node of the list. (This is actually just a pointer assignment remember: "assigning one pointer to another makes them point to the same thing.”)

3) Link Head Change the head pointer to point to the new node, so it is now the first node in the list.

Building the linked list . . .

2) Link Next Set the .next pointer of the new node to point to the current first node of the list. (This is actually just a pointer assignment remember: "assigning one pointer to another makes them point to the same thing.”)

myNode->next = head;

3) Link Head Change the head pointer to point to the new node, so it is now the first node in the list.

head = myNode;

Building the linked list . . .

The problem is to write a general function which adds a single node to head end of any list.

Building the linked list . . .void WrongInsertAtFront(node* head, int data) {

node* myNode = new node;

myNode->data = data;

myNode->next = head;

head = myNode;

}int main(void){

node* head = BuildOneTwoThree();

WrongInsertAtFront(head, 4); WrongInsertAtFront(head, 5);

display(head);

return 0; }

BEWARE!

This is WRONG way of inserting at front!

Building the linked list . . .void WrongInsertAtFront(node* head, int data) {

node* myNode = new node;

myNode->data = data;

myNode->next = head;

head = myNode; // NO this line does not work!

}int main(void){

node* head = BuildOneTwoThree();

WrongInsertAtFront(head, 4); WrongInsertAtFront(head, 5);

display(head);

return 0; }

Different ways to InsertAtFront of a linked list

node* ReturnsListAfterInsertAtFront(node* lsthead, int data){

node* myNode = new node;

myNode->data = data;

myNode->next = lsthead;

lsthead = myNode;

return lsthead; }

int main(void){

node *head=NULL;

head=ReturnsListAfterInsertAtFront(head, 14);

head=ReturnsListAfterInsertAtFront(head, 15);

head=ReturnsListAfterInsertAtFront(head, 11);

display(head);

return 0; }

Building the linked list . . .void CorrectInsertAtFront(node* &head, int data) {

node* myNode = new node;

myNode->data = data;

myNode->next = head;

head = myNode;

}

int main(void){

node *head=NULL;

CorrectInsertAtFront(head, 4);

CorrectInsertAtFront(head, 5);

display(head); //OUTPUT?

return 0; }

Building the linked list . . .void CorrectInsertAtFront(node* &head, int data) {

node* myNode = new node;

myNode->data = data;

myNode->next = head;

head = myNode;

}

int main(void){

node* head = BuildOneTwoThree();

CorrectInsertAtFront(head, 4);

CorrectInsertAtFront(head, 5);

display(head); //OUTPUT?

return 0; }

Display the linked list . . .

void display(node * mylist){

while (mylist!=NULL){

cout<<mylist->data<<endl;

mylist=mylist->next;

}

}

int main(void){

node * list=BuildOneTwoThree();

display(list);

return 0;

}

Length of the linked list . . .

int Length(node* head) {

node* current = head;

int count = 0;

while (current != NULL) {

count++;

current = current->next;

}

return count;

}

What is wrong here?

struct node{

int data;

node* next;

};

void PrintList(node* list){

while(list!=NULL){

cout<<list->data<<endl;

list++;

}

}

What is wrong here?

struct node{

int data;

node* next;

};

void PrintList(node* list){

while(list!=NULL){

cout<<list->data<<endl;

list++; //Wrong!

}

}

struct node{int data;node* next;

};void PrintList(node* list){

while(list!=NULL){cout<<list->data<<endl;list=list->next;

}}

SELF TEST: Building the linked list . . .

void LinkTest() {

node* head = BuildOneTwoThree();

node* myNode;

myNode= new node;

myNode->data = 4;

myNode->next = head;

head = myNode;

// now head points to the list _________ ?

display(head);

}

Append at end.

What Linked Lists Look Like?More space per element storedOperations towards the front of the list are

fast while operations which access node farther down the list take longer the further they are from the front.

This "linear" cost to access a node is fundamentally more costly then the constant time [ ] access provided by arrays.

In this respect, linked lists are definitely less efficient than arrays.

Linked list operations

Try on some examples!

ifstream fin; //declare input stream called fin for fileofstream fout;//declare output stream called fout for fileint num1, num2, sum;//connect fin to file called myinputfile.txtfin.open("myinputfile.txt"); //connect fout to file called myoutputfile.txtfout.open("myoutputfile.txt");

fin>>num1; //Read first number from myinputfile.txtfin>>num2; //Read second number from myinputfile.txt

sum = num1+num2;//Write sum of numbers to myoutputfile.txtfout<<"sum of "<<num1<<" & "<<num2<<" is "<<sum<<endl;fout<<"Good bye!\n";/*When we are done close files, i.e. disconnect the

streams from files */fin.close(); fout.close();

Command line arguments to main can be used to pass information into a program when you run it.

C++ has two built-in, but optional, parameters to main( )

int argc, char *argv[] receive the command line arguments

argc is an integer that holds the number of arguments on the command line. It will always be at least 1, because the name of the program qualifies as the first argument.

argv is a pointer to an array of character pointersEach pointer in argv array points to a string

containing a command line argument.

argc and argv: Arguments to main( )

int main(int argc, char *argv[]) { if(argc!=4) { cout << "You forgot to give three inputs.\n"; return 1; } cout<<"My program file name is "<<argv[0]<<endl; cout<<"you typed "<<argv[1]<<" after it"<<endl; cout<<"this is the 2nd thing you typed: "<<argv[2]<<endl; cout<<"this is the 3rd thing you typed: "<<argv[3]<<endl;

return 0;}

int main(int argc, char *argv[]) {

int t, i;

for(t=0; t<argc; ++t) {

i = 0;

while(argv[t][i]) {

cout << argv[t][i];

++i;

}

cout << endl;

}

return 0;

}

argc and argv: Arguments to main( )

int main(int argc, char *argv[]) {

double a, b;

if(argc!=3) {

cout << "Usage: give two numeric-arguments\n";

return 1;

}

a = atof(argv[1]);

b = atof(argv[2]);

cout << a + b;

return 0;

}

Passing Numeric Command Line Arguments

double atof(const char*) converts the string form of a number into double (1.7E-308 to 1.7E+308)

int atoi(const char *) converts the string form of a number into integer (-32,768 to 32767)

long atol(const char *) converts the string form of a number into long integer (-2,147,483,648 to 2,147,483,647)

char str[80];

gets(str);

cout<<atoi(str);

Passing Numeric Command Line Arguments

top related