第五篇:基本数据结构——线性表的链式表示
来源:互联网 发布:新倩女幽魂辅助软件 编辑:程序博客网 时间:2024/05/17 04:56
以下为操作链表的算法,该链表为动态单链表。
链表以头指针为索引,头指针指向头节点,头节点指向首节点,以此类推,直到尾节点。
头节点中不存放数据,只存放指向首节点的指针,
设置头节点的目的是为了方便对链表的操作,如果不设置头节点,而是直接由头指针指向首节点,
这样在对头指针后的节点进行插入删除操作时就会与其他节点进行该操作时有所不同,便要作为一种特殊情况来分析
操作系统:ubuntu
编译软件:gcc
结果截图:
源代码:
- #include<stdio.h>
- #include<stdlib.h>
- #include<stdbool.h>
-
- typedef struct Node
- {
- int data;
- struct Node *pNext;
- }NODE,*PNODE;
-
- PNODE create_list();
- void traverse_list(PNODE);
- bool is_empty(PNODE);
- int length_list(PNODE);
- void sort_list(PNODE);
- bool insert_list(PNODE,int,int);
- bool delete_list(PNODE,int,int *);
- void clear_list(PNODE);
-
- int main(void)
- {
- int len;
- int data_del;
- PNODE pHead = NULL;
-
-
- pHead = create_list();
- traverse_list(pHead);
-
-
- len = length_list(pHead);
- if(!is_empty(pHead))
- printf("the length of the list is:%d\n",len);
-
-
- if(insert_list(pHead,3,78))
- printf("insert succeed,");
- else
- printf("insert failed,");
- traverse_list(pHead);
-
-
- if(delete_list(pHead,3,&data_del))
- {
- printf("delete succeed,the deleted data is:%d\n",data_del);
- }
- else
- printf("delete failed,");
- traverse_list(pHead);
-
-
- sort_list(pHead);
- printf("After sorted,");
- traverse_list(pHead);
-
-
- clear_list(pHead);
- printf("After cleared,");
- traverse_list(pHead);
- return 0;
- }
-
-
- PNODE create_list()
- {
- int val;
- PNODE pHead =(PNODE)malloc(sizeof(NODE));
- PNODE pCurrent = pHead;
- pCurrent->pNext = NULL;
- if(NULL == pHead)
- {
- printf("pHead malloc failed!");
- exit(-1);
- }
- printf("Input first data(q to quit):");
- while(scanf("%d",&val)==1)
- {
- PNODE pNew = (PNODE)malloc(sizeof(NODE));
- if(NULL == pNew)
- {
- printf("pNew malloc failed!");
- exit(-1);
- }
- pNew->data = val;
- pCurrent->pNext = pNew;
- pNew->pNext = NULL;
- pCurrent = pNew;
- printf("Input next data(q to quit):");
- }
- return pHead;
- }
-
-
-
- void traverse_list(PNODE pHead)
- {
- PNODE pCurrent = pHead->pNext;
- printf("now dataes in the list are:\n");
- while(pCurrent != NULL)
- {
- printf("%d ",pCurrent->data);
- pCurrent = pCurrent->pNext;
- }
- printf("\n");
- return ;
- }
-
- bool is_empty(PNODE pNode)
- {
- if(NULL == pNode->pNext)
- return true;
- else
- return false;
- }
-
-
- int length_list(PNODE pNode)
- {
- int count = 0;
- PNODE pCurrent = pNode->pNext;
- while(pCurrent != NULL)
- {
- count++;
- pCurrent = pCurrent->pNext;
- }
-
- return count;
- }
-
-
- void sort_list(PNODE pHead)
- {
- PNODE p,q;
- int temp;
- for(p=pHead->pNext;p!=NULL;p=p->pNext)
- for(q=p->pNext;q!=NULL;q=q->pNext)
- {
- if(p->data>q->data)
- {
- temp = p->data;
- p->data = q->data;
- q->data = temp;
- }
- }
-
- return ;
- }
-
-
- bool insert_list(PNODE pHead,int pos,int val)
- {
- int i = 0;
- PNODE p = pHead;
-
-
-
- while(p!=NULL && i<pos)
- {
- p = p->pNext;
- i++;
- }
-
-
- if(i>pos || p==NULL)
- return false;
-
- PNODE pNew = (PNODE)malloc(sizeof(NODE));
- if(NULL == pNew)
- {
- printf("pNew malloc failed!");
- exit(-1);
- }
- pNew->data = val;
- pNew->pNext = p->pNext;
- p->pNext = pNew;
-
- return true;
- }
-
-
- bool delete_list(PNODE pHead,int pos,int *pData)
- {
- int i = 0;
- PNODE p = pHead;
-
-
-
-
- while(p->pNext!=NULL && i<pos)
-
- {
- p = p->pNext;
- i++;
- }
-
-
- if(i>pos-1 || p->pNext==NULL)
- return false;
-
- PNODE q = p->pNext;
- *pData = q->data;
- p->pNext = p->pNext->pNext;
- free(q);
- q = NULL;
- return true;
- }
-
-
- void clear_list(PNODE pHead)
- {
- PNODE p = pHead->pNext;
- PNODE r = NULL;
- while(p != NULL)
- {
- r = p->pNext;
- free(p);
- p = r;
- }
- pHead->pNext = NULL;
- return ;
- }