linked lists
Is it possible to add or remove elements on a particular position in a linked list.
Well I'm trying to write it myself, not using STL lists. Here is what I have done up to now.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
struct PC
{
string model;
PC *next;
}*first=NULL,*last=NULL;
void add()
{
PC *p=new PC;
cin>>p->model;
p->next=NULL;
if(!first)
{
first=last=p;
}
else
{
last->next=p;
}
}
void print_l()
{
PC* p=first;
while(p)
{
cout<<p->model<<endl;
p=p->next;
}
}
int main()
{
int choice;
while(1)
{
printf("1.Add\n2.Print\n");
cin>>choice;
switch(choice)
{
case 1: add();break;
case 2: print_l();break;
default: exit(0);
}
}
}
|
I don't have a clue about deleting.
Topic archived. No new replies allowed.