You are on page 1of 4

# EEE 212 – Algorithms & Data Structures

Spring 05/06 – Lecture Notes # 11 Outline Applications of Linked Lists Fundamental functions and applications of Linked Lists Search and Display List Operations SAMPLE PROGRAMS/FUNCTIONS Searching through the linked list.
Ex: The following function searches through the linked list and returns a pointer the first occurrence of the search key or returns NULL pointer if the search key is not in the list. Note that the linked list contains integer data items. nodptr *searchList(nodeptr *plist, int key) { nodeptr *p; p = plist; while(p != NULL){ if(p->info == key) return p; p = p->next; } return NULL; }

Ex: Write a function to display the student with highest CGPA in a linked list containing student data. Use the following node structure for your linked list. struct node{ int stNo; float CGPA; struct node *next; }; typedef struct node nodptr; void DisplayMax(nodeptr *plist) { nodeptr *p; float maxCGPA=-1.0; int maxstNo; p = plist; /*current node*/ if(p == NULL){ printf(“no node/data is available in the list\n”); return; } do{ if(p->CGPA > maxCGPA){ maxCGPA = p->CGPA; maxstNo = p->stNo; } p = p->next; } while(p!= NULL);

1

printf(“The student number with max CGPA: %d\n”, maxstNo); printf(“The student’s CGPA: %d\n”, maxCGPA); }

A full program using linear linked list structure
Write a program where you can insert a new item to the end of the Linked List, delete the last item from the linked list and display all the list items in the linked list. Note that the Linked List contains integer items. #include<stdio.h> #include<stdlib.h> #define NULL 0 /*contains malloc & free functions */

struct node{ int info; struct node *next; }; typedef struct node nodeptr;

Use double pointer if you are changing the content of the List which is a pointer to the beginning of the Linked List.

int menu(void); void insertEnd(nodeptr **, int);/*insert to the end of the list*/ void deleteEnd(nodeptr **, int *);/*delete from the end " " "*/ void displayList(nodeptr *); nodeptr *getnode(void); void freenode(nodeptr *); Use single pointer if you are not changing the int main() content of the List which is a pointer to the { beginning of the Linked List. int choice,x; nodeptr *List= NULL; /*List points the beginning of the list*/ do{ choice = menu(); switch(choice){ case 1: printf("Enter new data item:"); scanf("%d",&x); insertEnd(&List,x); break; case 2: deleteEnd(&List,&x); printf("The deleted data item is: %d",x); break; case 3: displayList(List); break; default: if(choice == 4) printf("\nEnd of the Program"); else printf("\nEnter the choice again"); break; } }while(choice !=4); return 0; }

2

int menu(void) { int c; printf("\nMain Menu:\n"); printf("1 - Insert an item\n"); printf("2 - Delete an item\n"); printf("3 - Display the Linked List items\n"); printf("4 - Quit\n\n"); scanf("%d",&c); Use double pointer if you are changing the return c; content of the plist which is a pointer to the beginning of the Linked List. } void insertEnd(nodeptr **plist, int x) { nodeptr *p, *q; p=getnode(); p->info = x; p->next = NULL; if(*plist == NULL) *plist = p; else{ q = *plist; while(q->next != NULL) q = q->next; q->next = p; Use double pointer if you are changing the } content of the plist which is a pointer to the beginning of the Linked List. } void deleteEnd(nodeptr **plist, int *px) { nodeptr *p,*q; if(*plist == NULL){ printf("void deletion\n"); return; } p = *plist; /*current node*/ q = p->next; /*the node after the current node*/ if(q == NULL){ *px = p->info; /*the data of the deleted element*/ freenode(p); *plist = NULL; } else{ while(q->next != NULL){ p = p->next; /*go to the next node*/ q = p->next; } p->next = NULL; *px = q->info; /*the data of the deleted element*/ freenode(q); } }

3

void displayList(nodeptr *plist) { nodeptr *p; p = plist; /*current node*/ if(p == NULL){ printf("no node/data is available in the list\n"); return; } while(p!= NULL){ printf("%5d", p->info); /*prints the current data*/ p = p->next; } } nodeptr *getnode(void) { nodeptr *p; p = (nodeptr *) malloc(sizeof(nodeptr)); return p; } void freenode(nodeptr *p) { free(p); }

Use single pointer if you are not changing the content of the plist which is a pointer to the beginning of the Linked List.

4