How to write a C Program to Demonstrates a linked list for numbers in C Programming Language ?
This Program Demonstrates a linked list for numbers.
Solution:
- /*Demonstrates a linked list for numbers.*/
- #include "list-0.h" // contain struct for the linked list
- #include <stdio.h>
- #include <cs50.h> // special library for using getint to get numper from user insead of scanf
- #include <stdlib.h> // for malloc and free functions
- #include <unistd.h> // for sleep function
- //node to determine the beginning of the list.
- node* first = NULL;
- // prototypes
- void delete(void);
- void insert(void);
- void search(void);
- void traverse(void);
- int main(void)
- {
- int c; //the numper of menue
- do
- {
- printf("\nmenu\n"
- "1-delete\n"
- "2-insert\n"
- "3-search\n"
- "4-traverse\n"
- "0-quit\n");
- // get the command
- printf("command: ");
- c = GetInt();
- // execute the command
- switch(c)
- {
- case 1: delete(); break;
- case 2: insert(); break;
- case 3: search(); break;
- case 4: traverse(); break;
- }
- }
- while( c != 0);
- // clear the hole list before exiting
- node* ptr = first;
- while ( ptr != NULL)
- {
- node* predptr = ptr;
- ptr = ptr->next;
- free(predptr);
- }
- return 0;
- }
- // searching a numper in the list
- void search(void)
- {
- printf("the numper please: ");
- int n = GetInt();
- // insitate a node for the search at the beginnig
- node* ptr = first;
- //continue searching until reach the end of the list
- while ( ptr != NULL)
- {
- if( ptr->n == n)
- {
- printf("founded\n");
- break;
- }
- else
- // update the pounter
- ptr = ptr-> next;
- }
- //if reached to the end of list
- if (ptr == NULL)
- printf("not founded\n");
- }
- void delete(void)
- {
- printf("the numper please: ");
- int n = GetInt();
- // insitate a pointer at the beginning of the list
- node* ptr = first;
- //insitate to rememper the previous node in the list before deleting
- node* predptr = NULL;
- while( ptr != NULL)
- {
- if( ptr-> n == n)
- {
- // deleting the first node
- if( ptr == first)
- {
- first = ptr-> next;
- free(ptr);
- }
- //deleting from the middle or tail
- else
- {
- predptr->next = ptr->next;
- free(ptr);
- }
- break;
- }
- // update the pointers to the next of each
- else
- {
- predptr = ptr;
- ptr = ptr-> next;
- }
- }
- // make the traverse process to print the list out
- traverse();
- }
- void traverse(void)
- {
- node* ptr = first;
- //print all the list numbers
- printf("the list is: ");
- while ( ptr!= NULL)
- {
- printf("%i ", ptr->n);
- ptr = ptr->next;
- }
- // flush standard output since we haven't outputted any newlines yet
- fflush(stdout);
- // pause before continuing
- sleep(1);
- printf("\n\n");
- }
- // the hardest process :)..
- void insert(void)
- {
- // allocate memory for new insert strcuct
- node* insert = malloc(sizeof(node));
- if(insert == NULL)
- {
- printf("cann't allocated..!!\n");
- return;
- }
- // fill the node
- printf("\nthe numper please: ");
- insert->n = GetInt();
- insert->next = NULL;
- // insert at the beginning if the list empty
- if( first == NULL)
- first = insert;
- // if it is lower than the first numper
- else if ( insert->n < first->n )
- {
- insert->next = first;
- first = insert;
- }
- else
- {
- // to terverse the list for comparing the numpers
- node* ptr = first;
- while (true)
- {
- // insert in in the last if it is the beggist numper
- // must be before the down comparing or will generate "segemntaion fault"
- if ( ptr->next == NULL)
- {
- ptr->next = insert;
- break;
- }
- // attention please : ptr->next->n
- //at 'c' compare the next numper at 'e' to the inserting
- else if( insert-> n < ptr->next->n )
- {
- insert-> next = ptr->next;
- ptr->next = insert;
- break;
- }
- // always update the ptr to reverse the list until we insert the item
- ptr = ptr->next;
- }
- }
- // make the traverse process to print the list out
- traverse();
- }