Linked List Code for Beginners

#include "stdafx.h"
#include <iostream>

#ifndef _DSCLASS_H_
#define _DSCLASS_H_
class linkedlist{
private:
struct node{
int data;
node *link;
}*list;
public:
linkedlist();
~linkedlist();
//stack methods
void push_item(int num);
void pop_item();

//queue methods
void append_item(int num);
void delete_item();

//other insertion and deletion methods
void insert_item(int pos,int num);
void delete_item(int pos);

//other operations
void reverse();
void merge(linkedlist l1, linkedlist l2);
linkedlist operator +(linkedlist l1);

void display();
};
linkedlist::linkedlist()
{
list = NULL;
}
linkedlist::~linkedlist()
{
//TODO: yet to be implemented
}

void linkedlist::insert_item(int pos, int num)
{

if (pos<0) return;
printf("inserting %d at %dth position...\n", num, pos);

node *newone;
node *ptr = list;

if (pos==0)
{
newone = new node();
newone->data = num;
newone->link = ptr;
list = newone;
return;
}

for (int i=0;i<pos-1;i++)
{
if (ptr!=NULL) ptr=ptr->link;
else return;
}

newone = new node();
newone->data = num;

if(ptr->link!=NULL) newone->link = ptr->link;
else newone->link = NULL;

ptr->link =newone;


}

void linkedlist::delete_item(int pos)
{
if (list == NULL && pos<0) return;
printf("deleting item at %d position...\n",pos);
if (pos==0)//deleting the head
{
node *temp = list;
list = list->link;
delete temp;
return;
}

node *ptr = list;
node *temp;
for (int i=0;i<pos-1;i++)
{
if (ptr!=NULL)
{
temp = ptr;
ptr = ptr->link;
}
else return;
}

if (ptr->link!=NULL)
temp->link = ptr->link;
else
temp->link = NULL;

delete ptr;

}

void linkedlist::push_item(int num)
{
printf("pushing %d..\n",num);
if (list == NULL)
{
list = new node();
list->data = num;
list->link = NULL;
}
else
{
node* t = new node();
t->data = num;
t->link = list;
list =t;
}

}

void linkedlist::append_item(int num)
{
printf("appending %d..\n",num);
if (list == NULL)
{
list = new node();
list->data = num;
list->link = NULL;
}
else
{
node* par = list;
while (par->link != NULL)
{
par=par->link;
}
//now we are at the end
node *newone = new node();
newone->data = num;
newone->link = NULL;
par->link = newone;
}
}


void linkedlist::pop_item()
{
printf("popping ..\n");
if (list ==NULL)
{
printf("empty list\n");
return;
}
else
{
node *del = list;
list = list->link;
delete del;
}
}


void linkedlist::delete_item()
{
printf("deleting ..\n");
if (list==NULL)
{
printf("empty list\n");
return;
}
else
{
node *del = list;
node *temp = NULL;
while(del->link!=NULL)
{
temp = del;
del = del->link;
}
if (temp!=NULL) temp->link = NULL;
delete del;
if(del==list) list = NULL;

}
}
void linkedlist::display()
{
if (list==NULL)
return;

node *dis = list;
char str[100]="";
while (dis!=NULL)
{
sprintf(str,"%s%d->",str,dis->data);
dis=dis->link;
}
printf("%sNULL\n",str);
}

void linkedlist::reverse()
{
printf("reversing...");
if (list == NULL && list->link == NULL) return;
node *current = list;
node *prev = NULL;
node *next = NULL;
while (current!=NULL)
{
next = current->link;
current->link = prev;
prev= current;
current = next;
}
list = prev;

}

void linkedlist::merge(linkedlist l1, linkedlist l2)
{

node *top = l1.list;
while (top!=NULL)
{
append_item(top->data);
top = top->link;
}
top = l2.list;
while (top!=NULL)
{
append_item(top->data);
top = top->link;
}

}

linkedlist linkedlist:: operator +(linkedlist l1)
{
linkedlist temp;

node *top = this->list;
while (top!=NULL)
{
temp.append_item(top->data);
top = top->link;
}

top = l1.list;
while (top!=NULL)
{
temp.append_item(top->data);
top = top->link;
}
return temp;
}
#endif

0 comments: