SlideShare a Scribd company logo
/* Write a program for creation of sorted list from a given list of numbers */




#include <stdio.h>

#include <stdlib.h>

#define NULL 0



struct linked_list

{

          int number;

          struct linked_list *next;

};

typedef struct linked_list node;



main ()

{

          int n;

          node *head = NULL;

          void print(node *p);

          node *insert_Sort(node *p, int n);



          printf("Input the list of numbers.n");

          printf("At end, type -999.n");
scanf("%d",&n);



         while(n != -999)

         {

                  if(head == NULL)        /* create 'base' node */

                  {

                            head = (node *)malloc(sizeof(node));

                            head ->number = n;

                            head->next = NULL;



                  }



                  else                                    /* insert next item */

                  {

                            head = insert_sort(head,n);

                  }

                      scanf("%d", &n);

         }

         printf("n");

         print(head);

         print("n");

}

node *insert_sort(node *list, int x)

{
node *p1, *p2, *p;

     p1 = NULL;

     p2 = list; /* p2 points to first node */



     for( ; p2->number < x ; p2 = p2->next)

     {

              p1 = p2;



              if(p2->next == NULL)

              {

                         p2 = p2->next;                                       /* p2 set to NULL */

                         break;                                /* insert new node at end */

              }

     }



              /* key node found */

              p = (node *)malloc(sizeof(node)); /* space for new node */

              p->number = x;       /* place value in the new node */

              p->next = p2;       /* link new node to key node */

              if (p1 == NULL)

                                                   list = p;      /* new node becomes the first node */

              else

                                                   p1->next = p; /* new node inserted after 1st node
*/

              return (list);
}

void print(node *list)

{

         if (list == NULL)

                   printf("NULL");

         else

         {

                   printf("%d-->",list->number);

                   print(list->next);

         }

         return;

}



Output

Input the list of number.

At end, type -999.

80 70 50 40 60 -999

40-->50-->60-->70-->80 -->NULL

Input the list of number.

At end, type -999.

40 70 50 60 80 -999

40-->50-->60-->70-->80-->NULL
/* Write a function for inserting an item into a linked list */




node *insert(node *head)

{

         node *find(node *p, int a);

         node *new;                       /* pointer to new node */

         node *n1;                     /* pointer to node preceding key node */

         int key;

         int x;                           /* new item (number) to be inserted */



         printf("Value of new item?");

         scanf("%d", &x);

         printf("Value of key item ? (type -999 if last) ");

         scanf("%d", &key);



         if(head->number == key)                       /* new node is first */

         {

             new = (node *)malloc(size of(node));
new->number = x;

                new->next = head;

                head = new;

        }

        else                        /* find key node and insert new node */

        {                                  /* before the key node */

                     n1 = find(head, key); /* find key node */



                     if(n1 == NULL)

                         printf("n key is not found n");

                     else                       /* insert new node */

                     {

                 new = (node *)malloc(sizeof(node));

                 new->number = x;

                 new->next = n1->next;

                 n1->next = new;

                     }

            }

        return(head);

}

node *find(node *lists, int key)

{

                     if(list->next->number == key)                /* key found */

                                return(list);
else



           if(list->next->next == NULL)   /* end */

                    return(NULL);

    else

           find(list->next, key);

}
/* Write a function for deleting an item from linked list */




node *delete(node *head)

{

        node *find(node *p, int a);

        int key;                     /* item to be deleted */

        node *n1;                     /* pointer to node preceding key node */

        node *p;                     /* temporary pointer */

        printf("n What is the item (number) to be deleted?");

        scanf("%d", &key);

        if(head->number == key)               /* first node to be deleted) */

        {

                   p = head->next;                    /* pointer to 2nd node in list */

                   free(head);                   /* release space of key node */

                   head = p;                   /* make head to point to 1st node */
}

    else

    {

              n1 = find(head, key);

              if(n1 == NULL)

                  printf("n key not found n");

              else                                 /* delete key node */

              {

                         p = n1->next->next;            /* pointer to the node

                                                      following the keynode */



                         free(n1->next);                    /* free key node */

                         n1->next = p;                    /* establish link */

              }

    }

    return(head);

}

        /* USE FUNCTION find() HERE */
/* Write a program to create a linear linked list interactively and print out the list and the total number
of items in the list. */




#include             <stdio.h>

#include             <stdlib.h>

#define              NULL 0



struct linked_list

{

           int number;

           struct linked_list *next;

};

typedef struct linked_list node; /* node type defined */



main()
{

        node *head;

        void create(node *p);

        int count(node *p);

        void print(node *p);

        head = (node *)malloc(sizeof(node));

        create(head);

        printf("n");

        printf(head);

        printf("n");

        printf("nNumber of items = %d n", count(head));

}

void create(node *list)

{

        printf("Input a numbern");

        printf("(type -999 at end): ");

        scanf("%d", &list -> number); /* create current node */



        if(list->number == -999)

        {

                 list->next = NULL;

        }



        else            /*create next node */
{

                   list->next = (node *)malloc(sizeof(node));

                   create(list->next); */ Recursion occurs */

         }

         return;

}

