单链表实现行编辑

来源:互联网 发布:淘宝怎么秒杀 编辑:程序博客网 时间:2024/06/15 02:23

头文件

Word_Stack.h

#include<cstdlib>
#include<iostream>
#include<string>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
typedef char SElemType;
typedef int Status;
typedef struct word
{
 SElemType data;
 struct word* next;
}word, *Word;

//构造一个空栈S
Status InitStack(Word& S)
{
 S = (Word)malloc(sizeof(word));
 if (!S)
  exit(OVERFLOW);
 S->next = NULL;
 return OK;
}

//把栈S置为空
Status ClearStack(Word& S)
{
 if (S->next)
 {
  Word p = S->next;
  while (p)
  {
   S->next = p->next;
   free(p);
   p = S->next;
  }
 }
 return OK;
}

//销毁栈S
Status DestroyStack(Word& S)
{
 ClearStack(S);
 free(S);
 S = NULL;
 return OK;
}

//若栈S为空栈,则返回TRUE,否则返回FALSE
Status StackEmpty(Word S)
{
 if (!S->next)
  return TRUE;
 else
  return FALSE;
}

//返回栈S的元素个数,即栈的长度
int StackLength(Word S)
{
 int length = 0;
 Word p = S->next;
 while (p)
 {
  ++length;
  p = p->next;
 }
 return length;
}

//若栈S不空,用e返回S的栈顶元素,并返回OK,否则返回ERROR
Status GetTop(Word S, SElemType& e)
{
 if (!StackEmpty(S))
 {
  e = S->next->data;
  return OK;
 }
 return ERROR;
}

//插入元素e为新的栈顶元素
Status Push(Word& S, SElemType e)
{
 Word s = (Word)malloc(sizeof(word));
 if (!s)
  exit(OVERFLOW);
 s->data = e;
 s->next = S->next;
 S->next = s;
 return OK;
}

//若栈不空,则删除S的栈顶元素,并用e返回其值,并返回OK;否则返回ERROR
Status Pop(Word& S, SElemType& e)
{
 if (!StackEmpty(S))
 {
  Word p = S->next;
  e = p->data;
  S->next = p->next;
  free(p);
  return OK;
 }
 return ERROR;
}

//打印输出栈中元素
Status visit(SElemType data)
{
 if (cout<<data)
  return OK;
 else
  return ERROR;
}

//从栈底到栈顶依次对栈中每个元素调用函数visit().一旦visit()失败,则操作失败
Status StackTraverse(Word S, Status(*visit)(SElemType))
{
 int i = StackLength(S);
 while (1)
 {
  Word p = S;
  for (int j = 0; j < i; ++j)
   p = p->next;
  if (!(*visit)(p->data))
   return ERROR;
  if (S->next == p)
  {
   cout << " ";
   return OK;
  }
  --i;
 }
}

//把字符串转换成字符并用栈存储
Word input(char* str)
{
 char ch;
 Word words=NULL;
 int i = 0;
 if (str[0] == '\0')
  return NULL;
 else
 {
  InitStack(words);
  while (str[i]!= '\0')
  {
   switch (str[i])
   {
   case '@':
    ClearStack(words); break;
   case '#':
    if(!StackEmpty(words))
     Pop(words, ch);
    break;
   default:Push(words, str[i]);
    break;
   }
   i++;
  }
  return words;
 }
}

string char_into_string(char* ch)
{
 string str = ch;
 return str;
}


Line_Link.h

#include"Word_Stack.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status;

typedef struct line
{
 Word elem;
 struct line* next;
}line,*Line, *Position;
typedef struct
{
 Line head, tail;
 int len;
}LinkList_Line;

//分配由p指向的值为e的结点,并返回OK;若分配失败,则返回ERROR
Status MakeNode(Line& p,Word e)
{
 p = (Line)malloc(sizeof(line));
 if (!p)
  return ERROR;
 p->elem = e;
 return OK;
}

//释放p所指向的结点
void FreeNode(Line& p)
{
 free(p);
 p = NULL;
}

//构造一个空的线性链表L
Status InitList(LinkList_Line& L)
{
 L.head = (Line)malloc(sizeof(line));
 L.head->next = NULL;
 L.tail = L.head;
 L.len = 0;
 return OK;
}

//销毁线性链表L
Status DestroyList(LinkList_Line& L)
{
 Line p = L.head->next;
 while (p)
 {
  Line q = p->next;
  FreeNode(p);
  p = q;
 }
 FreeNode(L.head);
 return OK;
}

//将线性链表L置为空表
Status ClearList(LinkList_Line& L)
{
 if (!L.head->next)
 {
  L.tail = L.head;
  L.len = 0;
  return OK;
 }
 Line p = L.head->next;
 while (p)
 {
  Line q = p->next;
  FreeNode(p);
  p = q;
 }
 L.head->next = NULL;
 L.tail = L.head;
 L.len = 0;
 return OK;
}

//已知h指向线性链表的头结点,将s所指向结点插入在第一个结点之前
Status InsFirst(Line h, Line s)
{
 s->next = h->next;
 h->next = s;
 return OK;
}

//已知h指向线性链表的头结点,删除链表中的第一个结点并以q返回
Status DelFirst(Line h, Line& q)
{
 q = h->next;
 if (q)
 {
  h->next = q->next;
  q->next = NULL;
  return OK;
 }
 return ERROR;
}

//将指针s所指向的一串结点链接在线性链表L的最后一个结点之后,并改变链表L的尾指针指向新的尾结点
Status Append(LinkList_Line& L, Line s)
{
 int num = 0;
 L.tail->next = s;
 while (s)
 {
  num++;
  if (!s->next)
  {
   L.tail = s;
   L.len += num;
   return OK;
  }
  s = s->next;
 }
 return ERROR;
}

