C language implementation of common data structure--linked list

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>typedefstructNode {intdata; structNode *Next;} node;/*Initialize linked list: 1. First assign space to the head pointer and assign the head pointer to TEMP2. Second, allocate space for each element 3. Next pointer to the current node assigned to the data,null of the current node 4. Assign the current node (head pointer) Next pointer to previous node 5. The previous node pointer moves back, ready to initialize the next element 6. Finally, the head pointer of the current list is returned.*/node*Initnode () {inti; Node*p = (node*)malloc(sizeof(node)); Node*temp =p;  for(i =0; I <Ten; i++) {node*a = (node*)malloc(sizeof(node)); A->data =i; A->next =NULL; Temp->next =A; Temp= temp->Next; }    returnp;}/*insert element at the specified position: 1. Assign the head pointer to temp2.temp until you specify the position 3. Allocates space for the element that needs to be inserted, and assigns the content to the element that allocates the space 4. Assigns the next element of the insertion position to the following pointer to the element to be inserted 5. The next pointer to temp is assigned to the insertion element 6. The head pointer back to the current list*/node*insertelem (Node *p,intElemintPOS) {    inti; Node*temp =p;  for(i =0; I < POS; i++) {Temp= temp->Next; } node*c = (node*)malloc(sizeof(node)); C->data =Elem; C->next = temp->Next; Temp->next =C; returnp;}/*deletes the element at the specified location: 1. Assigns the head pointer to the TEMP2.TEMP until the specified position is 3. Declares a variable that represents the node to be deleted 4. Next pointer to the previous node of the node to be deleted is assigned to Node 5. Release the node space to be deleted 6. Finally returns the head pointer of the current list*/node*delelem (Node *p,intPOS) {    inti; Node*temp =p;  for(i =0; I < POS; i++) {Temp= temp->Next; } node*c = temp->Next; Temp->next = c->Next;  Free(c); returnp;}/*Query the location of the specified element: 1. Assign the head pointer to temp2.temp until the value of the temp node equals the element that you want to query 3. Returns the index4 of the current node. If the list traversal ends and the specified node is not found, returns-1*/intSelectelem (Node *p,intelem) {Node*temp =p; inti =0;  while(temp->next) {Temp= temp->Next; if(Temp->data = =elem) {            returni; } I++; }    return-1;}/*updating the value of a linked list specified node*/node*amendelem (Node *p,intPosintNewelem) {    inti; Node*temp =p;  for(i =0; I < POS; i++) {Temp= temp->Next; } node*amend = temp->Next; Amend->data =Newelem; returnp;}/*traversing a linked list*/voidDisplay (node *p) {node*temp =p;  while(temp->next) {Temp= temp->Next; printf ("%d", temp->data); } printf ("\ n");}//The following is from the data structure and algorithm-C language DescriptionintIsEmpty (Node *p) {returnp->next==NULL;}intIslast (Node *pos,node *p) {returnpos->next==NULL;}//deletes the element at the specified positionNode *delete (node *p,intelem) {Node*temp=p; Node*tmp;  while(temp->next) {        if(temp->next->data==elem) {tmp=temp->Next; Temp->next=tmp->Next;  Free(TMP); } temp=temp->Next; }    returnp;}//inserts an element at a specified locationNode *insert (node *p,intPosintelem) {Node*temp=p; inti;  for(i=0; i<pos; i++) {Temp=temp->Next; } node*cell = (node*)malloc(sizeof(node)); Cell->data=Elem; Cell->next=temp->Next; Temp->next=cell; returnp;}//Delete ListvoidDeleteList (Node *p) {node*temp=p; Node*tmp; P->next=NULL;  while(temp->next) {tmp=temp->Next;  Free(TMP); Temp=temp->Next; }}intMain () {node*p =Initnode ();    Display (p); printf ("Insert element at 4th position 5:\n"); P= Insert (P,4,5);    Display (p); printf ("Remove the 3rd element: \ n"); P= Delelem (P,3);    Display (p); printf ("the location of the Find element 2 is: \ n"); intAddress = Selectelem (p,2); if(Address = =-1) {printf ("without the element \ n"); } Else{printf ("the seat of element 2 is:%d\n", address); } printf ("change the number of the 3rd position to 7:\n"); P= Amendelem (P,3,7);    Display (p); printf ("delete7\n"); P=delete (P,7);    Display (p); printf ("delete \ n");    DeleteList (P); Display (P);}

C language implementation of common data structure--linked list

Related Article

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.