Untitled4 - Arup Guha edited for COP 3502 Lecture Linked...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
// Arup Guha // 10/24/01 edited 2/24/04 for COP 3502 Lecture // Linked List Example / // Edited on 6/1/07 to include an example from Recitation Sheet #3 for COP 3502 / // Edited on 2/3/09 to include two recursive functions: delete and reverse. / #include <stdio.h> #include <stdlib.h> #include <string.h> # // Struct used to form a linked list of integers. struct ll { s int data; struct ll *next; }; } void print(struct ll *front); v struct ll* insert_front(struct ll *front, int num); struct ll* insert_inorder(struct ll *front, int num); struct ll* insert_back(struct ll *front, int num); s struct ll* delete(struct ll *front, int num); struct ll* deleteRec(struct ll *front, int num); /*** JUST ADDED!!! ***/ s struct ll* reverseRec(struct ll *front); /*** JUST ADDED!!! ***/ struct ll* reverse(struct ll *front); struct ll* concat(struct ll *first, struct ll *second); struct ll* maxlist(struct ll *first, struct ll *second); s struct ll* Add(struct ll* A, struct ll* B); struct ll* RecAdd(struct ll* A, struct ll* B); s void dellist(struct ll *p); v int main() { i struct ll *randlist=NULL; struct ll *inorderlist=NULL; struct ll *maximums=NULL; struct ll *final=NULL; struct ll *A = NULL, *B = NULL, *C, *D; // Test out some functions on randlist. randlist = insert_front(randlist,5); print(randlist); randlist = insert_back(randlist,2); print(randlist); randlist = delete(randlist, 3); print(randlist); randlist = insert_front(randlist, 1); print(randlist); randlist = delete(randlist, 2); print(randlist); randlist = insert_front(randlist, 6); randlist = insert_front(randlist, 9);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
randlist = insert_front(randlist, 8); // Create a new list that is the reverse of the old. struct ll* randlist2 = reverse(randlist); print(randlist); print(randlist2); // Now, reverse the original list. randlist = reverseRec(randlist); print(randlist); // Test out some functions on inorderlist. inorderlist = insert_inorder(inorderlist, 4); inorderlist = insert_inorder(inorderlist, 2); inorderlist = insert_inorder(inorderlist, 9); print(inorderlist); inorderlist = insert_inorder(inorderlist, 1); inorderlist = insert_inorder(inorderlist, 7); inorderlist = deleteRec(inorderlist, 4); print(inorderlist); inorderlist = deleteRec(inorderlist, 1); print(inorderlist); inorderlist = deleteRec(inorderlist, 9); print(inorderlist); // Create a new list with all the maximums of the two above. maximums = maxlist(randlist, inorderlist); print(maximums); // Concatenate the two lists above. final = concat(randlist, inorderlist); print(final); // Testing the Add and RecAdd functions. A = insert_front(A, 2); A = insert_front(A, 7); A = insert_front(A, 5); A = insert_front(A, 15); B = insert_front(B, 12); B = insert_front(B, 9); B = insert_front(B, 4); B = insert_front(B, 17); C = Add(A, B); print(C); D = RecAdd(A, B); print(D); system("PAUSE"); return 0; } // Pre-condition: front points to the head of a linked list structure. // Post-condition: each value in the list will be printed, in order.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

Untitled4 - Arup Guha edited for COP 3502 Lecture Linked...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online