0% found this document useful (0 votes)
45 views

Lab 1 Week 12

This lab covers pointers, dynamic memory allocation, and linked lists in C. The activities include: 1) Using pointers to read values into an array, 2) Tracing the output of a program that uses pointers, 3) Creating, displaying, inserting, and deleting nodes from a linked list.

Uploaded by

Intan Nur Dania
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Lab 1 Week 12

This lab covers pointers, dynamic memory allocation, and linked lists in C. The activities include: 1) Using pointers to read values into an array, 2) Tracing the output of a program that uses pointers, 3) Creating, displaying, inserting, and deleting nodes from a linked list.

Uploaded by

Intan Nur Dania
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

TAB1013 Structured Programming

Lab 1 Week 12

LAB 1 WEEK 12

Objective
By the end of this lab session, you will be able to:
1. Understand pointers
2. Understand dynamic memory allocation
3. Understand linked list

Activity #1

Given the following definition:

int num[20];

and using only pointer notation, write a for loop to read integer values from the
keyboard to fill up the array.

Activity #2

Trace the output of the following program.

int main(void)
{
int a = 6;
int b = 39;
int c[5] = {7,3,12,44,60};
int* poinC = c;

a = play(poinC,a,&b);

printf("2. %d %d %d %d %d %d %d\n",a, b, c[0],


c[1], c[2], c[3], c[4]);
getch();
return 0;
}

int play (int* poinX,int y, int *poinZ)


{
int i = 5;
int* p;

printf("1. %d %d %d\n",*poinX,y,*poinZ);
for (p = poinX;p < poinX + 5; p++)
*p = y + *p;
*poinX = 2 * i;
return (*poinZ + *poinX + y);
}

Activity #3

July 2008
TAB1013 Structured Programming
Lab 1 Week 12

Copy, compile, run the code and understand how linked list works.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>

struct node
{
int a;
struct node *next;
};
struct node *create()
{
struct node *p, *r, *n;
int s,k;
s=sizeof(struct node);
printf("Enter the element-1 to stop:");
scanf("%d", &k);
p=r=NULL;
while(k!=-1)
{
n=(struct node *)malloc(s);
n->a=k;
n->next=NULL;
if(r==NULL)
r=n;
else
p->next=n;
p=n;
printf("Enter the element-1 to stop:");
scanf("%d", &k);
}
return(r);
}
void display(r)
struct node *r;
{
printf("\nRoot ");
while(r!=NULL)
{
printf("\t%d", r->a);
r=r->next;
}
printf("\tnull");
}

main()
{
struct node *r;
Activity #4 r=create();
display(r);
getch();
}
July 2008
TAB1013 Structured Programming
Lab 1 Week 12

Copy, compile, run the code and understand how linked list works (insertion and
deletion).

/* Operating and maintaining a list */


#include <stdio.h>
#include <stdlib.h>

/* self-referential structure */
struct listNode
{
char data; /* each listNode contains a character */
struct listNode *nextPtr; /* pointer to next node*/
}; /* end structure listNode */

typedef struct listNode ListNode; /* synonym for struct listNode */


typedef ListNode *ListNodePtr; /* synonym for ListNode* */

/* prototypes */
void insert( ListNodePtr *sPtr, char value );
char delete( ListNodePtr *sPtr, char value );
int isEmpty( ListNodePtr sPtr );
void printList( ListNodePtr currentPtr );

int main()
{
ListNodePtr startPtr = NULL; /* initially there are no nodes */
int choice; /* user's choice */
char item; /* char entered by user */

/* display the menu */


printf("Enter your choice:\n");
printf("\t1 to insert an element into the list.\n");
printf("\t2 to delete an element from the list.\n");
printf("\t3 to end.\n\n");
printf("Your Choice > ");
scanf("%d", &choice);

/* loop while user does not choose 3 */


while (choice != 3)
{
switch (choice)
{
case 1:
printf("Enter a character: ");
scanf("\n%c", &item);
insert(&startPtr, item); /* insert item in list */
printList(startPtr);
break;

case 2:
/* if list is not empty */
if (!isEmpty(startPtr))
{
printf("Enter character to be deleted: ");
scanf("\n%c", &item);

July 2008
TAB1013 Structured Programming
Lab 1 Week 12

/* if character is found, remove it */


if (delete(&startPtr, item))
{ /* remove 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("Invalid choice.\n\n");
printf("Enter your choice:\n");
printf("\t1 to insert an element into the list.\n");
printf("\t2 to delete an element from the list.\n");
printf("\t3 to end.\n\n");
break;
}
printf("Your Choice > ");
scanf("%d", &choice);
}
printf("End of run.\n");
return 0;
}

/* Insert a new value into the list in sorted order */


void insert( ListNodePtr *sPtr, char value )
{
ListNodePtr newPtr; /* pointer to new node */
ListNodePtr previousPtr; /* pointer to previous node in list */
ListNodePtr currentPtr; /* pointer to current node in list */

newPtr = malloc( sizeof( ListNode ) ); /* create node */

if ( newPtr != NULL ) /* is space available */


{
newPtr->data = value; /* place value in node */
newPtr->nextPtr = NULL; /* node does not link to another node */

previousPtr = NULL;
currentPtr = *sPtr;

/* loop to find the correct location in the list */


while ( currentPtr != NULL && value > currentPtr->data )
{
previousPtr = currentPtr; /* walk to ... */
currentPtr = currentPtr->nextPtr; /* ... next node */
}

/* insert new node at beginning of list */


if ( previousPtr == NULL )
{
newPtr->nextPtr = *sPtr;

July 2008
TAB1013 Structured Programming
Lab 1 Week 12

*sPtr = newPtr;
}
else /* insert new node between previousPtr and currentPtr */
{
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else
printf( "%c not inserted. No memory available.\n", value );
} /* end function insert */

/* Delete a list element */


char delete( ListNodePtr *sPtr, char value )
{
ListNodePtr previousPtr; /* pointer to previous node in list */
ListNodePtr currentPtr; /* pointer to current node in list */
ListNodePtr tempPtr; /* temporary node pointer */

/* delete first node */


if ( value == ( *sPtr )->data )
{
tempPtr = *sPtr; /* hold onto node being removed */
*sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
free( tempPtr ); /* free the de-threaded node */
return value;
}
else
{
previousPtr = *sPtr;
currentPtr = ( *sPtr )->nextPtr;

/* loop to find the correct location in the list */


while ( currentPtr != NULL && currentPtr->data != value )
{
previousPtr = currentPtr; /* walk to ... */
currentPtr = currentPtr->nextPtr; /* ... next node */
}

/* delete node at currentPtr */


if ( currentPtr != NULL )
{
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );
return value;
}
}
return '\0';
} /* end function delete */

/* Return 1 if the list is empty, 0 otherwise */


int isEmpty( ListNodePtr sPtr )
{
return sPtr == NULL;
} /* end function isEmpty */

July 2008
TAB1013 Structured Programming
Lab 1 Week 12

/* Print the list */


void printList( ListNodePtr currentPtr )
{
/* if list is empty */
if ( currentPtr == NULL )
{
printf( "List is empty.\n\n" );
}
else
{
printf( "The list is:\n" );

/* while not the end of the list */


while ( currentPtr != NULL )
{
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
}
printf( "NULL\n\n" );
}
} /* end function printList */

July 2008

You might also like