头文件
SList.h
#ifndef __SLIST_H__
#define __SLIST_H__
#include"Utili.h"
#include<assert.h>
#define ElemType int
typedef struct ListNode
{
ElemType data;
struct ListNode *next;
}ListNode;
typedef struct List
{
ListNode *first;
ListNode *last;
size_t size;
}List;
//
void InitList(List *list);
bool push_back(List *list,ElemType x);
void show_list(List *list);
bool push_front(List *list,ElemType x);
bool pop_back(List *list);
bool insert_val(List *list,ElemType x);
bool delete_val(List *list,ElemType x);
//
void InitList(List *list)
{
list->first = list->last =(ListNode*)malloc(sizeof(ListNode));
list->last->next = NULL;
assert(list->first != NULL);
list->size = 0;
}
bool push_back(List *list,ElemType x)
{
ListNode *s = (ListNode*)malloc(sizeof(ListNode));
if(s == NULL)
{
return false;
}
s->data = x;
s->next = NULL;
list->last->next = s;
list->last = s;
list->size++;
return true;
}
bool push_front(List *list,ElemType x)
{
ListNode *s = (ListNode*)malloc(sizeof(ListNode));
if(s == NULL)
{
return false;
}
s->data = x;
s->next = list->first->next;
list->first->next = s;
if(list->size == 0)
{
list->last=s;
}
list->size++;
return true;
}
void show_list(List *list)
{
ListNode *p =list->first->next;
while(p !=NULL)
{
cout<<p->data<<"->";
p = p->next;
}
cout<<"over!"<<endl;
}
bool pop_back(List *list)
{
if(list->size == 0)
{
return false;
}
ListNode *p = list->first;
while(p != list->last)
{
p = p->next;
}
p->next = NULL;
free(list->last);
list->last = p;
list->size--;
}
bool insert_val(List *list,ElemType x)
{
ListNode *p =list->first;
while(p->next != NULL && x > p->next->data)
{
p = p->next;
}
ListNode *s = (ListNode*)malloc(sizeof(ListNode));
assert(s != NULL);
s->data = x;
s->next = NULL;
s->next = p->next;
p->next = s;
if(p == list->last)
{
list->last = s;
}
list->size++;
return true;
}
bool delete_val(List *list,ElemType x)
{
ListNode *p =list->first;
while(x != p->next->data)
{
p = p->next;
}
p->next = p->next->next;
list->size--;
return true;
}
#endif
TestSList.cpp
源文件
#include<iostream>
#include"SList.h"
using namespace std;
int main()
{
List mylist;
InitList(&mylist);
int select = 1;
ElemType item;
int pos;
while(select){
cout<<"**************************************************************"<<endl;
cout<<"*[1]push_back [2]push_front *"<<endl;
cout<<"*[3]show_list [0]quit_system *"<<endl;
cout<<"*[4]pop_back [5]pop_front *"<<endl;
cout<<"*[6]insert_pos [7]insert_val *"<<endl;
cout<<"*[8]delete_pos [9]delete_val *"<<endl;
cout<<"*[10]find_key [11]length *"<<endl;
cout<<"*[12]reverse_list [13]sort *"<<endl;
cout<<"*[14]clear_list [15]destory_list *"<<endl;
cout<<"**************************************************************"<<endl;
cout<<"请选择:>";
cin>>select;
switch(select){
case 1:
cout<<"请输入要插入的数据(-1结束):>";
while(cin>>item,item!=-1){
push_back(&mylist,item);
}
break;
case 2:
cout<<"请输入要插入的数据(-1结束):>";
while(cin>>item,item!=-1){
push_front(&mylist,item);
}
break;
case 3:
show_list(&mylist);
break;
case 4:
pop_back(&mylist);
break;
case 5:
//pop_front(&mylist);
break;
case 6:
//cout<<"请输入要插入的位置:>";
//cin>>pos;
//cout<<"请输入要插入的值:>";
//cin>>item;
//insert_pos(&mylist,pos,item);
case 7:
cout<<"请输入要插入的值"<<endl;
cin>>item;
insert_val(&mylist,item);
break;
case 8:
//cout<<"请输入要删除的位置:>";
//cin>>pos;
//delete_pos(&mylist,pos);
break;
case 9:
//cout<<"请输入要删除的值:>";
//cin>>item;
//delete_val(&mylist,item);
break;
case 10:
//cout<<"请输入要找到的值:>";
//find_key(&mylist,item);
break;
case 11:
//cout<<"SeqList Length = "<<Length(&mylist)<<endl;
break;
case 12:
//reverse_list(&mylist);
case 13:
//sort(&mylist);
break;
case 14:
//clear_list(&mylist);
break;
case 15:
//destory_list(&mylist);
break;
case 0:
break;
}
system("pause");
system("cls");
}
return 0;
}