Académique Documents
Professionnel Documents
Culture Documents
Outline of Topics 1. A substring example 2. Lists a. Self referential structures b. Allocation c. Deallocation 3. Stack Example 4. Doubly linked lists a. Declaration b. Allocation c. Accessing 5. #define debug 6. External functions a. Declaration b. Accessing c. Separate compilation 7. Files a. Of structures b. Accessing records within the file
1. A substring example
#include <stdio.h> #include <string.h> #define SZ 20 int substr(char *,char *, int, int); void main () { char str[SZ]; char small_str[SZ],new_str[SZ]; printf ("Enter a string: "); scanf ("%s", str); if (substr(str,small_str, 3, 5)) printf ("3rd thru 5th element of %s is %s\n",str, small_str); else printf ("invalid substring\n"); } int substr(char *str, char *new_str, int start, int last) { int len, i; len=strlen(str); if (start > len || last > len) return 0; for (i=0;i< start - 1; i++) str++; for (i=start-1; i < last; i++) *new_str++ =*str++; *new_str = '\0'; return 1; } Enter a string: 123456789 3rd thru 5th element of 123456789 is 345 Enter a string: 1234 invalid substring
Lists Use a self referential structure (one element of the structure is a pointer to the structure)
struct node { int data; struct node *next; };
Allocate nodes using malloc and sizeof (struct node) Typecast the pointer returned by malloc (struct node *) Typedefs are useful to eliminate redundancy Use free to deallocate the node A header file is useful defining structures and external information 3. Stack Example
stack.h extern void stack_init(); extern int stack_empty(); extern void stack_push(stack_data d); extern stack_data stack_pop(); #include <stdio.h> #include <stdlib.h> struct stack_rec { stack_data data; struct stack_rec *next; }; typedef struct stack_rec REC; stack.c #include "stack.h" #include <stdio.h> #include <stdlib.h> REC *top = NULL; void main() { int i; stack_init(); for (i=0; i< 9; i++){ printf("Pushing data item %d\n", i); stack_push(i); } while (!stack_empty()){ i = stack_pop(); printf("Item removed from stack is %d\n",i); } } void stack_init() { top = NULL; }
int stack_empty() { if (top == NULL) return(1); else return (0); } void stack_push(stack_data d) { REC *temp; temp = (REC *) malloc (sizeof (REC)); temp->data = d; temp->next = top; top = temp; } stack_data stack_pop() { REC *temp; stack_data d = 0; if (top != NULL) { d = top->data; temp = top; top = top->next; free(temp); } return(d); } Pushing data item 0 Pushing data item 1 Pushing data item 2 Pushing data item 3 Pushing data item 4 Pushing data item 5 Pushing data item 6 Pushing data item 7 Pushing data item 8 Item removed from stack is 8 Item removed from stack is 7 Item removed from stack is 6 Item removed from stack is 5 Item removed from stack is 4 Item removed from stack is 3 Item removed from stack is 2 Item removed from stack is 1 Item removed from stack is 0
4. Doubly linked lists Declaration contains two pointers next and previous
#include <stdio.h> #include <stdlib.h> typedef struct listnode { int entry; struct listnode *next; struct listnode *previous; }ListNode; } typedef struct list{ ListNode *first; ListNode *last; int count; } List; /* function prototypes */ void CreateList(List *); void PrintList(List *); void InsertList(int,List *); void DeleteList(int,List *); } void main() { List *order_list; ListNode *element; order_list = (List *) malloc (sizeof(List)); CreateList(order_list); printf("Entered 4 elements: 8, 1, 22, 12\n"); PrintList (order_list); DeleteList(8, order_list); DeleteList(4, order_list); printf("\nDeleted element 8\n"); PrintList (order_list); DeleteList(22, order_list); DeleteList(1, order_list); printf("\nDelete delements 22 and 1\n"); PrintList (order_list); } void CreateList(List *order_list) { int element; order_list->count = 0; order_list->first = NULL; void InsertList(int x, List *list) { ListNode *temp, *newnode; newnode = (ListNode *) malloc(sizeof(ListNode)); newnode->entry=x; newnode->next=NULL; newnode->previous=NULL; if(list->first == NULL) /* the list was empty */ { newnode->next = NULL; newnode->previous = NULL; list->first = list->last = newnode; } else /* insert front non empty list */ { temp=list->first; while (temp->entry < x && temp->next != NULL) { temp=temp->next; } while(p) /* traverse list { printf("Element %d\n", p->entry); p = p->next; } */ void PrintList(List *order_list) { ListNode *p; p = order_list->first; order_list->last = NULL; element = 8; InsertList (element, order_list); element = 1; InsertList (element, order_list); element = 22; InsertList (element, order_list); element = 12; InsertList (element, order_list);
if (temp->next == NULL &&temp->entry < x) /* Insert as last in list */ { newnode->previous=list->last; list->last->next = newnode; list->last = newnode; } else if (temp == list->first) /*Insert as first */ { newnode->next = list->first; list->first->previous = newnode; list->first = newnode; } else /*Insert before temp */ { newnode->previous = temp->previous; newnode->next= temp; temp->previous->next=newnode; temp->previous=newnode; } list->count++; } } void DeleteList(int num,List *order_list) { ListNode *temp = NULL; if(order_list->first == NULL) printf ("Error -- Attempt to delete from an empty list."); else if (order_list->first == order_list->last) if (order_list->first->entry == num) { free(order_list->first); order_list->first= NULL; order_list->last = NULL; } else printf("Error -- not found\n"); else { temp=order_list->first; while (temp->entry != num && temp->next != NULL) temp=temp->next; if (temp->entry == num) /*remove temp*/ { if (temp==order_list->first){ temp->next->previous = NULL; order_list->first = temp->next; } else if (temp == order_list->last){ temp->previous->next = NULL; order_list->last = temp->previous;
} else { temp->previous->next=temp->next; temp->next->previous = temp->previous; } free (temp); } else printf ("Error -- element not found\n"); } }
Entered 4 elements: 8, 1, 22, 12 Element 1 Element 8 Element 12 Element 22 Error -- element not found Deleted element 8 Element 1 Element 12 Element 22 Deleted elements 22 and 1 Element 12