void print(node *list)

{

                   if(list->next != NULL)

                   {

                       printf("%d-->",list ->number); /* print current item */



                       if(list->next->next == NULL)

                                 printf("%d", list->next->number);



                       print(list->next);                /* move to next item */

                   }

                   return;

         }



         int count(node *list)

         {

                   if(list->next == NULL)

                          return (0);
else

                         return(1+ count(list->next));

         }



Output



Input a number

(type -999 to end); 60

Input a number

(type -999 to end); 20

Input a number

(type -999 to end); 10

Input a number

(type -999 to end); 40

Input a number

(type -999 to end); 30

Input a number

(type -999 to end); 50

Input a number

(type -999 to end); -999



60 -->20 -->10 -->40 -->30 -->50 --> -999



Number of items = 6

More Related Content

PPTX
Circular linked list
PPTX
Single linked list
PPTX
Double linked list
PDF
Remoção em Lista Simplesmente Encadeada
PPTX
Double linked list
PDF
Data structure circular list
PPT
Link list part 2
PDF
The Ring programming language version 1.6 book - Part 84 of 189
Circular linked list
Single linked list
Double linked list
Remoção em Lista Simplesmente Encadeada
Double linked list
Data structure circular list
Link list part 2
The Ring programming language version 1.6 book - Part 84 of 189

What's hot (20)

PDF
The Ring programming language version 1.4.1 book - Part 22 of 31
PDF
Inserindo em Ordem Crescente na Lista Encadeada
PDF
Understanding storage class using nm
PPTX
Pre zen ta sion
PPTX
งานนำเสนอ อาจารย์ลาวัลย์
PDF
Hello Swift 3/5 - Function
PPT
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
PDF
6. binary tree
PDF
Implementing virtual machines in go & c 2018 redux
DOCX
DataStructures notes
PPT
Collection Core Concept
PDF
The Ring programming language version 1.10 book - Part 96 of 212
PDF
The Ring programming language version 1.5.1 book - Part 77 of 180
PPTX
ภาษาซี
PDF
C++ programs
PDF
2013 - Benjamin Eberlei - Doctrine 2
PDF
Short intro to the Rust language
PPTX
ภาษาซี
DOC
Ds 2 cycle
The Ring programming language version 1.4.1 book - Part 22 of 31
Inserindo em Ordem Crescente na Lista Encadeada
Understanding storage class using nm
Pre zen ta sion
งานนำเสนอ อาจารย์ลาวัลย์
Hello Swift 3/5 - Function
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
6. binary tree
Implementing virtual machines in go & c 2018 redux
DataStructures notes
Collection Core Concept
The Ring programming language version 1.10 book - Part 96 of 212
The Ring programming language version 1.5.1 book - Part 77 of 180
ภาษาซี
C++ programs
2013 - Benjamin Eberlei - Doctrine 2
Short intro to the Rust language
ภาษาซี
Ds 2 cycle
Ad

Similar to week-13x (20)

DOC
Final ds record
PDF
For this homework, you will write a program to create and manipulate.pdf
PDF
Solution#includestdio.h#includeconio.h#includealloc.h.pdf
PDF
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
PPT
Unit7 jwfiles
DOCX
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
PPT
Data structures
PPT
C Language Unit-7
PPT
Linked list1.ppt
PPT
17 linkedlist (1)
PDF
137 Lab-2.2.pdf
PPTX
LINKED LIST.pptx
PPT
Linkedlist
PDF
Program to insert in a sorted list #includestdio.h#include.pdf
DOCX
Implement of c &amp; its coding programming by sarmad baloch
PPTX
Ll.pptx
PPTX
linked list.pptx
PDF
TutorialII_Updated____niceupdateprogram.pdf
PPTX
Binary Search Tree
Final ds record
For this homework, you will write a program to create and manipulate.pdf
Solution#includestdio.h#includeconio.h#includealloc.h.pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
Unit7 jwfiles
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
Data structures
C Language Unit-7
Linked list1.ppt
17 linkedlist (1)
137 Lab-2.2.pdf
LINKED LIST.pptx
Linkedlist
Program to insert in a sorted list #includestdio.h#include.pdf
Implement of c &amp; its coding programming by sarmad baloch
Ll.pptx
linked list.pptx
TutorialII_Updated____niceupdateprogram.pdf
Binary Search Tree
Ad

More from KITE www.kitecolleges.com (20)

Recently uploaded (20)

PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Computing-Curriculum for Schools in Ghana
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Structure & Organelles in detailed.
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
master seminar digital applications in india
PDF
01-Introduction-to-Information-Management.pdf
Weekly quiz Compilation Jan -July 25.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
O7-L3 Supply Chain Operations - ICLT Program
Computing-Curriculum for Schools in Ghana
Supply Chain Operations Speaking Notes -ICLT Program
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Yogi Goddess Pres Conference Studio Updates
Microbial diseases, their pathogenesis and prophylaxis
Cell Structure & Organelles in detailed.
Module 4: Burden of Disease Tutorial Slides S2 2025
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
Orientation - ARALprogram of Deped to the Parents.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Anesthesia in Laparoscopic Surgery in India
master seminar digital applications in india
01-Introduction-to-Information-Management.pdf

