Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- lets_learn_single_linked_list_with_int_v1.c
- SSL - Single Linked List
- With global variables.
- With the meny.
- https://en.wikipedia.org/wiki/Linked_list
- https://www.geeksforgeeks.org/linked-list-set-1-introduction/
- https://www.geeksforgeeks.org/data-structures/linked-list/singly-linked-list/
- https://www.c-lang.thiyagaraaj.com/data-structures/linked-list/singly-linked-list-example-program-in-c
- https://www.sanfoundry.com/c-program-implement-singly-linked-list/
- https://www.codesdope.com/blog/article/linked-lists-in-c-singly-linked-list/
- https://www.thecrazyprogrammer.com/2013/09/menu-driven-c-program-to-perform-insert.html
- https://www.learn-c.org/en/Linked_lists
- https://www.geeksforgeeks.org/generic-linked-list-in-c-2/
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- https://www.facebook.com/dmilicev
- */
- #include <stdio.h>
- #include <stdlib.h> // for malloc(), calloc(), realloc(), free()
- typedef struct node{
- int data;
- struct node *next;
- } Node;
- Node *head, *newNode, *cur;
- // declare three global nodes:
- // head - start node, list start
- // newNode - auxiliary node, to create new nodes
- // cur - the current node, for scroll through the list
- /*
- struct node {
- int data;
- struct node* next; // now the data type is struct node
- };
- typedef struct node NODE; // now the data type instead of struct node becomes only NODE
- typedef NODE* PNODE; // PNODE is pointer to NODE
- */
- // Returns 1 if the list is empty, otherwise returns 0.
- int IsListEmpty(void)
- {
- if( head == NULL ){ // if there are no members in the list ie if the list is empty
- printf(" List is empty.\n\n");
- return 1;
- }
- else
- return 0;
- } // IsListEmpty()
- /*
- Scroll through the list:
- When working with lists, it is often necessary to scroll through the list, from element to element.
- Most often it is about processing the data in the list, searching the list to find it
- the corresponding element or search for the end of the list.
- In all these cases the algorithm is similar.
- A helper pointer cur is introduced that is initially equal to the list's head,
- i.e. points to the first element of the list if it is not empty.
- After verifying that the pointer cur points to an element (has a value other than NULL),
- it processes the data in that element (print, compare, calculate ...).
- Upon completion of the data processing, the auxiliary pointer cur
- gets the value of the pointer to the next element,
- and the whole process is repeated until the helper pointer cur has a value other than NULL,
- that is, while pointing to an element.
- When the helper pointer cur gets NULL, it means we have reached the end of the list.
- */
- // Goes through the entire list and counts the list elements.
- int count_list_elements()
- {
- int counter=0;
- cur = head; // the current element is mounted on the head
- while( cur ){ // for all elements of the list (until the cur is NULL ie last)
- counter++; // increase the number of elements of the list
- cur = cur->next; // move on to the next element of the list
- }
- return counter;
- } // count_list_elements()
- // Create a new node, the entry is done in node newNode
- void new_node(){
- newNode = (Node*)malloc( sizeof(Node) ); // reserve memory for node newNode
- if( newNode == NULL ) {
- printf("\n Memory allocation error! \n");
- exit(1);
- }
- printf("\n\n Enter newNode->data : "); // enter newNode->data ie the contents of the newNode node
- scanf("%d", &newNode->data); // check for integer input here !!!
- newNode->next = NULL; // newNode is the new node and points to NULL for now
- } // new_node()
- // Function to add a node at the beginning of linked list
- void add_at_begin(){
- new_node(); // enter new node in newNode
- if( head == NULL ){ // if there are no members in the list ie if the list is empty
- head=newNode; // then the head is a new member (already pointing to NULL)
- }
- else{ // if there are members in the list ie if the list is not empty
- newNode->next=head; // newNode points to old head
- head=newNode; // head becomes new member
- }
- } // add_at_begin()
- // Displays the entire list, each list member in a new row,
- // and at the same time counts the list members.
- // For each node it displays the contents and the pointer to the next one.
- // Returns the number of members of the list.
- int print_list_with_pointers(){
- int counter=0; // serves for counting list members
- printf("\n\n");
- if( head == NULL ) // if there are no members in the list ie if the list is empty
- printf(" List is empty.\n\n");
- else{ // if there are members in the list ie if the list is not empty
- cur = head; // the current member is mounted on the head
- printf("\t\t | cur->data - cur->next | \n\n"); // display the list header
- while( cur ){ // for all members of the list (until the cur is NULL ie last)
- counter++; // increase the number of members of the list
- printf("\n \t %2d. \t | %5d - %p | \n",
- counter, cur->data, cur->next ); // display the list member
- cur = cur->next; // move on to the next member of the list
- } // while( cur )
- } // else // if there are members in the list ie if the list is not empty
- //printf("\n\n List has %d elements. \n\n", counter );
- return( counter );
- } // print_list_with_pointers()
- // Displays only the contents of nodes in one row and
- // counts the members of the list at the same time.
- // Returns the number of members of the list.
- int print_list(void){
- int counter=0; // serves for counting list members
- printf("\n\n");
- if( head == NULL ) // if there are no members in the list ie if the list is empty
- printf(" List is empty. \n\n");
- else{ // if there are members in the list ie if the list is not empty
- cur = head; // the current member is mounted on the head
- while( cur ){ // for all members of the list (until the girl is NULL ie last)
- counter++; // increase the number of members of the list
- printf("%4d", cur->data ); // display the contents of the current node
- cur = cur->next; // move on to the next member of the list
- }
- }
- //printf("\n\n\n List has %d elements. \n\n", counter );
- printf("\n\n");
- return( counter );
- } // print_list()
- // Insert newNode after node whose contents are x
- int insert_after_x(){
- int x;
- printf("\n\n");
- printf("\n\n Enter the newNode after the node that has value, x = ");
- scanf("%d", &x);
- new_node(); // enter new node in newNode
- if( head == NULL ){ // if there are no members in the list
- printf("\n\n List is empty !!! \n\n");
- system("PAUSE");
- return(0);
- }
- else{ // if there are members in the list
- cur = head; // let's get to the first member of the list
- // to the end of the list or to a member whose contents are x
- while( cur->data != x && cur->next != NULL ){
- cur = cur->next; // go to next node
- }
- // now we are on a node whose content is x
- // he was pointing to his next member
- // and now it should point to newNode
- // a newNode should point to the member to which node x was pointing so far
- // or, more simply:
- // x was pointing to the cur
- // now x should point to newNode
- // a newNode should point to cur
- newNode->next = cur->next;
- cur->next = newNode;
- }
- } // insert_after_x()
- // Add a newNode to the end of the list
- void add_to_end(){
- new_node(); // enter new node in newNode
- if( head == NULL ){ // if there are no members in the list ie if the list is empty
- head=newNode; // then that newly entered member newNode becomes the start of the list (head)
- } // (and is also the end of the list because it is the only one)
- else{ // if there are members in the list ie if the list is not empty
- cur = head; // let's get to the first member of the list
- while( cur->next != NULL ){ // until the last member of the list is reached
- cur = cur->next; // move on to the next member of the list
- }
- // now the current member (cur) is the last member of the list,
- cur->next = newNode; // and it should show the newly entered node newNode
- }
- } // add_to_end()
- // Delete the initial (first) node in the list, delete the head
- void delete_first(){
- if( head == NULL ){ // if there are no members in the list ie if the list is empty
- printf("\n\n List is empty! Unable to delete! \n\n");
- system("PAUSE");
- }
- else{ // if there are members in the list ie if the list is not empty
- newNode = head; // the initial member head is memorized into the auxiliary member newNode due to free memory
- head = head->next; // the second member becomes the first, ie the newNode starting member head becomes his (former) next member
- free(newNode); // free up memory that was reserved for helper member newNode
- } // (this is where we remembered the former initial head member)
- } // delete_first()
- // Delete the last node in the list
- void delete_last(){
- if( head == NULL ) { // if there are no members in the list ie if the list is empty
- printf("\n\n List is empty! Unable to delete! \n\n");
- system("PAUSE");
- }
- else{ // if there are members in the list ie if the list is not empty
- cur=head; // let's get to the first member of the list
- if(cur->next != NULL){ // if next member exists
- while(cur->next->next != NULL){ // to the penultimate member
- cur=cur->next; // go to next member
- }
- // now the current member (cur) is the penultimate member of the list
- newNode = cur->next;// in newNode we remember the address of the last member of the list
- // due to free memory (we delete it)
- cur->next = NULL; // penultimate member cur has no following and therefore points to NULL
- free(newNode); // free up the memory occupied by the former last member of the list
- } // because we deleted it and we don't need it anymore
- else{ // if there is no next member, ie cur is the only member in the list, both first and last
- delete_first(); // then delete that single member of the list
- }
- }
- } // delete_last()
- // Deletes all the nodes of the list and frees up the memory that the nodes occupy
- void delete_all_nodes()
- {
- while( ( cur = head ) != NULL ){ // while the head exists, we delete the head
- newNode = head; // the initial member head is memorized into the auxiliary member newNode due to free memory
- head = head->next; // the second member becomes the first, ie the newNode starting member head becomes his (former) next member
- free(newNode); // free up memory that was reserved for helper member newNode
- }
- } // delete_all_nodes()
- /*
- To delete an item from a list:
- To delete a particular element x from a list, we need to know its position in the list.
- We move through the list until the liquid element is the one we delete (cur-> data = x).
- We also need to know the pointer to the previous element in the list in order to find its link
- assigned a pointer to the element following the element we are deleting.
- After that we can delete the required element, that is, free up the memory it occupies.
- */
- // Deletes the first node that has the contents of x and frees the memory occupied by that node.
- // Returns NumberOfDeleted nodes.
- int delete_first_with_value_x( int x )
- {
- int NumberOfDeleted=0;
- if( head == NULL ) { // if there are no members in the list ie if the list is empty
- printf("\n\n Lista je prazna! Nemoguce brisanje!!! \n\n");
- system("PAUSE");
- }
- else { // if there are members in the list ie if the list is not empty
- cur = head; // let's get to the first member of the list
- if( cur->data == x ) { // if the contents of the head is x
- delete_first();
- }
- else { // if the contents of the head isn't x
- // to the end of the list or to a member whose contents are x
- while( cur->data != x && cur->next != NULL ){
- printf("\n\n while cur->data = %2d \n\n", cur->data );
- newNode = cur; // in newNode we remember the previous of cur
- cur = cur->next;// go to next node
- }
- // now is cur node whose value is x, newNode is previous from cur
- if( cur->data == x ){ // if there is a node whose content is x
- newNode->next = cur->next; // previous of cur points to next of cur (because cur is deleted)
- newNode = cur; // in newNode we remember the cur for deleting and for freeing memory (deleting it)
- free(newNode); // free up the memory occupied by the former last member of the list
- NumberOfDeleted++;
- }
- else {
- printf("\n\n There is no node in the list with contents %2d ! \n\n", x );
- system("PAUSE");
- }
- } // if the contents of the head isn't x
- } // if there are members in the list ie if the list is not empty
- return NumberOfDeleted;
- } // delete_first_with_value_x()
- // Deletes all nodes that have x content and frees up the memory these nodes occupy.
- // Returns NumberOfDeleted nodes.
- int delete_all_with_value_x( int x ) {
- Node *previous, *current;
- int NumberOfDeleted=0;
- if( head == NULL ) { // if there are no members in the list ie if the list is empty
- printf("\n\n The list is empty! Impossible to delete! \n\n");
- system("PAUSE");
- }
- else { // if there are members in the list ie if the list is not empty
- current = head; // let's get to the first member of the list
- previous = NULL;
- while( current != NULL ){ // for all members of the list (until the girl is NULL ie last)
- previous = current;
- current = current->next;
- if ( current->data == x ){
- current = current->next;
- previous->next = current;
- NumberOfDeleted++;
- }
- }
- } // if there are members in the list ie if the list is not empty
- if( NumberOfDeleted == 0 ){
- printf("\n\n There is no node in the list with contents %2d ! \n\n", x );
- system("PAUSE");
- }
- return NumberOfDeleted;
- } // delete_all_with_value_x()
- // Delete an node by ordinal number in the list
- Node *delete_node_with_ordinal_number_k( Node *p, int ordinal_number )
- {
- Node *previous, *current;
- int i;
- if (p == NULL ){ // if the list is empty
- printf("List is empty. \n");
- system("PAUSE");
- }
- else // if the list is not empty
- {
- if ( ordinal_number > count_list_elements(p) ){
- printf("\n\n Ordinal number %d is greater than the number of list elements %d ! \n\n",
- ordinal_number, count_list_elements(p) );
- system("PAUSE");
- }
- else{
- previous = NULL;
- current = p;
- i = 1;
- while ( i < ordinal_number ){
- previous = current;
- current = current->next;
- i = i+1;
- }
- if ( previous == NULL ){
- p = current->next;
- free( current );
- }
- else{
- previous->next = current->next;
- free( current );
- }
- } // else // ( ordinal_number <= count_list_elements(p) )
- } // else // if the list is not empty
- return( p );
- } // delete_node_with_ordinal_number_k()
- /*
- Sort the list in ascending order:
- To sort the list, we first scroll through it to find the element
- with the lowest data value. We then remove that element from the list i
- we add it to the end of the second list, which was initially blank.
- We repeat this process until the initial list is empty.
- In the end, all that remains is for the function to return a cursor
- to a list in which all the elements have been moved.
- */
- // Sorts the list in ascending order
- Node *sort_list_in_ascending_order( Node *p )
- {
- Node *auxiliary1, *auxiliary2, *min, *previous, *q;
- q = NULL; // new auxiliary list
- while( p != NULL ) // to the end of the list
- {
- previous = NULL;
- min = auxiliary1 = p; // assume that the smallest is the first element (head)
- auxiliary2 = p->next;
- while ( auxiliary2 != NULL )
- {
- if( auxiliary2->data < min->data )
- {
- min = auxiliary2;
- previous = auxiliary1;
- }
- auxiliary1 = auxiliary2;
- auxiliary2 = auxiliary2->next;
- }
- if(previous == NULL)
- p = min->next;
- else
- previous->next = min->next;
- min->next = NULL;
- if( q == NULL )
- q = min; // moves the element with the least value of data from list p to the beginning of list q
- else
- {
- auxiliary1 = q;
- // goes through list q to find its last element
- while( auxiliary1->next != NULL )
- auxiliary1 = auxiliary1->next;
- // moves the element with the lowest data value from list p to the end of list q
- auxiliary1->next = min;
- }
- }
- return ( q );
- } // sort_list_in_ascending_order()
- /*
- Sort the list in descending order:
- To sort the list, we first scroll through it to find the element
- with the highest data value. We then remove that element from the list i
- we add it to the beginning of the second list, which was initially blank.
- We repeat this process until the initial list is empty.
- In the end, all that remains is for the function to return a cursor
- to a list in which all the elements have been moved.
- */
- // Sorts the list in descending order
- Node *sort_list_in_descending_order( Node *p )
- {
- Node *auxiliary1, *auxiliary2, *max, *previous, *q;
- q = NULL; // new auxiliary list
- while( p != NULL ) // to the end of the list
- {
- previous = NULL;
- max = auxiliary1 = p; // assume that the largest is the first element (head)
- auxiliary2 = p->next;
- while ( auxiliary2 != NULL )
- {
- if( auxiliary2->data > max->data )
- {
- max = auxiliary2;
- previous = auxiliary1;
- }
- auxiliary1 = auxiliary2;
- auxiliary2 = auxiliary2->next;
- }
- if( previous == NULL )
- p = max->next;
- else
- previous->next = max->next;
- max->next = NULL;
- if( q == NULL )
- q = max; // moves the element with the highest data value from list p to the beginning of list q
- else
- {
- auxiliary1 = q;
- // goes through list q to find its last element
- while( auxiliary1->next != NULL )
- auxiliary1 = auxiliary1->next;
- // moves the element with the highest data value from list p to the end of list q
- auxiliary1->next = max;
- }
- }
- return ( q );
- } // sort_list_in_descending_order()
- // Creates a list of 10 nodes to avoid individually entering nodes (list elements)
- void make_list_of_n_int( int n )
- {
- int i;
- delete_all_nodes(); // Deletes all the nodes of the list and frees up the memory that the nodes occupy
- // next if needed only if we don't call delete_all_nodes() first
- if( head == NULL ){ // if there are no members in the list ie if the list is empty
- for( i=1; i<=n; i++ ){
- newNode=(Node*)malloc(sizeof(Node)); // reserve memory for newNode
- if( newNode == NULL ) {
- printf("\n Memory allocation error! \n");
- exit(2);
- }
- newNode->data = i;
- newNode->next = NULL; // newNode is the new node and points to NULL for now
- // add the formed i-th node to the end of the list
- if( head == NULL ){ // if there are no members in the list ie if the list is empty
- head=newNode; // then that newly entered member newNode becomes the start of the list (head)
- } // (and is also the end of the list because it is the only one)
- else{ // if there are members in the list ie if the list is not empty
- cur=head; // let's get to the first member of the list
- while(cur->next!=NULL){ // until the last member of the list is reached
- cur=cur->next; // move on to the next member of the list
- }
- // now the current member (cur) is the last member of the list,
- cur->next=newNode; // and he should point to the newly added node newNode
- } // if( head == NULL )
- } // for( i=1; i<=n; i++ )
- } // if( head == NULL )
- else {
- printf("\n\n The list is not empty. Delete all members of the list first! \n\n");
- system("PAUSE");
- }
- } // make_list_of_n_int()
- /*
- We move around the list from head to right.
- First is the current element head and the previous element is NULL.
- Then, at the end of the list, we repeat:
- next = current-> next; // next is the one shown by current
- current-> next = previous; // current points to the previous one
- previous = current; // previous becomes current
- current = next; // current becomes next (go to next element)
- Rotate the list order:
- In order to rotate the order in the list, it is necessary that
- for each element we know the pointers to its predecessor and follower.
- We then declare the current element a precursor,
- and his follower for current.
- */
- // Reverse the list node order
- Node *reverse_order( Node *p )
- {
- Node *previous, *current, *next;
- current = p; // current is head, the first element in the list
- previous = NULL; // initial value for previous
- while( current != NULL ) // from head to end of list
- {
- next = current->next; // next is the one shown by current
- current->next = previous; // current now points to the previous one
- previous = current; // previous becomes current
- current = next; // current becomes next (go to next element)
- }
- return previous; // returns previous because it is now the head (first element) of the list
- } // reverse_order()
- /*
- To insert a new element into a ascending (growing) sorted list:
- To insert a new element into a previously sorted sheet,
- we compare in sequence the data in the list with the data value of the newNode element.
- This process is repeated until we get a pointer to the element
- which is in front of an element whose data is larger than the data in the newNode element.
- */
- // Inserts a new element into the ascending (growing) sorted list
- Node *insert_in_ascending_sorted_list( Node *p, int x )
- {
- Node *current, *previous, *auxilary;
- current = p;
- previous = NULL;
- while( current->data < x )
- {
- previous = current;
- if ( current->next != NULL )
- current = current->next;
- else
- break;
- }
- auxilary = (Node *) malloc( sizeof(Node) );
- if( auxilary == NULL )
- {
- printf("\n Memory allocation error! \n");
- exit(3);
- }
- if ( previous == NULL ) // the element x will be inserted at the beginning of the list
- {
- auxilary->data = x;
- auxilary->next = p;
- p = auxilary;
- }
- else
- {
- auxilary->data = x;
- auxilary->next = previous->next;
- previous->next = auxilary;
- }
- return( p );
- } // insert_in_ascending_sorted_list
- /*
- Inserting a new element in a descending sorted list:
- To insert a new element into a previously sorted list,
- we compare in sequence the data in the list with the data value of the newNode element.
- This process is repeated until we get a pointer to the element
- which is behind an element whose data is larger than the data in the newNode element.
- */
- // Inserts a new element into the descending sorted list
- Node *insert_in_descending_sorted_list( Node *p, int x )
- {
- Node *current, *previous, *auxilary;
- current = p;
- previous = NULL;
- while( current->data > x )
- {
- previous = current;
- if ( current->next != NULL )
- current = current->next;
- else
- break;
- }
- auxilary = (Node *) malloc( sizeof(Node) );
- if( auxilary == NULL )
- {
- printf("Memory allocation error! \n");
- exit(4);
- }
- if ( previous == NULL ) // the element x will be inserted at the beginning of the list
- {
- auxilary->data = x;
- auxilary->next = p;
- p = auxilary;
- }
- else
- {
- auxilary->data = x;
- auxilary->next = previous->next;
- previous->next = auxilary;
- }
- return( p );
- } // insert_in_descending_sorted_list()
- // Meny for node sorting
- int node_sorting_meny(void)
- {
- int choice, x, y;
- while(1){
- system("CLS");
- print_list();
- printf("\n\n +-------------------------------------------------------+ \n");
- printf(" | | \n");
- printf(" | SORTING THE LIST that has %2d nodes \n", count_list_elements() );
- printf(" | | \n");
- printf(" +-------------------------------------------------------+ \n");
- printf(" | | \n");
- printf(" | 1 - Sort the list in ascending order | \n");
- printf(" | | \n");
- printf(" | 2 - Sort the list in descending order | \n");
- printf(" | | \n");
- printf(" | 3 - Inserting a new node into ascending sorted list | \n");
- printf(" | | \n");
- printf(" | 4 - Inserting a new node into descending sorted list | \n");
- printf(" | | \n");
- printf(" | 0 - Exit | \n");
- printf(" | | \n");
- printf(" +-------------------------------------------------------+ \n");
- printf("\n\t Choice (0-4): ");
- scanf("%d", &choice);
- switch(choice){
- case 1: // Sort the list in ascending order
- head = sort_list_in_ascending_order( head );
- break;
- case 2: // Sort the list in descending order
- head = sort_list_in_descending_order( head );
- break;
- case 3: // Inserting a new node into ascending sorted list
- printf("\n\n Enter the value of the new element: x = ");
- scanf("%d",&x);
- head = insert_in_ascending_sorted_list( head, x );
- break;
- case 4: // Inserting a new node into descending sorted list
- printf("\n\n Enter the value of the new element: x = ");
- scanf("%d",&x);
- head = insert_in_descending_sorted_list( head, x );
- break;
- case 0: // Exit
- return 0;
- break;
- default :
- printf("\n\n \t You have to choose (0-4) ! \n\n");
- break;
- } // switch(choice)
- } // while(1)
- } // node_sorting_meny()
- // Meny for deleting nodes
- int node_deleting_meny(void)
- {
- int choice, x, y;
- while(1){
- system("CLS");
- print_list();
- printf("\n\n +-------------------------------------------------------+ \n");
- printf(" | | \n");
- printf(" | DELETE NODES OF LIST that has %2d elements \n", count_list_elements() );
- printf(" | | \n");
- printf(" +-------------------------------------------------------+ \n");
- printf(" | | \n");
- printf(" | 1 - full list (all nodes in the list) | \n");
- printf(" | | \n");
- printf(" | 2 - head (first in the list) | \n");
- printf(" | | \n");
- printf(" | 3 - tail (last in the list) | \n");
- printf(" | | \n");
- printf(" | 4 - first with value x | \n");
- printf(" | | \n");
- printf(" | 5 - first k with the value x | \n");
- printf(" | | \n");
- printf(" | 6 - all with the value x | \n");
- printf(" | | \n");
- printf(" | 7 - all bigger than x | \n");
- printf(" | | \n");
- printf(" | 8 - all less than x | \n");
- printf(" | | \n");
- printf(" | 9 - all in interval [x,y] | \n");
- printf(" | | \n");
- printf(" | 10 - element with order number k | \n");
- printf(" | | \n");
- printf(" | 11 - all even | \n");
- printf(" | | \n");
- printf(" | 12 - all odd | \n");
- printf(" | | \n");
- printf(" | 0 - Exit | \n");
- printf(" | | \n");
- printf(" +-------------------------------------------------------+ \n");
- printf("\n\t Choice (0-12): ");
- scanf("%d", &choice);
- switch(choice){
- case 1: // full list (all nodes in the list)
- delete_all_nodes();
- break;
- case 2: // head (first in the list)
- delete_first();
- break;
- case 3: // tail (last in the list)
- delete_last();
- break;
- case 4: // first with value x
- printf("\n\n Enter the value of the node whose first occurrence you are deleting: ");
- scanf("%d", &x );
- delete_first_with_value_x( x );
- break;
- case 5: // first k with the value x
- break;
- case 6: // all with the value x
- printf("\n\n Enter the value of the node whose all occurrences will be deleted: ");
- scanf("%d", &x );
- delete_all_with_value_x( x );
- break;
- case 7: // all bigger than x
- break;
- case 8: // all less than x
- break;
- case 9: // all in interval [x,y]
- break;
- case 10: // element with order number k
- printf("\n\n Enter the number of the item you are deleting, k = ");
- scanf ("%d",&x);
- head = delete_node_with_ordinal_number_k( head , x );
- break;
- case 11: // all even
- break;
- case 12: // all odd
- break;
- case 0: // Exit
- return 0;
- break;
- default :
- printf("\n\n \t You have to choose (0-12) ! \n\n");
- break;
- } // switch(choice)
- } // while(1)
- } // node_deleting_meny()
- // Main meny
- int main(void)
- {
- int choice, x;
- make_list_of_n_int(10);
- while(1){
- system("CLS");
- //print_list_with_pointers();
- print_list();
- printf("\n\n +-------------------------------------------------------+ \n");
- printf(" | | \n");
- printf(" | WORK WITH LIST that has %2d elements \n", count_list_elements() );
- printf(" | | \n");
- printf(" +-------------------------------------------------------+ \n");
- printf(" | | \n");
- printf(" | 1 - New node at the beginning of the list | \n");
- printf(" | | \n");
- printf(" | 2 - New node after node whose value is x | \n");
- printf(" | | \n");
- printf(" | 3 - New node at the end of the list | \n");
- printf(" | | \n");
- printf(" | 4 - Delete nodes ( meny ) | \n");
- printf(" | | \n");
- printf(" | 5 - Reverse the list node order | \n");
- printf(" | | \n");
- printf(" | 6 - Sorting nodes ( meny ) | \n");
- printf(" | | \n");
- printf(" | 7 - Make a new list | \n");
- printf(" | | \n");
- printf(" | 0 - End of work | \n");
- printf(" | | \n");
- printf(" +-------------------------------------------------------+ \n");
- printf("\n\t Choice: (0-7): ");
- scanf("%d", &choice);
- switch(choice){
- case 1: add_at_begin(); // New node at the beginning of the list
- break;
- case 2: insert_after_x(); // New node after node whose value is x
- break;
- case 3: add_to_end(); // New node at the end of the list
- break;
- case 4: node_deleting_meny(); // Delete nodes ( meny )
- break;
- case 5: head = reverse_order( head ); // Reverse the list node order
- break;
- case 6: node_sorting_meny(); // Sorting nodes ( meny )
- break;
- case 7: make_list_of_n_int(10); // Make a new list
- break;
- case 0: // End of work
- return(0);
- break;
- default :
- printf("\n\n \t Morate izabrati (0-7) ! \n\n");
- break;
- } // switch(choice)
- } // while(1)
- printf("\n\n");
- return 0;
- } // main())
Add Comment
Please, Sign In to add comment