//删除线性链表L中的尾结点并以q返回,改变链表L的尾指针指向新的尾结点
Status Remove(LinkList_Line& L, Line& q)
{
 if (L.head == L.tail)
  return ERROR;
 Line s = L.head;
 while (s)
 {
  if (L.tail == s->next)
  {
   q = s->next;
   s->next = NULL;
   L.tail = s;
   L.len--;
   return OK;
  }
  s = s->next;
 }
 return ERROR;
}

//已知p指向线性链表L中的一个结点,将s所指结点插入在p所指结点之前,并修改指针p指向新插入的结点
Status InsBefore(LinkList_Line& L, Line& p, Line s)
{
 Line q = L.head;
 while (q)
 {
  if (q->next == p)
  {
   q->next = s;
   s->next = p;
   p = s;
   L.len++;
   return OK;
  }
  q = q->next;
 }
 return ERROR;
}

//已知p指向线性链表L中的一个结点,将s所指结点插入p所指结点之后,并修改指针p指向新插入的结点
Status InsAfter(LinkList_Line& L, Line& p, Line s)
{
 s->next = p->next;
 p->next = s;
 p = s;
 if (!p->next)
  L.tail = p;
 L.len++;
 return OK;
}

//已知p指向线性链表中的一个结点,用e更新p所指结点中数据元素的值
Status SetCurElem(Line& p, Word e)
{
 p->elem = e;
 return OK;
}

//已知p指向线性链表中的一个结点,返回p所指向结点中数据元素的值
Word GetCurElem(Line p)
{
 return p->elem;
}

//若线性链表L为空,则返回TRUE;否则返回FALSE
Status ListEmpty(LinkList_Line L)
{
 if (!L.len)
  return TRUE;
 else
  return FALSE;
}

//返回线性链表L中元素个数
int ListLength(LinkList_Line L)
{
 return L.len;
}

//返回线性链表L中头结点的位置
Position GetHead(LinkList_Line L)
{
 return L.head;
}

//返回线性链表L中最后一个结点的位置
Position GetLast(LinkList_Line L)
{
 if (L.head->next)
  return L.tail;
 else
  return NULL;
}

//已知p指向线性链表L中的一个结点,返回p所指结点的直接前驱的位置,若无前驱则返回NULL
Position PriorPos(LinkList_Line L, Line p)
{
 if (p == L.head->next)
  return NULL;
 Line q = L.head->next;
 while (q&&q->next)
 {
  if (q->next == p)
   return q;
  q = q->next;
 }
 return NULL;
}

//已知p指向线性链表L中的一个结点,返回p所指结点的直接后继的位置,若无后继则返回NULL
Position NextPos(LinkList_Line L, Line p)
{
 if (p->next == NULL)
  return NULL;
 return p->next;
}

//返回p指示线性链表L中第i个结点的位置,并返回OK,i值不合法时返回ERROR
Status LocatePos(LinkList_Line L, int i, Line& p)
{
 if (i<1 || i>ListLength(L))
  return ERROR;
 int j = 0;
 p = L.head;
 while (j<i)
 {
  p = p->next;
  ++j;
 }
 return OK;
}

//关系函数,例如小于等于
Status compare(Word a, Word b)
{
 if (a->data<=b->data)
  return TRUE;
 else
  return FALSE;
}

//返回线性链表L中第1个与e满足函数compare()判定关系的元素的位置,若不存在这样的元素则返回NULL
Position LocateElem(LinkList_Line L, Word e, Status(*compare)(Word, Word))
{
 Line p = L.head->next;
 while (p&&!(*compare)(e, p->elem))
 {
  p = p->next;
  if (!p)
   return NULL;
 }
 if (!p)
  return NULL;
 else
  return p;
}

//访问并打印
Status display(Word i)
{
 if (StackTraverse(i,visit))
  return OK;
 else
  return ERROR;
}

//依次对L的每个元素调用函数visit().一旦visit()失败,则操作失败
Status ListTraverse(LinkList_Line L, Status(*display)(Word))
{
 Line p = L.head->next;
 while (p)
 {
  if (!(*display)(p->elem))
   return ERROR;
  p = p->next;
  if (!p)
   cout << endl;
 }
 return OK;
}

//在带头结点的单链表L的第i个元素之前插入元素e
Status ListInsert(LinkList_Line& L, int i,Word e)
{
 Line h, s;
 if (!(MakeNode(s, e)))
  return ERROR;
 if (1 == i)
 {
  InsFirst(L.head, s);
  L.tail = s;
  L.len++;
  return OK;
 }
 if (!LocatePos(L, i - 1, h))
  return ERROR;
 InsFirst(h, s);
 while (s)
 {
  if (!s->next)
   L.tail = s;
  s = s->next;
 }
 L.len++;
 return OK;
}

//在带头结点的单链表L中删除第i个元素,并用e返回
Status ListDelete(LinkList_Line& L, int i, Word& e)
{
 Line p;
 if (!LocatePos(L, i, p))
  return ERROR;
 e = p->elem;
 Line s = PriorPos(L, p);
 if (!s)
  return ERROR;
 s->next = p->next;
 if (!p->next)
  L.tail = s;
 FreeNode(p);
 L.len--;
 return OK;
}


主程序:

#include"Line_Link.h"
int main(void)
{
 LinkList_Line L;
 InitList(L);
 int i = 1;
 char ch[15];
 cout<< "请输入文本:"<<endl;
 while(1)
 {
  cout<< "请输入第"<<i<<"个单词:";
  cin>> ch;
  if (char_into_string(ch) == "exit")
   break;
  Word s=input(ch);
  ListInsert(L, i, s);
  ++i;
 }
 system("cls");
 ListTraverse(L, display);
 return 0;
}


1 0
原创粉丝点击