1 / 17

Linked list exercise

Linked list exercise. A linked list consisting of 2 nodes with the following structure: typedef struct test{ char name[10]; struct test* nextadd; } TEST; TEST *start,*t1,*t2 start holds the address of node 1 Node 2’s nextadd is NULL

gad
Download Presentation

Linked list exercise

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked list exercise A linked list consisting of 2 nodes with the following structure: typedef struct test{ char name[10]; struct test* nextadd; } TEST; TEST *start,*t1,*t2 start holds the address of node 1 Node 2’s nextadd is NULL Write slice of code to reverse the order of this link and let start point to the new node 1

  2. t1 points to node 1 node 1 links NULL t2 points to node 2 node 2 links node 1 t1=start t2=t1->nextadd t1->nextadd=NULL T2->nextadd= t1 start=t2 start points to node 2 Node 2 address Node 1 address start NULL

  3. I/O review • p. 452 for scan • p. 458for print scanf ( “ \”%[^\”]\””,s); first white space outside “, no effect second white space between “ and \ means ‘ignore all consecutive white-space char up to the next char that is not a white space \” represent that the double quote must be matched in the input %[^\”] mean reading and store all char up to a “ \” means that input must match “ If matched, marker moves to the char following “

  4. printf(“7.1f %-10.2e\n”,x,y) 7 spaces, 1 decimal point (round up if necessary), 1 blank space,left adjusted, 10 spaces, 2 decimal, e±dd,new line char title[]=“this is a review”; n=123; printf(“%+2d\n%5s%.5s\n”,n,title,title+1) +123 this is a reviewhis i

  5. printf(“10.*f\n”,3,-12.34567) ___-12.346

  6. 1 2 4 3 6 7 8 9 IV.(15pts)Long Integers Suppose the following declaration is available globally: struct longint { int digit; int power; struct longint *next; }; This template has been used in creating a linked list representation of large integers, where the pointer to the list "points" to the least significant digit. For example, the number 3,421,000,000 would be represented as Write a C function whose prototype is: struct longint *bigger(struct longint *int1, struct longint *int2); which will accept pointers to the beginning of two large integer linked lists and return a pointer to the beginning of the list which represents the larger integer. start

  7. struct longint *bigger(struct longint *in1,struct longint *in2) { int n1=0,n2=0,i; struct longint *p1,*p2; p1 = in1; while(p1->next!=NULL) { n1++;p1=p1->next; } p2=in2; while(p2->next!=NULL) {n2++;p2=p2->next;} if(p1->power>p2->power)return in1; if(p2->power>p1->power)return in2; if(p1->digit>p2->digit)return in1; if(p2->digit>p1->digit)return in2;

  8. while(n1>=0&&n2>=0) { p1=in1; for(i=0;i<n1;i++) p1=p1->next; p2=in2; for(i=0;i<n2;i++)p2=p2->next; if(p1->power>p2->power)return in1; if(p2->power>p1->power)return in2; if(p1->digit>p2->digit)return in1; if(p2->digit>p1->digit)return in2; n1--;n2--; } if(n1<0&&n2>=0)return(in2); if(n2<0&&n1>=0)return(in1); if(n1<0&&n2<0) printf("\n values same\n"); return(in1); }

  9. V.(15pts)Cold Spots Suppose that the following declarations are available globally: struct citytemps { char city[35]; float temp[366];}; struct coldcnts { char city[35]; int count;}; Suppose further that a data file named cold.dat has been prepared containing the names of each city in Texas, along with that city's minimum temperatures(in Fahrenheit) for each day of the past year. The name of each city starts on a new line and is terminated by a semicolon (;). Following the name are the minimum temperatures for that city. For example: Austin; 44 48 38 ... Beeville; 38 37 35 ... San Antonio; 51 49 40 ..

  10. Write a complete C program that will input the data into an array of type struct citytemps, where each array element corresponds to a city. Then create a new array of type struct coldcnts, consisting of the name of the city and the number of days that city's minimum temperature was at or below freezing. Do not include in this new array any cities which had all days above freezing. Then print this new array, one city per line.

  11. #include <stdio.h> #include <string.h> struct citytemps {char city[35]; float temp[366];}; struct coldcnts {char city[35]; /*not required*/ int count;}; main( ) { FILE *fin,*fout; struct citytemps cities[500]; struct coldcnts freeze[500]; int i,j,k,nfrz=0,ncity=0,done=0,nv; float t; char name[35]; fin=fopen("city.dat","r"); fout=fopen("city.out","w");

  12. while(done==0) { nv=fscanf(fin,"%[^;];",name); if(name[1]=='\0')nv=0; if(nv>0) { strcpy(cities[ncity].city,name); fprintf(fout,"\n city %d %s:",ncity+1,cities[ncity].city); for(i=0;i<366;i++) { fscanf(fin,"%f",&t); cities[ncity].temp[i]=t; } ncity++; } else done=1; }

  13. for(i=0;i<ncity;i++) { k=0; for(j=0;j<366;j++) { if(cities[i].temp[j]<=32) k++; } if(k>0) { freeze[nfrz].count=k; strcpy(freeze[nfrz].city,cities[i].city); nfrz++; } } for(i=0;i<nfrz;i++) fprintf(fout,"\n %s %d",freeze[i].city,freeze[i].count); }

  14. 11.1.4 What is the error in the following code? int num; num = malloc( sizeof (int) ); Ans: num is an integer, not a pointer.

  15. 11.1.8 What is printed? int *ptr; ptr = malloc( sizeof(int) ); *ptr = 999; ptr = malloc( sizeof( int ) ); *ptr = 123; printf(“\n%d\n”, *ptr); Ans: 123

  16. 11.3.1 What is printed? struct node { char letter; struct node* next; } a, b, c, *ptr; a.letter = ‘A’; b.letter = ‘B’; c.letter = ‘C’; a.next = &b; b.next = &c; c.next = NULL; ptr = a.next; while (ptr != NULL) { printf( “\n%c\n”, ptr->letter ); ptr = ptr->next; } Ans: B C

  17. 11.3.2 What is printed when we execute the code of Exercise 1 if we change the line while( ptr != NULL ) { To while( ptr ->next != NULL ) { Ans: B

More Related