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();
- }
Learn More :
Demonstration
Linked List
- C Program to Implementation of List ADT as linked-list
- Pre Order, Post order, In order Implement Binary Tree using linked list
- C program allocates new nodes and creates a four element list with fixed values
- C Program Simple linked list
- Program to Add Two Polynomials Using Linked List C Program
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list
Number
- Find out the perfect number using c program
- Write a c program to find out H.C.F. of two numbers.
- Check the given number is armstrong number or not using c program.
- Write a c program to find largest among three numbers using conditional operator
- FIND OUT GENERIC ROOT OF A NUMBER - C PROGRAM.
- FIND PRIME FACTORS OF A NUMBER USING C PROGRAM
- How To Write a C program that generates two random numbers ?
- Write a C program to find maximum or equal between two numbers ?
- How to Write a C program to find maximum between two numbers ?
- Write a C program to perform math operations on two input whole numbers. The operations are:
- Write a C program to find maximum between three numbers ?
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program to Enter an ODD number between 1 and 49.
- C program acquires keyboard 10 numbers for each of these numbers to determine if it is a first issue, by printing immediately message should at the end, if none of the numbers you entered was a first issue, print an appropriate message.
- C program generates a random number and uses an algorithm to generate 9 other numbers.
- C Program to Find Random Number
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program to find LCM and HCF Of Two Number Using Recursion - 3
- C Program To Find LCM and HCF Of Two Number -1
- C Program To Find Reverse Of Any Digit Number
- C Program To Find The Frequency Of A Number
- C Program To Print Prime Numbers Upto The Number You Want
- C Program To Print Sum Of n Digit Number
- C Program To Reverse A Number
- C Program To Search A Number Inside The Array