南邮数据结构实验1.2:带表头结点单链表的相关操作

题目:参照程序2.8~2.14,编写程序,完成带表头结点单链表的初始化、查找、插入、删除、输出、撤销等操作。

部分代码

带表头结点单链表的结构体定义:

typedef struct Node {
	ElemType element;     //结点的数据域
	struct Node * link;   //结点的指针域
}Node;

typedef struct {
	struct Node* head;    //表头结点
	int n;
}HeaderList;

带表头结点单链表的插入函数:

//带表头结点单链表的插入
Status Insert(HeaderList *h, int i, ElemType x) {
	Node *p, *q;
	int j;
	if (i<-1 || i>h->n - 1)
		return ERROR;
	p = h->head;                      //从头结点开始找ai元素所在的结点p
	for (j = 0; j <= i; j++) {
		p = p->link;
	}
	q = (Node*)malloc(sizeof(Node));  //生成新结点q
	q->element = x;
	q->link = p->link;                //新结点q插在p之后
	p->link = q;
	h->n++;
	return OK;
}

带表头结点单链表的删除函数:

//带表头结点单链表的删除
Status Delete(HeaderList *h,int i){
    int j;
    Node *p,*q;
    if(!h->n){
        return ERROR;
        if(i<0||i>h->n-1){
            return ERROR;
        }
    }
    q=h->head;
    for(j=0;j<i;j++){
        q=q->link;
    }
    p=q->link;
    q->link=p->link;                //从单链表中删除p所指结点
    free(p);                        //释放p所指结点的存储空间
    h->n--;
    return OK;
}

完整程序:

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef int Status;
#define ERROR 0
#define OK 1

typedef struct Node {
	ElemType element;     //结点的数据域
	struct Node * link;   //结点的指针域
}Node;

typedef struct {
	struct Node* head;    //表头结点
	int n;
}HeaderList;


//带表头结点单链表的初始化
Status Init(HeaderList *h) {
    h->head=(Node*)malloc(sizeof(Node));      //生成表头结点
    if(!h->head){
        return ERROR;
    }
	h->head->link = NULL;                     //设置单链表为空表
	h->n = 0;
	return OK;
}


//带表头结点单链表的查找
Status Find(HeaderList *h,int i,ElemType *x){
    Node *p;
    int j;
    if(i<0||i>h->n-1){
        return ERROR;
    }
    p=h->head->link;
    for(j=0;j<i;j++){
        p=p->link;
    }
    *x=p->element;
    return OK;
}


//带表头结点单链表的插入
Status Insert(HeaderList *h, int i, ElemType x) {
	Node *p, *q;
	int j;
	if (i<-1 || i>h->n - 1)
		return ERROR;
	p = h->head;                      //从头结点开始找ai元素所在的结点p
	for (j = 0; j <= i; j++) {
		p = p->link;
	}
	q = (Node*)malloc(sizeof(Node));  //生成新结点q
	q->element = x;
	q->link = p->link;                //新结点q插在p之后
	p->link = q;
	h->n++;
	return OK;
}


//带表头结点单链表的删除
Status Delete(HeaderList *h,int i){
    int j;
    Node *p,*q;
    if(!h->n){
        return ERROR;
        if(i<0||i>h->n-1){
            return ERROR;
        }
    }
    q=h->head;
    for(j=0;j<i;j++){
        q=q->link;
    }
    p=q->link;
    q->link=p->link;                //从单链表中删除p所指结点
    free(p);                        //释放p所指结点的存储空间
    h->n--;
    return OK;
}


//带表头结点单链表的输出
Status Output(HeaderList *h) {
	Node *p;
	if (!h->n)
		return ERROR;
	p = h->head->link;
	while (p) {
		printf("%d ",p->element);
		p = p->link;
	}
	return OK;
}


//带表头结点单链表的撤销
void Destroy(HeaderList *h){
    Node *p,*q;
    while(h->head->link){
        q=h->head->link;
        p=h->head->link->link;
        free(h->head->link);
        h->head=q;
    }
}



void main()
{
	int i, x;
	HeaderList list;
	Init(&list);
	for (i = 0; i < 9; i++) {
		Insert(&list, i - 1, i);    //插入0~8
	}
    printf("the linklist is:");
	Output(&list);
    Delete(&list,0);                //删除0
    printf("\nthe linklist is:");
    Output(&list);
    Find(&list,0,&x);               //查找下标为0的元素
	printf("\nthe value is:");
	printf("%d ",x);
    Destroy(&list);
}

实验结果:

版权声明:本文为博主原创文章,未经博主允许不得转载。

在编写程序以实现带表头结点单链表操作时,你需要定义链表节点结构体,包含数据域和指向下一个节点的指针,并创建相应的链表类。以下是基本的操作: 1. **初始化(Initialization)**: - 创建一个链表头节点,初始值通常为空,`head = NULL`。 - 如果需要插入元素,可以构造一个新的节点,将头节点指向这个新节点。 ```c++ struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} // 构造函数 }; ListNode* initializeList() { ListNode* head = new ListNode(0); // 创建空链表 return head; } ``` 2. **查找(Search)**: - 使用指针遍历链表,直到找到目标值或遍历到末尾。 ```c++ bool search(ListNode* head, int target) { ListNode* current = head; while (current != NULL && current->val != target) { current = current->next; } return current != NULL; // 返回true表示找到,false未找到 } ``` 3. **插入(Insertion)**: - 可以在指定位置插入新节点,或者在链表末尾添加新节点。 ```c++ void insertNode(ListNode*& head, int pos, int value) { if (pos == 0) { // 插入到头部 ListNode* newNode = new ListNode(value); newNode->next = head; head = newNode; } else { ListNode* prev = nullptr; ListNode* current = head; for (int i = 0; i < pos - 1 && current != NULL; ++i) { prev = current; current = current->next; } if (prev) { newNode = new ListNode(value); newNode->next = current; prev->next = newNode; } } } ``` 4. **遍历并输出(Traversal and Output)**: - 遍历整个链表,打印每个节点的值。 ```c++ void printList(ListNode* head) { ListNode* current = head; while (current != NULL) { std::cout << current->val << " "; current = current->next; } std::cout << std::endl; } ``` 5. **删除(Deletion)**: - 指定位置删除节点,这里仅给出删除头节点的例子,其他位置删除则需要额外处理。 ```c++ void deleteNode(ListNode*& head, int pos) { if (pos == 0) { ListNode* temp = head; head = head->next; delete temp; } else { ListNode* prev = nullptr; ListNode* current = head; for (int i = 0; i < pos - 1 && current != NULL; ++i) { prev = current; current = current->next; } if (prev) { prev->next = current->next; delete current; } } } ``` 6. **撤销(Undo)**: - 实现撤销功能取决于具体的上下文。如果是在编辑操作场景,这可能是回滚到上一次更改。但在链表操作中,通常没有直接的“撤销操作,除非记录每一次操作前的状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wonz

创作不易,一块就行。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值