One-way linked table shift

Source: Internet
Author: User

One-way linked table shift

Structure
9th questions

[Description]

Enter several positive integers. Input-1 ends and the positive integers are organized in a one-way linked list. Define and call the function: struct node * createList (void), create a one-way linked list, and return a pointer to the head node of the linked list. Define and call a function: void printList (struct node * head) to output a one-way linked list. Define and call the function: int sum (struct node * head), calculate and return the sum of integers in all nodes. Define and call the function: struct node * cyclicShift (struct node * head, DIR dir) to realize cyclic shift of the linked list. DIR is of the enumeration type, with LEFT and RIGHT values, when the dir value is LEFT, the cycle shifts LEFT once. When the dir value is RIGHT, the cycle shifts RIGHT once. the pointer pointing to the head node of the linked list after the cycle shift is returned.

[Input]

Enter several integers, ending with-1, and the integers are separated by spaces.

[Output]

The first line outputs the sum of integers.

The output result of the second row is shifted to the left, and the integers are separated by spaces. There is no space after the last integer.

The output result of the third row is shifted to the right by a space interval between integers. There is no space after the last integer.

[Input example]

1 3 5 2-1

[Output example]

11

3 5 2 1

1 3 5 2

Tip]

The type declaration of the node Structure in a one-way linked list is as follows:

Struct node {

Int num;/* store a positive integer */

Struct node * next;

};

The definition of the enumeration type DIR is as follows:

Enum DIR {LEFT, RIGHT };

Implement cyclic shift of the linked list. When the cycle moves left, delete the first node of the linked list and insert it to the end of the linked list. When the cycle moves right, delete the last node of the linked list, and insert it into the head of the linked list.

[Source]

Program Design Basics-take C as an example. Chapter 4 Computer Experiment 4.






# Include
 
  
# Include
  
   
// You have learned a lot about this question... Struct node {int num; struct node * next;}; enum DIR {LEFT, RIGHT}; // This enumeration should be defined at the beginning, because the subsequent functions need to use it, the struct node * createList (void) {struct node * head = NULL, * tail, * t; // The head must be initialized to NULL! Otherwise, it will be difficult to write int num; unsigned int size = sizeof (struct node); // the sizeof structure can be done directly. Note that the structure Name node must be added! Scanf ("% d", & num); while (num> 0) {t = (struct node *) malloc (size ); // dynamically allocate the memory and save it in t-> num = num; t-> next = NULL; // first, the chain table is regarded as a unit. if (head = NULL) head = t; // you can determine whether the chain table is empty. elsetail-> next = t; // after the first time, the tail here is used to direct the next of the last linked list to the chain table tail = t. // tail is the pointer of the current linked list, returning to the next loop represents the pointer of the previous linked list. Scanf ("% d", & num);} return head;} // create a linked table void printList (struct node * head) {struct node * p = NULL; int I = 0; for (p = head; p! = NULL; p = p-> next) {if (I ++) // here I ++ is used correctly. It will still be 0 at the beginning, printf ("% d", p-> num); elseprintf ("% d", p-> num );} printf ("\ n");} // output chain table struct node * javasicshift (struct node * head, enum DIR) {struct node * p, * n; // note that the pointer types must be the same structure type as the linked list. if (dir = LEFT) {n = head; // n is used to save the header pointer head = head-> next; // the header Pointer Points to the next p = head; while (p-> next! = NULL) {p = p-> next;} // locate the last linked list and save it in P. n-> next = NULL; // The tail of the pointer is not NULL. First, it is NULL, that is, the tail pointer p-> next = n; // Insert a new linked list n after the last linked list p, that is, the original head linked list} else if (dir = RIGHT) {// enumeration can still represent this enumeration type without adding any symbols such as "", and can judge the relationship between p = head; while (p-> next! = NULL) {p = p-> next;} // find the second to last pointer n = p-> next; // use n to save the last pointer p-> next = NULL; // change the last pointer to n-> next = head; head = n; // append the last pointer n to the beginning and change it to head.} Return head;} // cyclic shift int sum (struct node * head) {int s = 0; struct node * p = NULL; for (p = head; p! = NULL; p = p-> next) {s + = p-> num;} return s;} // sum int main () {struct node * head, * p = NULL; head = createList (); int s; s = sum (head); printf ("% d \ n", s); enum DIR dir; head = policicshift (head, LEFT); // you can directly write enumeration items when Enumeration type is used as a parameter. No need to add the symbol // head = cyclicShift (head, (enum DIR) 0); because the element in the enumeration is essentially a number, but the type has changed, so the forced type conversion is still possible // head = cyclicShift (head, 0), but it is still possible to use the compiler, but it is not possible on OJ, because the parameter type and function definition do not match printList (head); head = policicshift (head, RIGHT); printList (head); return 0 ;}
  
 


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.