The basic operation of a single-linked list (build. Traverse. Add. Delete)

Source: Internet
Author: User

There are n elements, we put them in a linked list, do some operations to add a number in a location, or delete a position of the number;

Enter a valid code:

#include <iostream>#include<stdio.h>#include<stdlib.h>using namespacestd;structnode{intdate; structNode *next;}; Node*creatlist (Node *head,intn);voidTravlist (Node *head); node*addlist (Node *head,intNintPosintnum); node*dellist (Node *head,intNintPOS);intMain () {intN, POS, num; Node*Head;  while(SCANF ("%d", &n)! =EOF) {Head= (node*)malloc(sizeof(node)); Head=Creatlist (head, N);        Travlist (head); scanf ("%d%d", &pos, &num);///Insert num at pos position;Head = Addlist (head, n+1, POS, num);        Travlist (head); scanf ("%d", &pos);///deletes the number of POS positions;Head = Dellist (head, n+1, POS);    Travlist (head); }    return 0;} Node*creatlist (Node *head,intN) {Head->next =NULL;  for(intI=1; i<=n; i++) {node*p; P= (node *)malloc(sizeof(node)); scanf ("%d", &p->date); P->next = head->Next; Head->next =p; }    returnhead;}voidTravlist (Node *head) {Node*Real; Real= head->Next;  while(Real! =NULL) {printf ("%d", real->date); Real= real->Next; } printf ("\ n");} Node*addlist (Node *head,intNintPosintnum) {Node*pre, *p, *Q; Pre=Head; P= pre->Next; intI=1;  while(i<POS) {Pre=p; P= p->Next; I++; } q= (node*)malloc(sizeof(node)); Q->date =num; Q->next = pre->Next; Pre->next =Q; returnHead;} Node*dellist (Node *head,intNintPOS) {Node*pre, *p; Pre=Head; P= pre->Next; intI=1;  while(i<POS) {Pre=p; P= p->Next; I++; } Pre->next = p->Next;  Free(P); returnhead;}/*2 3 4 1015 4 3 2 (4 ) 3 2 3 2 1*/
View Code

Code that is available for all inputs:

#include <stdio.h>#include<stdlib.h>#defineOK 1#defineERROR 0typedefintStatus;typedefstructlnode{intdata; structLnode *Next;} Lnode,*linklist;intm=sizeof(Lnode); Linklist Creatlist (intN); Linklist Creatlistrear (intn);voidtrealist (linklist L); Status Listdel (linklist L,intk); Status Listadd (linklist L,intNumintx); Linklist Creatlist (intN///Insert from the front of the table;{linklist head,p; inti; Head= (linklist)malloc(m); Head->next=NULL;  for(i=1; i<=n;i++) {p= (linklist)malloc(m); scanf ("%d",&p->data); P->next=head->Next; Head->next=p; }    returnHead;} Linklist Creatlistrear (intN///Insert from the back of the table;{linklist rear,p,head; inti; Head= (linklist)malloc(m); Head->next=NULL; Rear=Head;  for(i=0; i<n;i++) {p= (linklist)malloc(m); scanf ("%d",&p->data); Rear->next=p; Rear=Q; } Rear->next=NULL; returnhead;}voidTrealist (linklist L)///traverse a single linked list;{linklist p; P=l->Next;  while(p) {printf ("%d",p->data); P=p->Next; } printf ("\ n");} Status Listdel (linklist L,intK///Delete elements of a single linked list;{    intj=1; Linklist P=l,q=l->Next;  while(q&&j<k) {p=Q; Q=q->Next; J++; }    if((q==0&& j<=k) | | k<1 )        returnERROR; P->next=q->Next;  Free(q); returnOK;} Status Listadd (linklist L,intNumintX///add elements of a single linked list;{linklist p,q,s; P=m; Q=p->Next; intj=1;  while(q&&j<x) {p=Q; Q=q->Next; J++; }    if((q==0&AMP;&AMP;J&LT;X) | | x<1)        returnERROR; S= (linklist)malloc(m); S->data=num; S->next=p->Next; P->next=s; returnOK;}intMain () {intn,k,num,x;  while(SCANF ("%d", &n)! =EOF)        {linklist L; //l=creatlist (n);L=creatlistrear (n);//two ways to establish a linked list;trealist (L); printf ("Please enter a location to delete \ n"); scanf ("%d",&k); intm=Listdel (l,k); if(m==1) printf ("Delete succeeded \ n"); Elseprintf ("Delete failed \ n");        Trealist (L); printf ("Please enter the number and location to insert \ n"); scanf ("%d%d",&num,&x); M=Listadd (l,num,x); if(m==1) printf ("Insert succeeded \ n"); Elseprintf ("insert failed \ n");    Trealist (L); }    return 0;}
View Code

The basic operation of a single-linked list (build. Traverse. Add. Delete)

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.