线性链表及其应用
1.线性链表节点ADT定义
typedef int ElmType;
typedef struct node{
ElmType data;
struct node* next;
}Node;
typedef enum ret_type {ERROR=-1,OK = 0} Status;
typedef Node* linkedList;
1.2 产生一个节点
//产生一个节点
linkedList node_create(ElmType data)
{
linkedList node=(linkedList)malloc(sizeof (Node));
node->data=data;
node->next=NULL;
return node;
}
1.3 打印链表
//打印链表
Status list_print(linkedList head)
{
if(head==NULL)
{
return ERROR;
}
linkedList ptr = head->next;
while (ptr)
{
printf("%5d",ptr->data);
ptr=ptr->next;
}
printf(("n"));
return OK;
}
1.4在链表头部插入新的元素
//在链表头部插入新的元素
Status list_push_front(linkedList head,ElmType data)
{
if(head==NULL)
{
return ERROR;
}
if (head->next==NULL)
{
linkedList newNode= node_create(data);
head->next=newNode;
}
else
{
linkedList newNode= node_create(data);
newNode->next=head->next;
head->next=newNode;
}
}
1.5 在链表头部删除元素
//在链表头部删除元素
Status list_pop_front(linkedList head,ElmType* data)
{
if(head==NULL)
{
return ERROR;
}
else
{
linkedList ptr=head->next;
head->next=ptr->next;
printf("%d",ptr->next);
free(ptr);
}
}



