Single linked list

Source: Internet
Author: User

Define a single-linked list structure

struct lnode{    elemtype data;     struct Lnode *next;} *linklist;

Initialize linked list (tail interpolation method)

voidCreatlist (Linklist &L)    {linklist p, h; cout<<"Please enter the length of the linked list"<<Endl; intN; CIN>>N; L= (linklist)malloc(sizeof(Lnode)); L->next =NULL; H=L; if(!L) {cout<<"Application Space Failure"<<Endl; } cout<<"Please enter a value for the new node"<<Endl;  while(n! =0) {p= (linklist)malloc(sizeof(Lnode)); CIN>> p->data; P->next = h->Next; H->next =p; H= h->Next; N--; }}
Find by Location
voidGetelem (Linklist &L) {cout<<"Please enter a location to find"<<Endl; inte; CIN>>e; Linklist P=L;  while(P->next! = NULL && E! =0) {p= p->Next; E--; }    if(E! =0) {cout<<"The location you are looking for is not in this list"<<Endl; }    Else{cout<<"The elements you are looking for are:"<< P->data <<Endl; }}
Insert an Element

voidInsertelem (Linklist &L)    {Elemtype data; inte; cout<<"Enter the value and location of the inserted element, separated by spaces"<<Endl; CIN>>data; CIN>>e; linklist s, p=L; S= (linklist)malloc(sizeof(Lnode)); S->data =data;  while(P->next! = NULL && E! =1) {p= p->next;; E--; }    if(E >1) {cout<<"you have entered a location more than the length of this table!"<<Endl; } s->next = p->Next; P->next =s;}
Delete an element

voidDeleteelem (Linklist &L) {    inte; Linklist P= L,q =NULL; cout<<"Please enter delete element location"<<Endl; CIN>>e;  while(P->next! = NULL && E! =1) {p= p->Next; E--; }    if(E >1) {cout<<"you have entered a location more than the length of this table!"<<Endl; }    Else{Q= p->Next; P->next = q->Next;  Free(q); }}
Go through the list again
void Showlist (linklist &L) {    " go through the list " << Endl;     = L;      while (L->next! = NULL)    {        = l->next;         ' ' ;    }     << Endl;}
Full code
#include"stdafx.h"#include<iostream>#include<stdlib.h>using namespaceStd;typedefintElemtype;//define a single-linked list structuretypedefstructlnode{elemtype data; structLnode *next;}*linklist;//Initialize the list of linksvoidCreatlist (Linklist &L)    {linklist p, h; cout<<"Please enter the length of the linked list"<<Endl; intN; CIN>>N; L= (linklist)malloc(sizeof(Lnode)); L->next =NULL; H=L; if(!L) {cout<<"Application Space Failure"<<Endl; } cout<<"Please enter a value for the new node"<<Endl;  while(n! =0) {p= (linklist)malloc(sizeof(Lnode)); CIN>> p->data; P->next = h->Next; H->next =p; H= h->Next; N--; }}//Find by LocationvoidGetelem (Linklist &L) {cout<<"Please enter a location to find"<<Endl; inte; CIN>>e; Linklist P=L;  while(P->next! = NULL && E! =0) {p= p->Next; E--; }    if(E! =0) {cout<<"The location you are looking for is not in this list"<<Endl; }    Else{cout<<"The elements you are looking for are:"<< P->data <<Endl; }}//Insert an elementvoidInsertelem (Linklist &L)    {Elemtype data; inte; cout<<"Enter the value and location of the inserted element, separated by spaces"<<Endl; CIN>>data; CIN>>e; linklist s, p=L; S= (linklist)malloc(sizeof(Lnode)); S->data =data;  while(P->next! = NULL && E! =1) {p= p->next;; E--; }    if(E >1) {cout<<"you have entered a location more than the length of this table!"<<Endl; } s->next = p->Next; P->next =s;}//Delete an elementvoidDeleteelem (Linklist &L) {    inte; Linklist P= L,q =NULL; cout<<"Please enter delete element location"<<Endl; CIN>>e;  while(P->next! = NULL && E! =1) {p= p->Next; E--; }    if(E >1) {cout<<"you have entered a location more than the length of this table!"<<Endl; }    Else{Q= p->Next; P->next = q->Next;  Free(q); }}//Go through the list againvoidShowlist (Linklist &L) {cout<<"Go through the list again"<<Endl; Linklist l=L;  while(L->next! =NULL) {L= l->Next; cout<< L->data <<' '; } cout<<Endl;}

Single 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.