c語言刪除函數
『壹』 c語言編寫一個插入刪除函數
一般呢,插入和刪除函數是分開寫的,還有分成兩種存儲結構,1.順序表,2.鏈表,我給你一個我上數據結構時候寫的鏈表的操作,裡面全都有,如果不會用,追問我
#include<stdio.h>
#include<malloc.h>
#include<Windows.h>
#include<conio.h>
#include<stdlib.h>
typedef struct
{
int data;
struct LNode *next;
}LNode;
LNode *Listinit(LNode *L)//初始化鏈表返還頭指針
{
L = (LNode *)malloc(sizeof(LNode));
if (!L)return 0;
L->next = NULL;
return L;
}
int GetElem_L(LNode *L, int i, int *e)//取第i個元素
{
int j;
LNode *p;
p=L->next;j=1;
while(p&&j<i)
{
p=p->next;++j;
}
if(!p||j>i) return 0;//i超過表長
*e=p->data;
return 1;
}
int ListInsert_L(LNode *L, int i, int e)//插入數據元素
{
LNode *p1 = L,*p2=L;
int j = 0;
if (i-1 > LinkLength(L))
return 2;
while(p1!=NULL && j<i-1)
{
p1 = p1->next;
j++;
}
p2 = (LNode *)malloc(sizeof(LNode));
if (!p2)
return 0;
p2->data = e;
p2->next = p1->next;
p1->next = p2;
return 1;
}
void ClearList(LNode *L)//重置為空表
{
LNode *p;
while(L->next)
{
p=L->next;
L->next=p->next;
free(p);
}
}
void print_link(LNode *L)//輸出函數
{
LNode *p = L;
p = p->next;
while (p != NULL)
{
printf("%5d", p->data);
p = p->next;
}
}
int ListDlete_L(LNode *L, int i, int *e)//刪除L中I,並用e返回
{
int j = 0;
LNode *p1 = NULL, *p2 = NULL;
p1 = L;
while (p1->next != NULL && j < i - 1)
{
p1 = p1->next;
j++;
}
if (p1->next == NULL || j > i - 1)
return 0;
p2 = p1->next;
p1->next = p2->next;
free(p2);
return 1;
}
int LinkLength(LNode *L)//鏈表的長度
{
int i = 0;
LNode *p = L->next;
while (p != NULL)
{
i++;
p = p->next;
}
return i;
}
『貳』 C語言刪除字元串中指定字元
一、問題描述:從鍵盤輸入一個字元串給str和一個字元給c,刪除str中的所有字元c並輸出刪除後的字元串str。
1、輸入:第一行是一個字元串; 第二行是一個字元。
2、輸出:刪除指定字元後的字元串。
二、設計思路:
1、 同插入問題,定義兩個字元數組a,b。以及標志刪除位置的int型pos。
2、用gets函數輸入數組a的值,並利用for循環將數組a 到 數組b。
3、利用for循環,令pos位的數組b元素賦值到a。
三、實現代碼如下:
(2)c語言刪除函數擴展閱讀
gets()函數用來從標准輸入設備(鍵盤)讀取字元串直到換行符結束,但換行符會被丟棄,然後在末尾添加'