OPERATIONS ON SINGLY LINKED LIST (SLL)

Exported from Notepad++
/** * @author Dr. Naveen Kumar K R * * Updated on: Dec 28, 2021 * Updated on: Dec 27, 2021 */ #include <stdio.h> #include <stdlib.h> typedef struct StudentRecord { char USN[20]; char name[20]; char dept[20]; char phone[20]; }Record; typedef struct Node *ListPointer; typedef struct Node { Record data; ListPointer link; }Node; ListPointer last; void menu() { printf("\n.. SINGLY LINKED LIST DEMONSTRATION ..\n"); printf ( "\n\n1. N front Insertion\n\n2. Display\n"); printf ( "\n3. Front Insertion\n\n4. Delete Front\n"); printf ( "\n5. Rear Insertion\n\n6. Delete Rear\n"); printf ( "\n7. Exit\n"); } Record getNextRecord( void ) { Record data; scanf ( "%s", data.USN ); scanf ( "%s", data.name ); scanf ( "%s", data.dept ); scanf ( "%s", data.phone ); return data; } ListPointer getNode( Record data ) { ListPointer temp; temp = ( ListPointer ) malloc ( sizeof ( *temp ) ); if ( temp == NULL ) { printf ("\nAllocation Failed\n"); getchar (); //getch(); exit(EXIT_FAILURE); }else { temp->data = data; } return temp; } int length() { ListPointer node; int count = 0; if( last ) { node = last; do{ count ++; node = node->link; } while( node != last); } return count; } void addFront ( Record data, ListPointer node ) { if( !last ) { last = node; node->link = node; } else { node->link = last->link; last->link = node; } } Record deleteFront ( void ) { ListPointer first; Record data; data = last->link->data; if( last == last->link ) { last = NULL; } else { first = last->link; last->link = last->link->link; } free( first ); return data; } void addRear ( Record data, ListPointer node ) { addFront( data, node ); last = node; } Record deleteRear ( void ) { ListPointer trail; Record data; data = last->data; if( last == last->link ) { last = NULL; } else { trail = last; do{ trail = trail->link; } while( trail->link != last ); free( last ); last = trail; } return data; } void display ( void ) { ListPointer first, node; int i; printf("\nUSN\tNAME\tDEPT.\tPHONE\n"); first = node = last->link; do{ printf ( "\n%s", node->data.USN ); printf ( "\t%s", node->data.name ); printf ( "\t%s", node->data.dept ); printf ( "\t%s\n", node->data.phone ); node = node->link; } while( node != first); printf("\nTotal no. of records: %d\n", length()); } int main ( void ) { Record data; ListPointer node; int nRecords; int choice; int i; while ( 1 ) { menu(); printf ( "\nChoice: " ); scanf ( "%d", &choice ); switch ( choice ) { case 1: printf("\n.. * FRONT INSERTION ..\n"); printf ( "\nHow many STDs you want to insert: " ); scanf ( "%d", &nRecords ); printf("\nGive %d record details one \ by one\n", nRecords); printf("\nUSN\tNAME\tDEPT.\tPHONE\n"); for ( i = 0; i < nRecords; ++ i ) { data = getNextRecord(); node = getNode(data); addFront( data, node); } break; case 2: if ( length() == 0 ) { printf ( "\nEmpty list\n" ); } else { printf("\n Student's details...\n"); display ( ); } break; case 3: printf("\n.. SINGLE FRONT INSERTION ..\n"); printf("\nGive record details..\n"); printf("\nUSN\tNAME\tDEPT.\tPHONE\n"); data = getNextRecord(); node = getNode(data); addFront( data, node ); break; case 4: if ( length() == 0 ) { printf ( "\nEmpty list\n" ); } else { printf("\n.. SINGLE FRONT DELETION ..\n"); data = deleteFront( ); printf("\nDeleted Record: %s\n", data.USN ); } break; case 5: printf("\n.. SINGLE BACK INSERTION ..\n"); printf("\nGive record details..\n"); printf("\nUSN\tNAME\tDEPT.\tPHONE\n"); data = getNextRecord(); node = getNode(data); addRear ( data, node ); break; case 6: if ( length() == 0 ) { printf ( "\nEmpty list\n" ); } else { printf("\n.. SINGLE REAR DELETION ..\n"); data = deleteRear( ); printf("\nDeleted Record: %s\n", data.USN ); } break; case 7: return 0; default: printf ( "\nWrong Choice\n" ); } } } /* OUTPUT: .. SINGLY LINKED LIST DEMONSTRATION .. 1. N front Insertion 2. Display 3. Front Insertion 4. Delete Front 5. Rear Insertion 6. Delete Rear 7. Exit Choice: 1 .. * FRONT INSERTION .. How many STD's: 3 Give 3 record details one by one USN NAME DEPT. PHONE 111 John CSE 11111 222 Jack CSE 22222 333 Kate ISE 33333 Choice: 2 Student's details... USN NAME DEPT. PHONE 333 Kate ISE 33333 222 Jack CSE 22222 111 John CSE 11111 No. of STD's: 3 Choice: 3 .. SINGLE FRONT INSERTION .. Give record details.. USN NAME DEPT. PHONE 444 Daniel ECE 44444 Choice: 2 Student's details... USN NAME DEPT. PHONE 444 Daniel ECE 44444 333 Kate ISE 33333 222 Jack CSE 22222 111 John CSE 11111 No. of STD's: 4 Choice: 4 .. SINGLE FRONT DELETION .. Deleted Record: 444 USN NAME DEPT. PHONE 444 Daniel ECE 44444 Choice: 2 Student's details... USN NAME DEPT. PHONE 333 Kate ISE 33333 222 Jack CSE 22222 111 John CSE 11111 No. of STD's: 3 Choice: 5 .. SINGLE BACK INSERTION .. Give record details.. USN NAME DEPT. PHONE 555 Kathy ECE 55555 Choice: 2 Student's details... USN NAME DEPT. PHONE 333 Kate ISE 33333 222 Jack CSE 22222 111 John CSE 11111 555 Kathy ECE 55555 No. of STD's: 4 Choice: 6 .. SINGLE BACK DELETION .. Deleted Record: 555 USN NAME DEPT. PHONE 555 Kathy ECE 55555 Choice: 2 Student's details... USN NAME DEPT. PHONE 333 Kate ISE 33333 222 Jack CSE 22222 111 John CSE 11111 No. of STD's: 3 Choice: 4 .. SINGLE FRONT DELETION .. Deleted Record: 333 USN NAME DEPT. PHONE 333 Kate ISE 33333 Choice: 6 .. SINGLE BACK DELETION .. Deleted Record: 111 USN NAME DEPT. PHONE 111 John CSE 11111 Choice: 2 Student's details... USN NAME DEPT. PHONE 222 Jack CSE 22222 No. of STD's: 1 Choice: 4 .. SINGLE FRONT DELETION .. Deleted Record: 222 USN NAME DEPT. PHONE 222 Jack CSE 22222 Choice: 2 Empty list Choice: 6 Empty list Choice: 7 Exit */

No comments:

Post a Comment

Online Certifications

Python for Beginners Offered by: Christian Drumm, Stephan Jacobs Course dates: 2022-04-05 to 2022-06-01 Topics Python Fundamentals Lists and...