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

29
Tuesday, February 6, 2007 Invest yourself in Invest yourself in everything you do. There’s everything you do. There’s fun in being serious.” fun in being serious.” - Wynton Marsalis

Post on 20-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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

Page 2: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

Linked Lists

struct node {

int data;

node* next;

};

Page 3: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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;}

Page 4: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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.

Page 5: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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.

Page 6: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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;

Page 7: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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.

Page 8: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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;

Page 9: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

Building the linked list . . .

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

Page 10: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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!

Page 11: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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; }

Page 12: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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; }

Page 13: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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; }

Page 14: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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; }

Page 15: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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;

}

Page 16: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

Length of the linked list . . .

int Length(node* head) {

node* current = head;

int count = 0;

while (current != NULL) {

count++;

current = current->next;

}

return count;

}

Page 17: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

What is wrong here?

struct node{

int data;

node* next;

};

void PrintList(node* list){

while(list!=NULL){

cout<<list->data<<endl;

list++;

}

}

Page 18: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

What is wrong here?

struct node{

int data;

node* next;

};

void PrintList(node* list){

while(list!=NULL){

cout<<list->data<<endl;

list++; //Wrong!

}

}

Page 19: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

struct node{int data;node* next;

};void PrintList(node* list){

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

}}

Page 20: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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);

}

Page 21: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

Append at end.

Page 22: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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.

Page 23: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

Linked list operations

Try on some examples!

Page 24: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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();

Page 25: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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( )

Page 26: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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;}

Page 27: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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( )

Page 28: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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

Page 29: Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being

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