exercises in c language. I need a person who know c language

profilemr.su
Chapter24.pdf

CHAPTER - 24

LINKED LISTS

CHAPTER 24

LINKED LISTS

INTRODUCTION (Reading)

SELF REFERENTIAL STRUCTURES (Reading)

MEMORY MANAGEMENT (Reading)

DYNAMIC DATA STRUCTURES

DYNAMIC MEMORY ALLOCATION

DYNAMIC MEMORY IMPLEMENTATION

LINKED LISTS

DYNAMIC MEMORY ALLOCATION

usage:

newptr = (struct node *) malloc (sizeof

(struct node) );

if (newptr = (struct node *) malloc (sizeof

(struct node) ) )

assert (dm_allocated = ‘Y’) ;

free (newptr);

• Function malloc allocates the required

memory dynamically at execution time, it

takes an argument the number of bytes to

be allocated, and returns a pointer of type

void * to the allocated memory.

• A void * pointer may be assigned to a

variable of any pointer type.

• Not returning dynamically allocated

memory when it is no longer needed can

cause the system to run out of memory

prematurely.

• The free function de-allocate memory, the

memory is returned to the system so that

the memory can be reallocated in the

future.

• The length of the structures can be

calculated with sizeof (struct node).

• The limit for dynamic memory allocation

can be the available physical memory in

the computer in a virtual memory system.

LINKED LISTS

#include <stdio.h>

#include <stdlib.h>

struct listnode {

char data;

struct listnode *nextptr;

};

typedef struct listnode LISTNODE;

typedef struct LISTNODE *LISTNODEPTR;

void insert (LISTNODEPTR *, char) ;

char delete (LISTNODEPTR *, char) ;

int isempty (LISTNODEPTR);

void printlist (LISTNODEPTR);

void instructions (void);

void main ()

{

LISTNODEPTR startptr = NULL;

int choice;

char item;

instructions ();

printf (“? “);

scanf (“%d”, &choice);

while (choice != 3)

{

switch (choice)

{

case 1:

printf (“Enter a character: “);

scanf ("\n%c", &item);

insert ((&startptr, item);

printlist (startptr);

break;

DYNAMIC MEMORY IMPLEMENTATION

LINKED LISTS

case 2:

if (!isempty (startptr) )

{

printf (“Enter a character to

be deleted: “);

scanf (“\n%c”, &item);

if (deletedata (&startptr, item) )

{

printf (“%c deleted. \n “,

item);

printlist (startptr);

}

else

printf ( “%c not found. \n

\n“, item);

}

else

printf (“List is empty. \n\n”);

break;

default :

printf (“Enter a character to be

deleted: “);

instructions ();

break;

} /* end of switch statement */

printf (“? “);

scanf (“%d”, &choice);

} /* end of while loop */

printf ("End of run. \n”);

return;

} /* end of main routine */

DYNAMIC MEMORY IMPLEMENTATION

LINKED LISTS

DYNAMIC MEMORY IMPLEMENTATION

void instructions (void)

{

printf (“\n Enter your choice: \n“);

printf (“ 1 to insert an element into

the list.\n“);

printf (“ 2 to delete an element from

the list.\n“);

printf (“ 3 to end.\n“);

}

int isempty (LISTNODEPTR sptr)

{

return (sptr == NULL);

}

void insert (LISTNODEPTR * sptr, char value)

{

LISTNODEPTR newptr, prevptr, currptr;

newptr = (LISTNODEPTR) malloc (sizeof

(LISTNODE) );

if (newptr != NULL)

{

newptr->data = value;

newptr->nextptr = NULL;

prevptr = NULL;

currptr = *sptr;

while ((currptr != NULL) && (value >

currptr->data))

{

prevptr = currptr;

currptr = currptr ->nextptr;

}

LINKED LISTS DYNAMIC MEMORY IMPLEMENTATION

if (prevptr == NULL )

{

newptr-> nextptr = *sptr;

*sptr = newptr;

}

else

{

prevptr->nextptr = newptr;

newptr-> nextptr = currptr;

}

}

else

printf (“%c not inserted. No memory is

available\n “, value);

} /* end of insert routine */

char delete (LISTNODEPTR *sptr, char

value)

{

LISTNODEPTR prevptr, currptr,

tempptr;

if (value == (*sptr)->data )

{

tempptr = *sptr;

*sptr = (*sptr)->nextptr;

free (tempptr);

return (value);

}

else

{

prevptr = *sptr;

currptr = (*sptr)->nextptr;

LINKED LISTS

DYNAMIC MEMORY IMPLEMENTATION

while ((currptr != NULL) &&

(currptr->data != value))

{

prevptr = currptr;

currptr = currptr ->nextptr;

}

if (currptr != NULL)

{

tempptr = currptr;

prevptr->nextptr = currptr ->nextptr;

currptr = currptr->nextptr;

free (tempptr);

return (value);

} /* end of inner if statement */

} /* end of outer else statement */

return (‘\0’);

} /* end of delete routine */

void printlist (LISTNODEPTR currptr)

{

if (currptr == NULL) )

{

printf (“List is empty. \n\n “);

}

else

{

printf (“List is not empty. \n\n “);

while ( currptr != NULL)

{

printf (“%c --> “ , currptr ->data );

currptr = currptr ->nextptr;

} /* end of while loop */

printf (“NULL \n\n “);

} /* end of else statement */

} /* end of print list */

LINKED LISTS DYNAMIC MEMORY IMPLEMENTATION

Output of the program:

Enter your choice:

1 to insert an element into the list.

2 to delete an element from the list.

3 to end.

? 1

Enter a character: B

List is not empty.

B --> NULL

? 1

Enter a character: A

List is not empty.

A --> B --> NULL

? 1

Enter a character: C

List is not empty.

A --> B --> C --> NULL

? 2

Enter a character to be deleted: D

D not found.

? 2

Enter a character to be deleted: B

B deleted.

List is not empty.

A --> C --> NULL

? 2

Enter a character to be deleted: C

C deleted.

List is not empty.

A --> NULL

? 2

Enter a character to be deleted: A

A deleted.

List is empty.

? 4

Enter a character to be deleted:

Enter your choice:

1 to insert an element into the list.

2 to delete an element from the list.

3 to end.

? 3

End of run.