![Page 1: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/1.jpg)
Day20.C
Data StructureIntroduction
Young W. Lim
December 12, 2017
This work is licensed under a Creative Com-mons “Attribution-NonCommercial-ShareAlike 3.0Unported” license.
![Page 2: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/2.jpg)
Day20.C Young W. Lim
1 Self-Referential Structure
1.1 Examples of declaring self-referential structure vari-ables
::::::::::::::
h1.c
::::::::::::::
#include <stdio.h>
//.............................
struct aaa {
int data;
struct aaa *next;
};
struct aaa vara;
//.............................
struct bbb {
int data;
struct bbb *next;
} varb;
//.............................
struct ccc {
int data;
struct ccc *next;
};
typedef struct ccc ctype;
ctype varc;
//.............................
typedef struct ddd {
int data;
struct ddd *next;
} dtype;
dtype vard;
int main(void) {
vara.data = 111;
vara.next = NULL;
varb.data = 222;
Page 1
![Page 3: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/3.jpg)
Day20.C Young W. Lim
varb.next = NULL;
varc.data = 333;
varc.next = NULL;
vard.data = 444;
vard.next = NULL;
printf("vara.data=%d vara.next=%p \n", vara.data, vara.next);
printf("varb.data=%d varb.next=%p \n", varb.data, varb.next);
printf("varc.data=%d varc.next=%p \n", varc.data, varc.next);
printf("vard.data=%d vard.next=%p \n", vard.data, vard.next);
}
::::::::::::::
h1.out
::::::::::::::
vara.data=111 vara.next=(nil)
varb.data=222 varb.next=(nil)
varc.data=333 varc.next=(nil)
vard.data=444 vard.next=(nil)
::::::::::::::
h2.c
::::::::::::::
#include <stdio.h>
//.............................
struct aaa vara;
struct aaa {
int data;
struct aaa *next;
};
//.............................
struct bbb {
int data;
struct bbb *next;
} varb;
//.............................
typedef struct ccc ctype;
ctype varc;
struct ccc {
int data;
struct ccc *next;
Page 2
![Page 4: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/4.jpg)
Day20.C Young W. Lim
};
//.............................
typedef struct ddd {
int data;
struct ddd *next;
} dtype;
dtype vard;
int main(void) {
vara.data = 111;
vara.next = NULL;
varb.data = 222;
varb.next = NULL;
varc.data = 333;
varc.next = NULL;
vard.data = 444;
vard.next = NULL;
printf("vara.data=%d vara.next=%p \n", vara.data, vara.next);
printf("varb.data=%d varb.next=%p \n", varb.data, varb.next);
printf("varc.data=%d varc.next=%p \n", varc.data, varc.next);
printf("vard.data=%d vard.next=%p \n", vard.data, vard.next);
}
::::::::::::::
h2.out
::::::::::::::
vara.data=111 vara.next=(nil)
varb.data=222 varb.next=(nil)
varc.data=333 varc.next=(nil)
vard.data=444 vard.next=(nil)
::::::::::::::
h3.c
::::::::::::::
#include <stdio.h>
//.............................
struct aaa {
int data;
Page 3
![Page 5: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/5.jpg)
Day20.C Young W. Lim
struct aaa *next;
};
struct aaa vara;
//.............................
struct bbb {
int data;
struct bbb *next;
} varb;
//.............................
typedef struct ccc ctype;
struct ccc {
int data;
ctype *next;
};
ctype varc;
//.............................
typedef struct ddd {
int data;
struct ddd *next;
} dtype;
dtype vard;
int main(void) {
vara.data = 111;
vara.next = NULL;
varb.data = 222;
varb.next = NULL;
varc.data = 333;
varc.next = NULL;
vard.data = 444;
vard.next = NULL;
printf("vara.data=%d vara.next=%p \n", vara.data, vara.next);
printf("varb.data=%d varb.next=%p \n", varb.data, varb.next);
printf("varc.data=%d varc.next=%p \n", varc.data, varc.next);
printf("vard.data=%d vard.next=%p \n", vard.data, vard.next);
}
::::::::::::::
Page 4
![Page 6: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/6.jpg)
Day20.C Young W. Lim
h3.out
::::::::::::::
vara.data=111 vara.next=(nil)
varb.data=222 varb.next=(nil)
varc.data=333 varc.next=(nil)
vard.data=444 vard.next=(nil)
::::::::::::::
h4.c
::::::::::::::
#include <stdio.h>
//.............................
struct atype {
int data;
struct atype *next;
};
struct atype vara;
//.............................
struct btype {
int data;
struct btype *next;
} varb;
//.............................
typedef struct ctype ctype;
struct ctype {
int data;
ctype *next;
};
ctype varc;
//.............................
typedef struct dtype {
int data;
struct dtype *next;
} dtype;
dtype vard;
int main(void) {
vara.data = 111;
vara.next = NULL;
varb.data = 222;
varb.next = NULL;
Page 5
![Page 7: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/7.jpg)
Day20.C Young W. Lim
varc.data = 333;
varc.next = NULL;
vard.data = 444;
vard.next = NULL;
printf("vara.data=%d vara.next=%p \n", vara.data, vara.next);
printf("varb.data=%d varb.next=%p \n", varb.data, varb.next);
printf("varc.data=%d varc.next=%p \n", varc.data, varc.next);
printf("vard.data=%d vard.next=%p \n", vard.data, vard.next);
}
::::::::::::::
h4.out
::::::::::::::
vara.data=111 vara.next=(nil)
varb.data=222 varb.next=(nil)
varc.data=333 varc.next=(nil)
vard.data=444 vard.next=(nil)
• all variables are globally defined
• defined outside main function
• global variables vara, varb, varc, vard
• results may be different for local variables
Page 6
![Page 8: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/8.jpg)
Day20.C Young W. Lim
case view I
case 1 case 2
struct aaa { struct aaa vara;
int data;
struct aaa *next; struct aaa {
}; int data;
struct aaa *next;
struct aaa vara; };
struct bbb { struct bbb {
int data; int data;
struct bbb *next; struct bbb *next;
} varb; } varb;
struct ccc { typedef struct ccc ctype;
int data;
struct ccc *next; ctype varc;
};
struct ccc {
typedef struct ccc ctype; int data;
struct ccc *next;
ctype varc; };
typedef struct ddd { typedef struct ddd {
int data; int data;
struct ddd *next; struct ddd *next;
} dtype; } dtype;
dtype vard; dtype vard;
Page 7
![Page 9: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/9.jpg)
Day20.C Young W. Lim
case view II
case 3 case 4
struct aaa { struct atype {
int data; int data;
struct aaa *next; struct atype *next;
}; };
struct aaa vara; struct atype vara;
struct bbb { struct btype {
int data; int data;
struct bbb *next; struct btype *next;
} varb; } varb;
typedef struct ccc ctype; typedef struct ctype ctype;
struct ccc { struct ctype {
int data; int data;
ctype *next; ctype *next;
}; };
ctype varc; ctype varc;
typedef struct ddd { typedef struct dtype {
int data; int data;
struct ddd *next; struct dtype *next;
} dtype; } dtype;
dtype vard; dtype vard;
Page 8
![Page 10: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/10.jpg)
Day20.C Young W. Lim
• variable view 1. vara
Case 1 Case 2struct aaa { struct aaa vara;
int data;
struct aaa *next; struct aaa {
}; int data;
struct aaa *next;
struct aaa vara; };
Case 3 Case 4struct aaa { struct atype {
int data; int data;
struct aaa *next; struct atype *next;
}; };
struct aaa vara; struct atype vara;
case 1 case 2 case 3 case 4tag name aaa aaa aaa atypeorder definition declaration definition definition
declaration definition declaration declarationstruct type struct aaa struct aaa struct aaa struct atypepointer type struct aaa * struct aaa * struct aaa * struct atype *var type struct aaa struct aaa struct aaa struct atype
Page 9
![Page 11: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/11.jpg)
Day20.C Young W. Lim
• variable view 2. varb
Case 1 Case 2struct bbb { struct bbb {
int data; int data;
struct bbb *next; struct bbb *next;
} varb; } varb;
Case 3 Case 4struct bbb { struct btype {
int data; int data;
struct bbb *next; struct btype *next;
} varb; } varb;
case 1 case 2 case 3 case 4tag name bbb bbb bbb btypeorder simultaneous simultaneous simultaneous simultaneous
definition definition definition definitiondeclaration declaration declaration declaration
struct type struct bbb struct bbb struct bbb struct btypepointer type struct bbb * struct bbb * struct bbb * struct btype *var type struct bbb struct bbb struct bbb struct btype
Page 10
![Page 12: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/12.jpg)
Day20.C Young W. Lim
• variable view 3. varc
Case 1 Case 2struct ccc { typedef struct ccc ctype;
int data;
struct ccc *next; ctype varc;
};
struct ccc {
typedef struct ccc ctype; int data;
struct ccc *next;
ctype varc; };
Case 3 Case 4typedef struct ccc ctype; typedef struct ctype ctype;
struct ccc { struct ctype {
int data; int data;
ctype *next; ctype *next;
}; };
ctype varc; ctype varc;
case 1 case 2 case 3 case 4tag name ccc ccc ccc ctypeorder definition typedef typedef typedef
typedef declaration definition definitiondeclaration definition declaration declaration
struct type struct ccc struct ccc struct ccc struct ctypepointer type struct ccc * struct ccc * struct ccc * struct ctype *var type ctype ctype ctype ctype
Page 11
![Page 13: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/13.jpg)
Day20.C Young W. Lim
• variable view 4. vard
Case 1 Case 2typedef struct ddd { typedef struct ddd {
int data; int data;
struct ddd *next; struct ddd *next;
} dtype; } dtype;
dtype vard; dtype vard;
Case 3 Case 4typedef struct ddd { typedef struct dtype {
int data; int data;
struct ddd *next; struct dtype *next;
} dtype; } dtype;
dtype vard; dtype vard;
case 1 case 2 case 3 case 4tag name ddd ddd ddd dtypeorder simultaneous simultaneous simultaneous simultaneous
typedef typedef typedef typedefdefinition definition definition definitiondeclaration declaration declaration declaration
struct type struct ddd struct ddd struct ddd struct dtypepointer type struct ddd * struct ddd * struct ddd * struct dtype *var type dtype dtype dtype dtype
Page 12
![Page 14: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/14.jpg)
Day20.C Young W. Lim
• variable view summary (vara, varb, varc, vard)
case 1 case 2 case 3 case 4tag name aaa aaa aaa atypeorder definition declaration definition definition
declaration definition declaration declarationstruct type struct aaa struct aaa struct aaa struct atypepointer type struct aaa * struct aaa * struct aaa * struct atype *var type struct aaa struct aaa struct aaa struct atype
case 1 case 2 case 3 case 4tag name bbb bbb bbb btypeorder simultaneous simultaneous simultaneous simultaneous
definition definition definition definitiondeclaration declaration declaration declaration
struct type struct bbb struct bbb struct bbb struct btypepointer type struct bbb * struct bbb * struct bbb * struct btype *var type struct bbb struct bbb struct bbb struct btype
case 1 case 2 case 3 case 4tag name ccc ccc ccc ctypeorder definition typedef typedef typedef
typedef declaration definition definitiondeclaration definition declaration declaration
struct type struct ccc struct ccc struct ccc struct ctypepointer type struct ccc * struct ccc * struct ccc * struct ctype *var type ctype ctype ctype ctype
case 1 case 2 case 3 case 4tag name ddd ddd ddd dtypeorder simultaneous simultaneous simultaneous simultaneous
typedef typedef typedef typedefdefinition definition definition definitiondeclaration declaration declaration declaration
struct type struct ddd struct ddd struct ddd struct dtypepointer type struct ddd * struct ddd * struct ddd * struct dtype *var type dtype dtype dtype dtype
Page 13
![Page 15: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/15.jpg)
Day20.C Young W. Lim
2 Linked List Data Structure
2.1 Case 1
//.............................
typedef struct node node;
struct node {
int data;
node *next;
};
//.............................
node.h
• a self-referential struture type definition
• a data member
• a next member (the address of the next node)
Page 14
![Page 16: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/16.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//-----------------------------------
void init(node **sp, node **ep) {
int i;
node *p;
i=0;
do {
p = malloc( sizeof(node) );
p->data = 10 * i++;
p->next = NULL;
if (*sp == NULL) *sp = p;
if (*ep == NULL) *ep = p;
else {
(*ep)->next = p;
(*ep) = p;
}
} while (i<10);
}
init.c
• initializes a linked list with 10 nodes
• allocates 10 nodes
• sets up the next member of each node
• sp points to the start node
• ep points to the end node
Page 15
![Page 17: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/17.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//-----------------------------------
void display(node *sp) {
node *p = sp;
int i=0;
while (p) {
printf("%2d %3d \n", i++, p->data);
p = p->next;
}
}
display.c
• traverses each node in the linked list
• prints its data member
Page 16
![Page 18: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/18.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//-----------------------------------
node *find(node *sp, int key) {
node *p = sp;
while (p) {
if (p->data == key) return p;
p = p->next;
}
return NULL;
}
find.c
• finds a node which has the same data member as the key
• returns the pointer to such a node
Page 17
![Page 19: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/19.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//-----------------------------------
void insert(node *p, int key) {
node *q, *r;
r = malloc( sizeof(node) );
q = p->next;
p->next = r;
r->next = q;
r->data = key;
}
insert.c
• allocates a new node
• assigns the key to the data member of the newly allocated node
• inserts this node after the node which is pointed by p
Page 18
![Page 20: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/20.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//-----------------------------------
void delete(node *p) {
node *q, *r;
q = p->next;
r = q->next;
p->next = r;
free( q );
}
delete.c
• deletes the next node after the node which is pointed by p
Page 19
![Page 21: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/21.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//----------------------------------
void init(node **sp, node **ep);
node *find(node *sp, int key);
void display(node *sp);
void insert(node *p, int key);
void delete(node *p);
//----------------------------------
int main(void) {
node *p = NULL; // current pointer
node *sp= NULL; // start pointer
node *ep= NULL; // end pointer
init( &sp, &ep );
p = find( sp, 50);
printf("p->data = %d \n", p->data);
insert( p, 55 );
printf("\n* after inserting one node ...\n");
display( sp );
delete( p );
printf("\n* after deleting one node ...\n");
display( sp );
}
t3.c
• p points to the node whose data member is 50
• inserts a new node whose data member is 55, after the found node
• deletes the next node after the node which is pointed by p
Page 20
![Page 22: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/22.jpg)
Day20.C Young W. Lim
2.2 Case 2
//.............................
typedef struct node node;
struct node {
int data;
node *next;
};
//.............................
node.h
• a self-referential struture type definition
• a data member
• a next member (the address of the next node)
Page 21
![Page 23: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/23.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//-----------------------------------
void init(node **sp, node **ep) {
int i;
node *p;
i=0;
do {
p = malloc( sizeof(node) );
p->data = 10 * i++;
p->next = NULL;
if (*sp == NULL) *sp = p;
if (*ep == NULL) *ep = p;
else {
(*ep)->next = p;
(*ep) = p;
}
} while (i<10);
}
init.c
• initializes a linked list with 10 nodes
• allocates 10 nodes
• sets up the next member of each node
• sp points to the start node
• ep points to the end node
Page 22
![Page 24: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/24.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//-----------------------------------
void display(node *sp) {
node *p = sp;
int i=0;
while (p) {
printf("%2d %3d \n", i++, p->data);
p = p->next;
}
}
display.c
• traverses each node in the linked list
• prints its data member
Page 23
![Page 25: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/25.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//-----------------------------------
node *find2(node *sp, int key, node **pp) {
node *p = sp;
while (p) {
if (p->data == key) return p;
*pp = p;
p = p->next;
}
return NULL;
}
find2.c
• finds a node which has the same data member as the key
• returns the pointer to such a node
• pp is the pointer to the previous node
Page 24
![Page 26: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/26.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//-----------------------------------
void insert2(node *p, node *pp, int key) {
node *r;
r = malloc( sizeof(node) );
pp->next = r;
r->next = p;
r->data = key;
}
insert2.c
• allocates a new node
• assigns the key to the data member of the newly allocated node
• inserts this node before the node which is pointed by p
• after the node which is pointed by pp
Page 25
![Page 27: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/27.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//-----------------------------------
void delete2(node *p, node *pp) {
pp->next = p->next;
free( p );
}
delete2.c
• deletes the node which is pointed by p
Page 26
![Page 28: Data Structure Introduction - Wikimedia€¦ · Day20.C Data Structure Introduction Young W. Lim December 12, 2017 This work is licensed under a Creative Com-mons \Attribution-NonCommercial-ShareAlike](https://reader036.vdocument.in/reader036/viewer/2022070719/5edf449dad6a402d666a9d99/html5/thumbnails/28.jpg)
Day20.C Young W. Lim
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
//----------------------------------
void init(node **sp, node **ep);
node *find(node *sp, int key);
void display(node *sp);
void insert(node *p, int key);
void delete(node *p);
//----------------------------------
int main(void) {
node *p = NULL; // current pointer
node *sp= NULL; // start pointer
node *ep= NULL; // end pointer
init( &sp, &ep );
p = find( sp, 50);
printf("p->data = %d \n", p->data);
insert( p, 55 );
printf("\n* after inserting one node ...\n");
display( sp );
delete( p );
printf("\n* after deleting one node ...\n");
display( sp );
}
t4.c
• p points to the node whose data member is 50
• inserts a new node whose data member is 55, before the found node
• deletes the node which is pointed by p
Page 27