OPERATIONS ON DOUBLY LINKED LIST (DLL)

Exported from Notepad++
/** * @author Dr. Naveen Kumar K R * * Updated on: Jan 5, 2022 */ #include <stdio.h> #include <stdlib.h> typedef struct EmployeeRecord { char SSN[20]; char name[20]; char dept[20]; char salary[20]; }Record; typedef struct Node *ListPointer; typedef struct Node { ListPointer lLink; Record data; ListPointer rLink; }Node; ListPointer head; void menu() { printf("\n.. DOUBLY 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.SSN ); scanf ( "%s", data.name ); scanf ( "%s", data.dept ); scanf ( "%s", data.salary ); 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; temp->lLink = temp->rLink = temp; } return temp; } int length() { ListPointer node; int count = 0; if( head ) { node = head->rLink; do{ count ++; node = node->rLink; } while( node != head); } return count; } void insert(ListPointer targetNode, ListPointer newNode) { newNode->lLink = targetNode; newNode->rLink = targetNode->rLink; targetNode->rLink->lLink = newNode; targetNode->rLink = newNode; } void dDelete ( ListPointer deleted ) { if ( deleted == deleted->rLink ) { printf ( "\n\n No EMPLOYEE records exist\n\n" ); return; } deleted->lLink->rLink = deleted->rLink; deleted->rLink->lLink = deleted->lLink; free ( deleted ); } void addFront ( Record data, ListPointer node ) { if ( !head ) { head = getNode( data ); } insert(head, node); } Record deleteFront ( void ) { ListPointer node; Record data; node = head->rLink; data = node->data; dDelete ( node ); return data; } void addRear ( Record data, ListPointer node ) { if ( !head ) { head = getNode( data ); } insert(head->lLink, node); } Record deleteRear ( void ) { ListPointer node; Record data; node = head->lLink; data = node->data; dDelete ( node ); return data; } void display ( void ) { ListPointer node; printf("\nSSN\tNAME\tDEPT.\tSALARY\n"); node = head->rLink; do{ printf ( "\n%s", node->data.SSN ); printf ( "\t%s", node->data.name ); printf ( "\t%s", node->data.dept ); printf ( "\t%s\n", node->data.salary ); node = node->rLink; } while( node != head ); 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 EMPLOYEEs you \ want to insert: " ); scanf ( "%d", &nRecords ); printf("\nGive %d record details \ one by one\n", nRecords); printf("\nSSN\tNAME\tDEPT.\tSALARY\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 Employee's details...\n"); display ( ); } break; case 3: printf("\n.. SINGLE FRONT INSERTION ..\n"); printf("\nGive record details..\n"); printf("\nSSN\tNAME\tDEPT.\tSALARY\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.SSN ); } break; case 5: printf("\n.. SINGLE BACK INSERTION ..\n"); printf("\nGive record details..\n"); printf("\nSSN\tNAME\tDEPT.\tSALARY\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.SSN ); } break; case 7: return 0; default: printf ( "\nWrong Choice\n" ); } } } /* .. DOUBLY LINKED LIST DEMONSTRATION .. 1. N back Insertion 2. Display 3. Front Insertion 4. Delete Front 5. Rear Insertion 6. Delete Rear 7. Exit Choice: 1 .. * BACK INSERTION .. How many EMP's: 3 Give 3 record details one by one SSN NAME DEPT. SALARY 111 John CSE 11111 222 Jack CSE 22222 333 Kate ISE 33333 Choice: 2 Employee's details... SSN NAME DEPT. SALARY 111 John CSE 11111 222 Jack CSE 22222 333 Kate ISE 33333 No. of EMP's: 3 Choice: 3 .. SINGLE FRONT INSERTION .. Give record details.. SSN NAME DEPT. SALARY 444 Daniel ECE 44444 Choice: 2 Employee's details... SSN NAME DEPT. SALARY 444 Daniel ECE 44444 111 John CSE 11111 222 Jack CSE 22222 333 Kate ISE 33333 No. of EMP's: 4 Choice: 4 .. SINGLE FRONT DELETION .. Deleted Record: 444 SSN NAME DEPT. SALARY 444 Daniel ECE 44444 Choice: 2 Employee's details... SSN NAME DEPT. SALARY 111 John CSE 11111 222 Jack CSE 22222 333 Kate ISE 33333 No. of EMP's: 3 Choice: 5 .. SINGLE BACK INSERTION .. Give record details.. SSN NAME DEPT. SALARY 555 Kathy ECE 55555 Choice: 2 Employee's details... SSN NAME DEPT. SALARY 111 John CSE 11111 222 Jack CSE 22222 333 Kate ISE 33333 555 Kathy ECE 55555 No. of EMP's: 4 Choice: 6 .. SINGLE BACK DELETION .. Deleted Record: 555 SSN NAME DEPT. SALARY 555 Kathy ECE 55555 Choice: 2 Employee's details... SSN NAME DEPT. SALARY 111 John CSE 11111 222 Jack CSE 22222 333 Kate ISE 33333 No. of EMP's: 3 Choice: 4 .. SINGLE FRONT DELETION .. Deleted Record: 111 SSN NAME DEPT. SALARY 111 John CSE 11111 Choice: 6 .. SINGLE BACK DELETION .. Deleted Record: 333 SSN NAME DEPT. SALARY 333 Kate ISE 33333 Choice: 2 Employee's details... SSN NAME DEPT. SALARY 222 Jack CSE 22222 No. of EMP's: 1 Choice: 4 .. SINGLE FRONT DELETION .. Deleted Record: 222 SSN NAME DEPT. SALARY 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...