Structure LinkedList4
Structure LinkedList4
mydef i, j;
typedef struct {
int acct_no;
char acct_type;
char name[80];
float balance;
} account ;
account customer[100], oldcustomer, newcustomer;
MA511: Computer Programming
Partha S Mandal, IITG
Member of a struct may be a struct
Equivalent to:
typedef struct { struct date {
int day; int day;
int month;
int month;
int year;
int year; };
} date; struct account {
typedef struct { int acct_no;
int acct_no; char acct_type;
char acct_type; char name[80];
char name[80]; float balance;
float balance; struct date update;
date update; };
struct account customer[100];
} account;
account customer[100];
customer[i].acct_no: variable of the structure account
customer[i].update.month: variable of the structure date
MA511: Computer Programming
Partha S Mandal, IITG
Member of a struct may be a struct and so on
v
22ccbc
&pu
Example: pointers
22CCC4
22ccb8 pu
3
&pv u
#include<stdio.h> 22CCC0
main(){ pv 3
int u = 3, v;
int *pu, *pv; // pointer to an integer
pu = &u; // assign add of u to pu
v
v = *pu; // assign value of u to v
pv = &v; // assign add of v to pv
printf(“u=%d &u = %X pu = %x *pu =%d\n”, u, &u, pu, *pu);
printf(“v=%d &v = %X pv = %x *pv =%d\n”, v, &v, pv, *pv);
}
Output:
u=3 &u = 22CCC4 pu = 22ccc4 *pu =3
v=3 &v = 22CCC0 pv = 22ccc0 *pv =3
Example: pointers
22ccbc
&pu
22CCC4
22ccb8 pu 3
&pv u
22CCC0
pv 3
v
Assignment
• Swap values of two variables using function
and passing pointer as arguments.
swap
/* swap values of two variables using function and passing pointer as arguments */
void swap(int *a, int *b){
int temp;
temp = *a; Output:
*a = *b; x= 15 y= 24, px = 22ccc4, py = 22ccc0
*b = temp; x= 24 y= 15, px = 22ccc4, py = 22ccc0
}
main(){
int x = 15, y = 24, *px, *py;
px = &x; py = &y;
printf("x= %d y= %d, px = %x, py = %x\n", x, y, px, py);
swap(px, py);
printf("x= %d y= %d, px = %x, py = %x\n", x, y, px, py);
}
swap
/* swap values of two variables using function and passing pointer as arguments */
void swap(int *a, int *b){
int temp;
temp = *a; Output:
*a = *b; x= 15 y= 24, &x = 22ccc4, &y = 22ccc0
*b = temp; x= 24 y= 15, &x = 22ccc4, &y = 22ccc0
}
main(){
int x = 15, y = 24;
printf("x= %d y= %d, &x = %x, &y = %x\n", x, y, &x, &y);
swap(&x, &y);
printf("x= %d y= %d, &x = %x, &y = %x\n", x, y, &x, &y);
}
MA 511: Computer Programming
Lecture 14: Pointer contd.
return 0; Output:
h
} x, 22ccc7
Passing pointer to an array
Example 1: Example 2:
void func(int *p); void func(int p[]);
int main(void){ int main(void){
static int a[5]={10,20,30,40,50}; static int a[5]={10,20,30,40,50};
func(a + 3); func(a + 3);
return 0; return 0;
} }
void func(int *p){ void func(int p[ ]){
int i, sum = 0; int i, sum = 0;
for(i=0; i<2; i++) for(i=0; i<2; i++)
sum += *(p+i); sum += *(p+i);
printf(“sum = %d”, sum); printf(“sum = %d”, sum);
} }
Assignments
• Polynomial addition
• Polynomial multiplication
MA 511: Computer Programming
Lecture 16: Pointer Contd.
. . .
return ptr;
}
int list[]={1,2,3,4};
printf(“%x, %x, %d”, list, &list[0], *list);
list 1 2 3 4
int *p,
int list[]={1,2,3,4}; [0] [1] [2] [3]
p = list; /* equivalent to p = &list[0] */
printf(“%d\n”, *p); /* prints the value “1” */
1 2 3 4
1 2 3 4
Pointer Arithmetic
int list[] = {1, 2, 3, 4};
int *p = list; /* same as p = &list[0] */
printf(“%x”,p); /* prints ffe2de0c */
p = p + 1; /* p increases by 4 */
printf(“%x”,p); /* prints ffe2de10 */
p
Think of pointer
arithmetic as add
1 “location” ffe2de0c ffe2de10 ffe2de14 ffe2de18
instead of one 1 2 3 4
byte or address.
Pointer Arithmetic
double list2[] = {1.0, 2.0, 3.0};
double *p = list2; /* same as p = &list2[0] */
printf(“%x”, p); /* prints ffe2de0c */
int cave[ArraySize][ArraySize];
Column
0 1 2 3
0 1 2 3 4
1
5 6 7 8
Row
2 9 10 11 12
3 13 14 15 16
2D Arrays
int myMatrix[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12} };
Column
0 1 2 3
0 1 2 3 4 myMatrix[0][1] → 2
1
Row 5 6 7 8
2 9 10 11 12
myMatrix[2][3] → 12
myMatrix[row][col]
Physically, in one block of memory
int myMatrix[2][4] = { {1,2,3,4},{5,6,7,8} };
ffe2de0c ffe2de10 ffe2de14 ffe2de18 ffe2de1c ffe2de20 ffe2de24 ffe2de28
1 2 3 4 5 6 7 8
row 1 row 2
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
(x + 1)
(x + 9)
Pointer & Multidimensional Array
How to access the item in row 3 and column 4
x[2][3] or *(*(x+2) + 3)
x 0 1 2 3 19
0
(x + 1) *x
1
(x + 2) *(x + 1)
typedef struct {
int acct_no;
char acct_type;
char name[80];
float balance;
date lastpayment;
} customer, *ptr = &customer;
get_data(ptr); //get_data(&customer);
void get_data(account *p){
printf("Account= %s Name=%s\n", ptr->acct_type, ptr->name);
p->acct_no = 55555; printf("AccNo=%d, Balance=%f\n", ptr->acct_no, ptr->balance);
p->acct_type = "C/A"; }
p->name = "abc"; Account= S/B Name=xyz
AccNo=12341, Balance=2345.000000
p->balance = 777777.00;Account= C/A Name=abc
AccNo=55555, Balance=777777.000000
}
get_data(ptr); //get_data(&customer);
void get_data(account *p){
printf("Account= %s Name=%s\n", ptr->acct_type, ptr->name);
p->acct_no = 55555; printf("AccNo=%d, Balance=%f\n", ptr->acct_no, ptr->balance);
p->acct_type = "C/A"; }
p->name = "abc"; Account= S/B Name=xyz
AccNo=12341, Balance=2345.000000
p->balance = 777777.00;Account= C/A Name=abc
AccNo=55555, Balance=777777.000000
}
temp
temp
number next number next number next
102 112 122
number next
target_no :122
999 NULL
How to insert node in middle of the list?
newnode = (node *) malloc(sizeof(node));
scanf(“%d”, &(newnode->data));
newnode->next = target->next;
target ->next= newnode;
start target
number next number next
X
102 112 122 NULL
newnode
115
How to insert node at the beginning of the list?
newnode
next
80
Assignment
• Write a C-Program for inserting a new node in
a linked list after the target data (or key). The
key value of a new node and target key should
be given from the terminal.
• Write a C-Program for inserting a new node in
a linked list maintaining order with respect to
the key value of nodes. The key value of a new
node should be given from the terminal.
MA 511: Computer Programming
Lecture 22: Linked list – Deletion