Linked List of Data Structure
Linked List of Data Structure
The first chapter of the data structure is the linked list. The linked list is the basis of other structures, including stacks and trees. The linked list is composed of nodes, with a header pointer and a header node at the frontend, the header pointer is a required element of a linked list. The header node can make the linked list more identical, but the header node is not a required element of a linked list. Now we use a struct as a node. A node contains two parts: the data domain and the pointer domain. The data domain is used to store the content to be stored, and the pointer domain stores the address of the next struct.
Typedef struct student
{
Char name [20];
Int age;
Int number;
Struct student * next;
} STU;
At this time, an STU is a node, which stores a student's name, age, student ID, and pointer to this struct type. It is not a pointer to itself.
Next, I will post a simple single-chain table with student information as the node, and movie information as the node to create, delete, and other operations have been appended with comments
# Include
# Include
# Include
# Include
Typedef struct student/* takes a struct as a node */{char name [20]; int age; int number; struct student * next; /* a pointer to the struct type */} STU; STU * start (STU * L, int n)/* Create a linked list using the header Insertion Method (first come first) */{int I; STU * p; L = (STU *) malloc (sizeof (STU);/* create node */(L)-> next = NULL; for (I = 0; I
Name, & p-> age, & p-> number); // printf ("% s % d \ n", p-> name, p-> age, p-> number); p-> next = (L-> next); (L)-> next = p;} return L;} void print (STU * L ); void print (STU * L)/* output linked list */{// printf ("********* \ n"); STU * temp; // printf ("********* \ n"); temp = L-> next; // printf ("% p \ n", temp ); while (temp! = NULL) {// printf ("********* \ n"); printf ("% s % d ***** \ n ", temp-> name, temp-> age, temp-> number); temp = temp-> next ;}} int main () {int n; STU * L; printf ("please enter a num"); scanf ("% d", & n); L = start (L, n);/* pay attention to the function return value mechanism, the returned value must have an accepted variable * // printf ("% p \ n", L); print (L); return 0 ;}
# Include
# Include
# Include
# Include
Typedef struct film/* single-chain table creation method 2, tail Insertion Method */{/* use a struct as a node */char * name; // name = (char *) malloc (10 * sizeof (char); int num; struct film * next;} FILM; FILM * make (FILM * L, int n) /* create a single-chain table */{int I; FILM * p, * r; L = (FILM *) malloc (sizeof (FILM); if (NULL = L) {return-1;} L-> name = (char *) malloc (10 * (sizeof (char); r = L; /* use R to track new nodes */for (I = 0; I
Name = (char *) malloc (10 * (sizeof (char); if (NULL = p) {return-1;} scanf ("% s % d ", p-> name, & p-> num);/* put the node at the end of the table */r-> next = p; r = p;} r-> next = NULL; return L;} void print (FILM * L); void print (FILM * L)/* output function */{FILM * pTemp; pTemp = L-> next; while (pTemp! = NULL) {printf ("% s % d", pTemp-> name, pTemp-> num); pTemp = pTemp-> next; printf ("\ n") ;}} int main () {int n; FILM * L; printf ("please enter some thing"); scanf ("% d ", & n); L = make (L, n); // clearList (L); print (L); return 0;} int clearList (FILM * L ); /* delete a linked list */int clearList (FILM * L) {FILM * p, * q; p = L-> next; while (p) {q = p-> next; free (q); p = q;} L-> next = NULL; return 0;} int getelem (FILM * L, int I ); int getelem (FILM * L, int I)/* searches for the content of node I and displays */{int j; FILM * p; p = L-> next; j = 1; while (p & j
Next; ++ j;} if (! Q | j> I) {return-1;} else {printf ("% s % d \ n", p-> name, p-> num );} return 0;} int Listinsert (FILM * L, int I);/* Add a node to I */int Listinsert (FILM * L, int I) {int j; FILM * p, * s; p = L; j = 1; while (p & I
Next; ++ j;} if (! P | j> I) {return-1;} else {s = (FILM *) malloc (sizeof (FILM); scanf ("% s % d ", s-> name, & s-> num); s-> next = p-> next; p-next = s;} return 0;} int Listdel (FILM * L, int I);/* delete a node */int Listdel (FILM * L, int I) {int j; FILM * p, * q; p = * L; j = 1; while (p-> next & j
Next; ++ j;} if (! (P-> next) | j> I) {return-1 ;}else {q = p-> next; p-> next = q-> next; free (q) ;}return 1 ;}
# Include
# Include
# Include
# Include
Typedef struct film/* create a single-chain table with only header nodes */{char * name; int num; struct film * next;} FILM; FILM * make (FILM * L, int n) {int I; FILM * p, * r; L = (FILM *) malloc (sizeof (FILM); r = L; for (I = 0; I
Name = (char *) malloc (10 * sizeof (char); scanf ("% s % d", p-> name, & p-> num ); r-> next = p; r = p;} r-> next = NULL; return L;} int main () {int n; FILM * L; printf ("please enter some thing"); scanf ("% d", & n); L = make (L, n); print (L); return 0 ;} int print (FILM * L); int print (FILM * L) {FILM * pTemp; pTemp = L-> next; while (pTemp! = NULL) {printf ("% s % d \ n", pTemp-> name, pTemp-> num); pTemp = pTemp-> next ;}}