1 / 16

# Computer Programming for Engineering Applications - PowerPoint PPT Presentation

Computer Programming for Engineering Applications . ECE 175. Intro to Programming . Need for Truly Dynamic “ Arrays ”. Assume the following structure Consecutive executions of malloc reserve different parts of the memory Goal is to connect the different pieces of memory.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Computer Programming for Engineering Applications ' - tierra

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Computer Programming forEngineering Applications

ECE 175

Intro to Programming

Need for Truly Dynamic “Arrays”

Assume the following structure

Consecutive executions of malloc reserve different parts of the memory

Goal is to connect the different pieces of memory

typedefstructnode_s {

char name[20];

int age;

structnode_s *listp;

} node_t;

name

age

name

age

name

age

ECE 175

Linked Lists: non-sequential pieces of memory connected using pointers

How are linked lists different than arrays?

Memory cells are not sequential

List size can be dynamic, no need to predefine it as with arrays

One can add and delete nodes to the list

ECE 175

Declaration of structures with pointer attributes to themselves

typedefstructnode_s {

char name[20];

int age;

structnode_s *listp;

} node_t;

name

age

listp

ECE 175

To create a linked list we need to

Create a head pointer that points to the first element, so we can traverse the list from the start

Make each element of the list point to the next one

Make the last pointer point to NULL, so we can denote the end of the list

name

age

listp

name

age

listp

name

age

listp

NULL

ECE 175

name

age

listp

name

age

listp

name

age

listp

int main(void)

{

int i=0;

FILE *inp;

char c;

char s1[20];

inp=fopen("database.dat", "r");

while(!feof(inp))

{

temp=(node_t *)malloc(sizeof (node_t)); // creation of memory

scan_fun(temp, inp); // initialization of element of list

if (current==NULL)

else

current->listp=temp; // elseconnecting to previous element

i++; // countnumber of elements added

current=temp; // updating the current element

temp->listp=NULL; // setting pointer to null.

}

NULL

ECE 175

Scanning elements from a file, and printing on the screen

voidscan_fun(node_t *pt, FILE *in)

{ //scans file and stores input on a node_t structure

fscanf(in, "%s%d", pt->name, &pt->age);

}

voidprint_fun(node_t *pt)

{ //prints a node_t structure

printf("%s, %d\n", pt->name, pt->age);

}

void print_list(node_t *pt)

{

if (pt==NULL)

printf("The list is empty\n");

else

{

while (pt!=NULL)

{ // traversing the list

print_fun(pt);

pt=pt->listp;

}

}

}

ECE 175

name

age

listp

name

age

listp

name

age

listp

NULL

ECE 175

Searching by the name attribute

node_t *find_name(node_t *pt, char *query)

{ // finds the name query in the database

// returns pointer to structure matched or null

while(strcmp(pt->name, query)!=0 && pt!=NULL)

{

pt=pt->listp;

}

returnpt;

}

ECE 175

Searching by a range of ages

voidfind_age(node_t *pt, int min, int max)

{ // finds elements within a range of ages

while(pt!=NULL)

{

if(pt->age >= min && pt->age <=max)

print_fun(pt);

pt=pt->listp;

}

}

ECE 175

Good for sorting

void swap(node_t *x, node_t *y)

{ // swaps two elements on the list

node_t t; // temporary node

strcpy(t.name, x->name);

t.age=x->age;

strcpy(x->name, y->name);

x->age=y->age;

strcpy(y->name,t.name);

y->age=t.age;

}

x

y

listp

listp

listp

listp

listp

ECE 175

voidsort_list(node_t *pt)

{ // sorts the list alphabetically

node_t *temp, *min;

while (pt!=NULL) // while list is not sorted

{ temp=pt; // point temp to the first unsorted element

min=pt; // make the first unsroted element the current minimum

while(temp!=NULL)

{

if(strcmp(min->name, temp->name)>0)

{// if new minimum is found

min=temp; // update to the new min

}

temp=temp->listp; // inspect the next element

}

swap(pt, min); // swap the first element with min

pt=pt->listp; // move to the next unsorted element

}

}

ECE 175

name

age

listp

name

age

listp

NULL

name

age

listp

{ // adds an element at the end of the list

node_t *temp;

temp=(node_t *)malloc(sizeof (node_t)); // memory allocation

printf("Enter the age of the new member:");

scanf("%d",&temp->age); //scan for the age

printf("Enter the name of the new member:");

scanf("%s", temp->name); // scan for the name

if (*h==NULL) // if list is empty

*h=temp; // point the head to temp

else

{

(*c)->listp=temp; // point previous last element to temp

}

*c=temp; // update the current last element

temp->listp=NULL; // point temp->listp to NULL

}

ECE 175

name

age

listp

name

age

listp

name

age

listp

NULL

ECE 175

voiddelete_member(node_t **h, node_t **c)

{

node_t *target; // pointer to string to be deleted

node_t **temp=h; // pointer to the head of the list

char s[20];

printf("Enter the name of the entryyouwant to delete:");

fflush(stdin);

scanf("%s",s);

target=find_name(*h, s); // finding the element to be deleted

if (target==NULL)

printf("This entry doesnotexist\n");

else

{

while ((*temp)!=target)

{

temp=&(*temp)->listp; // locating the address of the previous element

}

if (target==*h) // if first element must be deleted

*h=target->listp; // make head point to the next element

if (target==*c) // if last element is to be deleted

*c=*temp; // update the position of the last element

*temp=target->listp; // skip element to be deleted

free(target); // free the memory

}

}

ECE 175

pause(); // wait for user input

pause();

find_age(headp, 22, 28); //find elements with age between 22 and 28

pause();

delete_member(&headp, &current); // delete a member