week-13x

  • 1. /* Write a program for creation of sorted list from a given list of numbers */ #include <stdio.h> #include <stdlib.h> #define NULL 0 struct linked_list { int number; struct linked_list *next; }; typedef struct linked_list node; main () { int n; node *head = NULL; void print(node *p); node *insert_Sort(node *p, int n); printf("Input the list of numbers.n"); printf("At end, type -999.n");
  • 2. scanf("%d",&n); while(n != -999) { if(head == NULL) /* create 'base' node */ { head = (node *)malloc(sizeof(node)); head ->number = n; head->next = NULL; } else /* insert next item */ { head = insert_sort(head,n); } scanf("%d", &n); } printf("n"); print(head); print("n"); } node *insert_sort(node *list, int x) {
  • 3. node *p1, *p2, *p; p1 = NULL; p2 = list; /* p2 points to first node */ for( ; p2->number < x ; p2 = p2->next) { p1 = p2; if(p2->next == NULL) { p2 = p2->next; /* p2 set to NULL */ break; /* insert new node at end */ } } /* key node found */ p = (node *)malloc(sizeof(node)); /* space for new node */ p->number = x; /* place value in the new node */ p->next = p2; /* link new node to key node */ if (p1 == NULL) list = p; /* new node becomes the first node */ else p1->next = p; /* new node inserted after 1st node */ return (list);
  • 4. } void print(node *list) { if (list == NULL) printf("NULL"); else { printf("%d-->",list->number); print(list->next); } return; } Output Input the list of number. At end, type -999. 80 70 50 40 60 -999 40-->50-->60-->70-->80 -->NULL Input the list of number. At end, type -999. 40 70 50 60 80 -999 40-->50-->60-->70-->80-->NULL
  • 5. /* Write a function for inserting an item into a linked list */ node *insert(node *head) { node *find(node *p, int a); node *new; /* pointer to new node */ node *n1; /* pointer to node preceding key node */ int key; int x; /* new item (number) to be inserted */ printf("Value of new item?"); scanf("%d", &x); printf("Value of key item ? (type -999 if last) "); scanf("%d", &key); if(head->number == key) /* new node is first */ { new = (node *)malloc(size of(node));
  • 6. new->number = x; new->next = head; head = new; } else /* find key node and insert new node */ { /* before the key node */ n1 = find(head, key); /* find key node */ if(n1 == NULL) printf("n key is not found n"); else /* insert new node */ { new = (node *)malloc(sizeof(node)); new->number = x; new->next = n1->next; n1->next = new; } } return(head); } node *find(node *lists, int key) { if(list->next->number == key) /* key found */ return(list);
  • 7. else if(list->next->next == NULL) /* end */ return(NULL); else find(list->next, key); }
  • 8. /* Write a function for deleting an item from linked list */ node *delete(node *head) { node *find(node *p, int a); int key; /* item to be deleted */ node *n1; /* pointer to node preceding key node */ node *p; /* temporary pointer */ printf("n What is the item (number) to be deleted?"); scanf("%d", &key); if(head->number == key) /* first node to be deleted) */ { p = head->next; /* pointer to 2nd node in list */ free(head); /* release space of key node */ head = p; /* make head to point to 1st node */
  • 9. } else { n1 = find(head, key); if(n1 == NULL) printf("n key not found n"); else /* delete key node */ { p = n1->next->next; /* pointer to the node following the keynode */ free(n1->next); /* free key node */ n1->next = p; /* establish link */ } } return(head); } /* USE FUNCTION find() HERE */
  • 10. /* Write a program to create a linear linked list interactively and print out the list and the total number of items in the list. */ #include <stdio.h> #include <stdlib.h> #define NULL 0 struct linked_list { int number; struct linked_list *next; }; typedef struct linked_list node; /* node type defined */ main()
  • 11. { node *head; void create(node *p); int count(node *p); void print(node *p); head = (node *)malloc(sizeof(node)); create(head); printf("n"); printf(head); printf("n"); printf("nNumber of items = %d n", count(head)); } void create(node *list) { printf("Input a numbern"); printf("(type -999 at end): "); scanf("%d", &list -> number); /* create current node */ if(list->number == -999) { list->next = NULL; } else /*create next node */
  • 12. { list->next = (node *)malloc(sizeof(node)); create(list->next); */ Recursion occurs */ } return; } void print(node *list) { if(list->next != NULL) { printf("%d-->",list ->number); /* print current item */ if(list->next->next == NULL) printf("%d", list->next->number); print(list->next); /* move to next item */ } return; } int count(node *list) { if(list->next == NULL) return (0);
  • 13. else return(1+ count(list->next)); } Output Input a number (type -999 to end); 60 Input a number (type -999 to end); 20 Input a number (type -999 to end); 10 Input a number (type -999 to end); 40 Input a number (type -999 to end); 30 Input a number (type -999 to end); 50 Input a number (type -999 to end); -999 60 -->20 -->10 -->40 -->30 -->50 --> -999 Number of items = 6