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