C and Pointer 12th chapter structure exercises

Source: Internet
Author: User

12.3 Rewrite 12.7, using the head and tail pointers, respectively, to pass to the function as a single pointer, rather than as part of a node

#include <stdio.h> #include <stdlib.h> TRUE 1#define FALSE 0//pointer fwd points to the previous node, BWD points to the latter node typedef struct    Node {struct node *fwd;    struct NODE *bwd; int value;} node;/* pointer to the pointer to the head and tail nodes, four cases * inserted into the header, * inserted into the footer, * inserted into the empty table, * inserted into the table, the first three need to modify the headptr or tailptr pointer */int doublelinklistinsert (N    Ode **headptr, node **tailptr, int value) {node *this = *headptr;    Node *newnode;    while (This! = NULL && This, value < value) {this = this = FWD;    } NewNode = (node *) malloc (sizeof (node));    NewNode, value = value;            if (this = NULL) {//Insert to footer, or empty table if (this = = *headptr) {//INSERT into empty table *headptr = NewNode;            *tailptr = NewNode;            NewNode, fwd = NULL;        NewNode-BWD = NULL;            }else{//Insert to the end of the table newNode, fwd = NULL;            The original footer element is the front node of the current node NewNode-BWD = *tailptr;            (*tailptr), fwd = NewNode; Update tail node pointer *tailPTR = NewNode;            }}else{//INSERT into the header, or the table if (this = = *headptr) {//INSERT into the header newNode-bwd = NULL;            The original table head becomes the second node newNode, fwd = *headptr;            (*headptr), bwd = NewNode;        Update table Header *headptr = NewNode;            }else{//Insert to non-empty table in front of this position newNode, fwd = this;            NewNode-BWD = BWD;            This, bwd, fwd = NewNode;        This, BWD = NewNode; }} return TRUE;    int main () {Node third;    Node second;    Node first;    Third = (Node) {NULL, &second, 4};    Second = (Node) {&third, &first, 2};    First = (Node) {&second, NULL, 1};    Node *head = &first;    Node *tail = &third;    Doublelinklistinsert (&head, &tail, 35);    Doublelinklistinsert (&head, &tail, 3);    Doublelinklistinsert (&head, &tail,-10);    Node *rootptr = head; while (rootptr! = NULL) {printf ("%D\t ", rootptr-value);    Rootptr = Rootptr, fwd; } return 0;}

Run:

12.4 Write functions reverse-order all nodes of a single-linked list.

#include <stdio.h> #include <stdlib.h> #define TRUE 1#define FALSE 0typedef struct Node {struct node *next; int value;} linklist;    Linklist *sll_reverse (linklist *first) {if (first = = null) {return null;    } linklist *current = First;    Linklist *next;    Linklist *pre;    linklist *morepre = NULL;        while (next = current, next) = NULL) {//loop to move the node that is currently pointing to Pre = present;        current = next;        The next pointer of the previous node is modified by the previous node pre-and next = Morepre;    Morepre pointer morepre = Pre before moving the front node;    }//If the IF (current = = first) is returned between the individual nodes {return first;        }else{//modifies the last node pointer, as the head pointer returns the position of the last node, current, next = pre;    return current;    }}int Main () {linklist third = {NULL, 4};    linklist second = {&third, 3};    Linklist first = {&second, 2};    linklist *head = &first;    Head = Sll_reverse (head);        while (head! = NULL) {printf ("%d\t", head-and-value); Head = HeadNext } return 0;}

Run:

12.5 Write a program, remove a node from a single linked list, the first parameter is a pointer to a pointer to the head of the list

#include <stdio.h> #include <stdlib.h> #define TRUE 1#define FALSE 0typedef struct Node {struct node *next; int value;}    linklist;//Remove node Nodeint sll_delete (linklist **first, linklist *node) from the linked list pointed to by first, {linklist **ptr = first;    PTR is a pointer to the next field while (*ptr! = NULL && *ptr! = node) {ptr = & ((*ptr), next);    }//If not found if (*ptr = = NULL) {return FALSE;    }else{//If found, the pre-change node points to *ptr = (*ptr), next; Release node memory free (*PTR);
return FALSE; }}int Main () {linklist third = {NULL, 3}; linklist second = {&third, 2}; Linklist first = {&second, 1}; linklist *headptr = &first; linklist *head = headptr; while (head! = NULL) {printf ("%d\t", head-and-value); Head = head Next; } printf ("\ n"); Sll_delete (&headptr, &second); head = headptr; while (head! = NULL) {printf ("%d\t", head-and-value); Head = head Next; } printf ("\ n"); Sll_delete (&headptr, &first); head = headptr; while (head! = NULL) {printf ("%d\t", head-and-value); Head = head Next; } printf ("\ n"); Sll_delete (&headptr, &third); head = headptr; while (head! = NULL) {printf ("%d\t", head-and-value); Head = head Next; } return 0;}

  

Run:

12.6 The node is removed from the doubly linked list, and the first parameter is a pointer to the head of the linked list,

#include <stdio.h> #include <stdlib.h> #define TRUE 1#define FALSE 0typedef struct Node {struct node *next; int value;}    linklist;//Remove node Nodeint sll_delete (linklist *first, linklist *node) from the linked list pointed to by first, {linklist *pre = NULL;    linklist *cur = First;        while (cur = NULL && cur! = node) {pre = cur;    cur = cur next;    }//If not found if (cur = = NULL) {return FALSE;        }else if (cur = = first) {//At this point a value is passed in, only the value pointed to by the head pointer can be modified, modified to the second node *first = * (cur, next);        Free (node);    return TRUE;        }else{Pre, next = cur next;        Free (node);    return TRUE;    }}int Main () {linklist third = {NULL, 3};    linklist second = {&third, 2};    Linklist first = {&second, 1};    linklist *headptr = &first;    linklist *head = headptr;        while (head! = NULL) {printf ("%d\t", head-and-value);    Head = head Next;    } printf ("\ n");    Sll_delete (Headptr, &second);head = headptr;        while (head! = NULL) {printf ("%d\t", head-and-value);    Head = head Next;    } printf ("\ n");    Sll_delete (Headptr, &first);        while (headptr! = NULL) {printf ("%d\t", headptr-value);    Headptr = Headptr Next;    } printf ("\ n");    Sll_delete (Headptr, &third);    head = headptr;        while (head! = NULL) {printf ("%d\t", head-and-value);    Head = head Next;    } printf ("\ n"); return 0;}

Run:

12.7 Creating a Word Index Table

C and Pointer 12th chapter structure exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.