链表的实现

来源:互联网 发布:google 高程提取软件 编辑:程序博客网 时间:2024/06/04 19:52
#include<stdio.h>#include<stdlib.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2typedef int Status;typedef char ElemType;typedef struct LNode{  ElemType data;  struct LNode* next;}LNode,*LinkList;void CreateList_L(LinkList*L,int n){  int i;  LNode*p;  *L = (LinkList)malloc(sizeof(LNode));  (*L)->next = NULL;  for(i=n;i>0;--i)    {      p = (LinkList)malloc(sizeof(LNode));      scanf("%c",&p->data);      p->next = (*L)->next;(*L)->next = p;    }  p = (*L)->next;}//CreateList Lvoid PrintList_L(LinkList L){  LNode*p = L->next;  while(p!=NULL)    {      printf("%c",p->data);      p = p->next;    }}//Print all the datas of the LinkListStatus InsertList_L(LinkList*L,int pos,ElemType e){  LNode* p = *L,*q;  int i = 0;  while(p!=NULL && i<pos-1)    {      p = p->next;      i++;    }  if(p == NULL || pos-1<i)return ERROR;  q = (LNode*)malloc(sizeof(LNode));  q->data = e;q->next = p->next;  p->next = q;  return OK;}//Insert a node at the position posStatus DeleteList_L(LinkList*L,int pos,ElemType*e){  LNode*p = *L,*q;  int i = 0;  while(p->next!=NULL && i<pos-1)    {      p = p->next;      i++;    }  if(p->next==NULL || pos-1<i)return ERROR;  q = p->next;  *e = q->data;  p->next = q->next;  free(q);  return OK;}int main(){  LinkList L = NULL;  ElemType e;   CreateList_L(&L,5);  PrintList_L(L);  InsertList_L(&L,3,'9');  printf("\n");  PrintList_L(L);  printf("\n");  DeleteList_L(&L,2,&e);  PrintList_L(L);  printf("\nThe data %c has been deleted!\n",e);  return OK;  }

0 0
原创粉丝点击