SlideShare uma empresa Scribd logo
1 de 13
/* 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

Mais conteúdo relacionado

Mais procurados

DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
kramsri
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
kramsri
 

Mais procurados (20)

week-16x
week-16xweek-16x
week-16x
 
The Ring programming language version 1.4.1 book - Part 22 of 31
The Ring programming language version 1.4.1 book - Part 22 of 31The Ring programming language version 1.4.1 book - Part 22 of 31
The Ring programming language version 1.4.1 book - Part 22 of 31
 
Inserindo em Ordem Crescente na Lista Encadeada
Inserindo em Ordem Crescente na Lista EncadeadaInserindo em Ordem Crescente na Lista Encadeada
Inserindo em Ordem Crescente na Lista Encadeada
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
 
Pre zen ta sion
Pre zen ta sionPre zen ta sion
Pre zen ta sion
 
งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์
 
Hello Swift 3/5 - Function
Hello Swift 3/5 - FunctionHello Swift 3/5 - Function
Hello Swift 3/5 - Function
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
6. binary tree
6. binary tree6. binary tree
6. binary tree
 
Implementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 redux
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Collection Core Concept
Collection Core ConceptCollection Core Concept
Collection Core Concept
 
The Ring programming language version 1.10 book - Part 96 of 212
The Ring programming language version 1.10 book - Part 96 of 212The Ring programming language version 1.10 book - Part 96 of 212
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
The Ring programming language version 1.5.1 book - Part 77 of 180The Ring programming language version 1.5.1 book - Part 77 of 180
The Ring programming language version 1.5.1 book - Part 77 of 180
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
 
C++ programs
C++ programsC++ programs
C++ programs
 
2013 - Benjamin Eberlei - Doctrine 2
2013 - Benjamin Eberlei - Doctrine 22013 - Benjamin Eberlei - Doctrine 2
2013 - Benjamin Eberlei - Doctrine 2
 
Short intro to the Rust language
Short intro to the Rust languageShort intro to the Rust language
Short intro to the Rust language
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 

Semelhante a week-13x

Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdf
sudhirchourasia86
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
vasavim9
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
fathimahardwareelect
 
#include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #define ISEMP.pdf
#include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #define ISEMP.pdf#include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #define ISEMP.pdf
#include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #define ISEMP.pdf
harihelectronicspune
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf
sudhinjv
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
fortmdu
 

Semelhante a week-13x (20)

C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
 
Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdf
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 
#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdf
 
Linked list imp of list
Linked list imp of listLinked list imp of list
Linked list imp of list
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
 
Linked lists
Linked listsLinked lists
Linked lists
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
DS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docxDS UNIT4_OTHER LIST STRUCTURES.docx
DS UNIT4_OTHER LIST STRUCTURES.docx
 
#include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #define ISEMP.pdf
#include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #define ISEMP.pdf#include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #define ISEMP.pdf
#include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #define ISEMP.pdf
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
Linked list
Linked listLinked list
Linked list
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 

Mais de KITE www.kitecolleges.com (20)

DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENTDISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
 
BrainFingerprintingpresentation
BrainFingerprintingpresentationBrainFingerprintingpresentation
BrainFingerprintingpresentation
 
ch6
ch6ch6
ch6
 
week-11x
week-11xweek-11x
week-11x
 
PPT (2)
PPT (2)PPT (2)
PPT (2)
 
week-10x
week-10xweek-10x
week-10x
 
week-1x
week-1xweek-1x
week-1x
 
week-18x
week-18xweek-18x
week-18x
 
ch14
ch14ch14
ch14
 
ch16
ch16ch16
ch16
 
holographic versatile disc
holographic versatile discholographic versatile disc
holographic versatile disc
 
week-22x
week-22xweek-22x
week-22x
 
week-5x
week-5xweek-5x
week-5x
 
week-6x
week-6xweek-6x
week-6x
 
week-3x
week-3xweek-3x
week-3x
 
ch8
ch8ch8
ch8
 
Intro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.ukIntro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.uk
 
ch17
ch17ch17
ch17
 
ch4
ch4ch4
ch4
 
week-7x
week-7xweek-7x
week-7x
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Último (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.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