ok, i have completed the code, but i keep on getting errors that I don't know how to fix, this is an emplementation of the double linked list, using structs, can anyone help whats wrong?
Thanks alot
#include <iostream>
using namespace std;
typedef char ListEntry;
typedef int position;
typedef struct listnode
{
ListEntry entry;
struct listnode *next;
struct listnode *previous;
} ListNode;
typedef struct list
{
int count;
ListNode *current;
position currentpos;
} List;
ListNode* MakeListNode (ListEntry x)
{
ListNode *p= malloc(sizeof(ListNode));
if (p)
{
p->entry=x;
p->next=NULL;
p->previous=NULL;
} else
{
cout<<"No space available.\n"<<endl;
}
return p;
}
void setPosition (position p, List **list)
{
if (p<0 || (p >= (list->count) ) )
{
cout<<"Position is not in the list.\n"<<endl;
} else if (list->currentpos < p)
{
for (; list->currentpos !=p; list->currentpos++)
{
list->current=list->current->next;
}
} else if (list->currentpos>p)
{
for (; list->currentpos != p; list-> currentpos--)
{
list->current=list->current->previous;
}
}
}
void insertList (position p, ListEntry x, List *list)
{
ListNode *newnode, *following;
if (p<0 || p>list->count)
{
cout<<"Position not in list.\n"<<endl;
} else {
newnode=MakeListNode(x);
if (p==0)
{
newnode->previous=NULL;
if (list->count==0)
{
newnode->next=NULL;
} else
{
setPosition (0, &list);
newnode->next=list->current;
list->current->previous=newnode;
}
} else
{
setPosition (p-1, &list);
following=list->current->next;
newnode->next=following;
newnode->previous=list->current;
list->current->next=newnode;
if (following)
{
following->previous=newnode;
}
}
list->current=newnode;
list->currentpos=p;
list->count++;
}
}
void deletList (position p, List *list)
{
ListNode *current, *before, *following;
if (p<0 || p>=list->count)
{
cout<<"Position not in list.\n"<<endl;
}else if (list->count == 0)
{
cout<<"List is empty.\n"<<endl;
} else
{
if (p==0)
{
list->currentpos=list->currentpos->nex...
if (list->count > 0)
{
list->currentpos->previous=NULL;
}
}else
{
setPosition (p, ¤t);
setPosition (p-1, &before);
if (current->next != NULL )
{
setPosition (p+1, &following);
following->previous=current->previous;
}
list->count--;
}
list->count--;
}
}
void printList (List *list)
{
if (list->count == 0)
{
cout<<"List is empty.\n"<<endl;
} else
{
cout<<"The content of the list is:"<<endl;
for (int i=0; i< list->count; i++)
{
setPosition (i, &list);
cout<<list->current->entry<<" ";
}
cout<<endl;
}
}
int main ()
{
ListNode *listnode;
List *list;
insertList (2, 'e', list);
insertList (0, 'o', list);
insertList (1, 'n', list);
printList (list);
deletList (2, list);
deletList (0, list);
deletList (1, list);
printList (list);
cin.ignore (100, '\n');
cin.get ();
return 0;
}
0 коммент.:
Post a Comment